JAVA8的时间日期

  • LocalDate、LocalTime、LocalDateTime 类的实例是不可变的对象,分别表示使用 ISO-8601日 历系统的日期、时间、日期和时间。
  • Instant用于“时间戳”的运算。它是以Unix元年(传统 的设定为UTC时区1970年1月1日午夜时分)开始 所经历的描述进行运算。
  • Duration:用于计算两个“时间”间隔。
  • Period:用于计算两个“日期”间隔。
  • TemporalAdjusters : 该类通过静态方法提供了大量的常 用 TemporalAdjuster 的实现。
  • 时间日期格式化使用:java.time.format.DateTimeFormatter 类。

简单的示例代码如下:

        LocalDateTime localDate = LocalDateTime.now();
        System.out.println(localDate);//输出:2018-09-09T10:40:08.055
        System.out.println(localDate.getDayOfMonth());//输出:9
        LocalDateTime localDateTime = LocalDateTime.of(2018,9,8,12,23,44);
        System.out.println(localDateTime); //输出:2018-09-08T12:23:44

        Instant instant = Instant.now();
        System.out.println(instant);//输出:2018-09-09T02:40:08.056Z
        System.out.println(instant.toEpochMilli());//输出:1536460808056

        //Duration 计算两个时间之间的间隔
        Duration duration = Duration.between(instant,Instant.now());
        System.out.println(duration.toMillis());//输出:13
        System.out.println(duration.toNanos());//输出:13000000
        System.out.println(duration.getNano());//输出:13000000

        //Period 计算两个日期之间的间隔
        LocalDate localDate2 = LocalDate.now();
        LocalDate localDate1 = LocalDate.of(2018,7,1);
        Period period =  Period.between(localDate1,localDate2);
        System.out.println(period.getYears());//输出:0
        System.out.println(period.getDays());//输出:8
        System.out.println(period.getMonths());//输出:2

        //TemporalAdjuster

        LocalDateTime temp = localDate.with(TemporalAdjusters.firstDayOfYear());
        System.out.println(temp);//输出:2018-01-01T10:40:08.055
        System.out.println(localDate.with(TemporalAdjusters.next(DayOfWeek.FRIDAY)));//输出:2018-09-14T10:40:08.055

        //返回下一个休息日,周六或者周日

        LocalDateTime myTime = localDate.plusDays(13).with((e)->{
            LocalDateTime localDateTime1 = (LocalDateTime) e;
            DayOfWeek dayOfWeek = localDateTime1.getDayOfWeek();
            if(dayOfWeek.equals(DayOfWeek.SUNDAY)){
                return localDateTime1.plusDays(6);
            }else if(dayOfWeek.equals(DayOfWeek.MONDAY)){
                return localDateTime1.plusDays(5);
            }else if(dayOfWeek.equals(DayOfWeek.TUESDAY)){
                return localDateTime1.plusDays(4);
            }else if(dayOfWeek.equals(DayOfWeek.WEDNESDAY)){
                return localDateTime1.plusDays(3);
            }else if(dayOfWeek.equals(DayOfWeek.THURSDAY)){
                return localDateTime1.plusDays(2);
            }else{
                return localDateTime1.plusDays(1);
            }

        });
        System.out.println("myTime:"+myTime);//myTime:2018-09-23T10:40:08.055


        //DateTimeFormatter
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.BASIC_ISO_DATE;
        String string = myTime.format(dateTimeFormatter);
        System.out.println(string);//20180923

        dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //都有format方法,用哪个都可以
        System.out.println(myTime.plusHours(13).format(dateTimeFormatter));//2018-09-23 23:48:39
        System.out.println(dateTimeFormatter.format(myTime));//2018-09-23 10:48:39

        LocalDate ldt = LocalDate.parse(string,DateTimeFormatter.BASIC_ISO_DATE);
        System.out.println(ldt);//2018-09-23
        String date = "20180909";
        System.out.println(LocalDate.parse(date,DateTimeFormatter.BASIC_ISO_DATE));//2018-09-09

猜你喜欢

转载自blog.csdn.net/zwq_zwq_zwq/article/details/82556661