美文网首页java
29.JDK8开始新增日期API:Peroid、Duration

29.JDK8开始新增日期API:Peroid、Duration

作者: 每天起床都想摆 | 来源:发表于2022-02-09 17:38 被阅读0次

Duration/Period

Period

  • 在Java8中,我们可以使用以下类来计算日期间隔差异:java.time.Period

    比如一个日期到另一个日期相隔了多少天

  • 主要是Period类方法getYear(),getMonth() 和 getDays() 来计算,只能精确到年月日

  • 用于LocalDate之间的比较

    LocalDate today = LocalDate.now();
    System.out.println(today);
    LocalDate birthDay = LocalDate.of(1995, 1, 11);
    System.out.println(birthDay);
    Period period = Period.between(birthDate, today);
    System.out.println("年龄:%d年%d月%d日", period.getYears(), period.getMonths(), period.getDays());
    
  • 测试代码:

    package com.java.jdk8time_update;
    
    import java.time.LocalDate;
    import java.time.Period;
    
    public class PeriodTest {
        public static void main(String[] args) {
            // 当前本地 年月日
            LocalDate today = LocalDate.now();
            System.out.println(today);
    
            // 生日的年月日
            LocalDate birthDate = LocalDate.of(2000,12,16);
            System.out.println(birthDate);
            
            // 第二个参数减去第一个参数,返回一个间隔对象
            Period period = Period.between(birthDate, today);
            
            // 从间隔对象中获取年,获取月,获取日
            System.out.println(period.getYears());
            System.out.println(period.getMonths());
            System.out.println(period.getDays());
        }
    }
    
    

Duration

  • 在Java8中,我们可以使用以下类来计算时间间隔差异:java.time.Duration

  • 提供了使用基于时间的值测量时间量的方法

  • 用于LocalDateTime之间的比较,也可以用于Instant之间的比较

    package com.java.jdk8time_update;
    
    
    import java.time.Duration;
    import java.time.LocalDateTime;
    
    public class DurationTest {
        public static void main(String[] args) {
            // 当前本地 年月日
            LocalDateTime today = LocalDateTime.now();
            System.out.println(today);
    
            // 生日的年月日
            LocalDateTime birthDate = LocalDateTime.of(2000, 12, 16, 8, 32, 57);
            System.out.println(birthDate);
    
            Duration duration = Duration.between(birthDate, today);  //参数二减去参数一
            System.out.println("隔了多少天" + duration.toDays());
            System.out.println("隔了多少时" + duration.toHours());
            System.out.println("隔了多少分" + duration.toMinutes());
            System.out.println("隔了多少秒" + duration.toMillis());
            System.out.println("隔了多少纳秒" + duration.toNanos());
    
        }
    }
    
    
  • Duration:用于计算两个“时间”间隔
  • Period:用于计算两个“日期”间隔

ChronoUnit

  • 包:java.time.temporal.ChronoUnit

  • ChronoUnit类可用于在单个时间单位内测量一段时间,这个工具类是最全的了,可以用于比较所有的时间单位

    package com.java.jdk8time_update;
    
    import java.time.LocalDateTime;
    import java.time.temporal.ChronoUnit;
    
    public class ChronoUnitTest {
        public static void main(String[] args) {
            // 本地日期时间对象
            LocalDateTime today = LocalDateTime.now();
            System.out.println(today);
    
            LocalDateTime birthDate = LocalDateTime.of(2000,12,16,20,32);
            System.out.println(birthDate);
    
            // ChronoUnit通过点调用它的对象(查看源码可知通过枚举实现),使用该对象调用between
            
            System.out.println("相差的年数:" + ChronoUnit.YEARS.between(birthDate,today));
            System.out.println("相差的月数:" + ChronoUnit.MONTHS.between(birthDate,today));
            System.out.println("相差的周数:" + ChronoUnit.WEEKS.between(birthDate, today));
            System.out.println("相差的天数:" + ChronoUnit.DAYS.between(birthDate,today));
            System.out.println("相差的时数:" + ChronoUnit.HOURS.between(birthDate,today));
            System.out.println("相差的分数:" + ChronoUnit.MINUTES.between(birthDate,today));
            System.out.println("相差的秒数:" + ChronoUnit.SECONDS.between(birthDate,today));
            System.out.println("相差的毫秒数:" + ChronoUnit.MILLIS.between(birthDate,today));
            System.out.println("相差的微秒数:" + ChronoUnit.MICROS.between(birthDate,today));
            System.out.println("相差的纳秒数:" + ChronoUnit.NANOS.between(birthDate,today));
            System.out.println("相差的半天数:" + ChronoUnit.HALF_DAYS.between(birthDate,today));
            System.out.println("相差的十年数:" + ChronoUnit.DECADES.between(birthDate,today));
            System.out.println("相差的世纪数:" + ChronoUnit.CENTURIES.between(birthDate,today));
            System.out.println("相差的千年数:" + ChronoUnit.MILLENNIA.between(birthDate,today));
            System.out.println("相差的纪元数:" + ChronoUnit.ERAS.between(birthDate,today));
    
        }
    }
    
    

相关文章

网友评论

    本文标题:29.JDK8开始新增日期API:Peroid、Duration

    本文链接:https://www.haomeiwen.com/subject/survkrtx.html