JAVA8新特性——新的时间日期api

一.为什么使用java8

1.java.util.Date缺点:
(1)年份是从1970开始算的;
(2)DateFormat方法不是线程安全的。

package newDataApi;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.*;

//SimpleDateFormat线程不安全
public class TestSimpleDateFormat {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");

        Callable<Date> task = new Callable<Date>() {
            @Override
            public Date call() throws Exception {
                return simpleDateFormat.parse("20161218" );
            }
        };

        ExecutorService pool = Executors.newFixedThreadPool(10);

        List<Future<Date>> results = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            results.add(pool.submit(task));
        }

        for (Future<Date> future : results){
            System.out.println(future.get());
        }
    }
}

出现问题:
JAVA8新特性——新的时间日期api_第1张图片
2.java.util.Calendar缺点:
和Date类一样都是可变的。而且经常会同时用Date和Calendar,很混乱。

3.只有提供一些常用的功能

二.源码

package newDataApi;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
import java.util.Set;


public class TestLocalDateTime {

    public static void main(String[] args) throws InterruptedException {
        TestLocalDateTime tldt = new TestLocalDateTime();
        tldt.test1();
        System.out.println("___");
        tldt.test2();
        System.out.println("___");
        tldt.test3();
        System.out.println("____");
        tldt.test4();
        System.out.println("____");
        tldt.test5();
        System.out.println("____");
        tldt.test6();
    }

    //1.LocalDate、LocalTime、LocalDateTime
    public void test1() {
        //1、输出当前时间
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime);
        //2.自己指定时间
        LocalDateTime localDateTime1 = LocalDateTime.of(2017, 7, 21, 21, 23, 12);
        System.out.println(localDateTime1);
        //3.给当前时间+2年或者-2年
        LocalDateTime localDateTime2 = localDateTime.plusYears(2);
        LocalDateTime localDateTime3 = localDateTime.minusYears(2);
        System.out.println(localDateTime2);
        System.out.println(localDateTime3);
        //4.获得当前时间对象中的年月日,时分秒等待信息
        System.out.println(localDateTime.getYear());
        System.out.println(localDateTime.getMonthValue());
        System.out.println(localDateTime.getDayOfMonth());
        System.out.println(localDateTime.getHour());
        System.out.println(localDateTime.getMonthValue());
        System.out.println(localDateTime.getSecond());
    }

    //2.Instant : 时间戳(以Unix元年: 1970.1.1 00:00:00到某个时间的毫秒值)
    public void test2() {
        //以UTC时间为基础的
        Instant now = Instant.now();
        System.out.println(now);
        //带偏移量的UTC时间
        OffsetDateTime offsetDateTime = now.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);
        //转换为毫秒值
        System.out.println(now.toEpochMilli());
        //在1970.1.1 00:00:00后面设置过去的时间
        Instant instant = Instant.ofEpochSecond(60);
        System.out.println(instant);
    }

    /**3.
     * Duration : 计算两个时间的间隔
     * Period : 计算两个日期之间的间隔
     */
    public void test3() throws InterruptedException {
        //查看两个时间之间的间隔
        Instant ins1 = Instant.now();
        Thread.sleep(1000);
        Instant ins2 = Instant.now();

        Duration between = Duration.between(ins1, ins2);
        System.out.println(between.toMillis());

        LocalDateTime ldt1 = LocalDateTime.now();
        Thread.sleep(1000);
        LocalDateTime ldt2 = LocalDateTime.now();

        Duration between1 = Duration.between(ldt1, ldt2);
        System.out.println(between1.toMillis());

        //查看两个日期之间的间隔
        LocalDate localDate = LocalDate.of(2014, 12, 21);
        LocalDate localDate1 = LocalDate.now();

        Period period = Period.between(localDate, localDate1);
        System.out.println(period.getYears());
        System.out.println(period.getMonths());
        System.out.println(period.getDays());
    }

    //4.TemporalAdjuster : 时间矫正器
    public void test4(){
        LocalDateTime ldt = LocalDateTime.now();
        System.out.println(ldt);

        LocalDateTime ldt1= ldt.withDayOfMonth(10);
        System.out.println(ldt1);

        LocalDateTime ldt2 = ldt.with(TemporalAdjusters.firstDayOfMonth());
        System.out.println(ldt2);

        //自定义下一个工作日
        LocalDateTime ldt5 = ldt.with((l) -> {
           LocalDateTime ldt4 = (LocalDateTime) l;
          DayOfWeek ldt3 = ldt4.getDayOfWeek();

           if (ldt3.equals(DayOfWeek.FRIDAY)){
               return ldt4.plusDays(3);
           }else if(ldt3.equals(DayOfWeek.SATURDAY)){
               return ldt4.plusDays(2);
           }else{
               return ldt4.plusDays(1);
           }
        });
        System.out.println(ldt5);
    }

    //5.DateTimeFormatter : 格式化时间或者日期
    public void test5() {
        DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE;
        LocalDateTime ldt = LocalDateTime.now();
        String format = ldt.format(dtf);
        System.out.println(format);

        //自定义时间格式
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        String format1 = ldt.format(dateTimeFormatter);
        System.out.println(format1);

        //将转换后的时间转换为原格式
        LocalDateTime parse = ldt.parse(format1, dateTimeFormatter);
        System.out.println(parse);
    }

    //ZonedDate、 ZonedTime 、ZonedDateTime
    public void test6(){
        //获取所有的时区
        //Set availableZoneIds = ZoneId.getAvailableZoneIds();
        //自己指定时区
        LocalDateTime now = LocalDateTime.now();
        ZonedDateTime zonedDateTime = now.atZone(ZoneId.of("Europe/Tallinn"));
        System.out.println(zonedDateTime);
    }
}

JAVA8新特性——新的时间日期api_第2张图片

不同看视频:https://www.bilibili.com/video/BV1xb411w7jM?p=17

你可能感兴趣的:(spring,boot)