Java8 API 时间类Period

Period表示的是一个时间段,也就是一个持续时间,比如5秒,5年3个月20天,精确到年月日,有一个和Peroid类似的类是Duration,Duration表示的也是一个时间段,比Period时间更短,比如5秒、100纳秒。
这里我们重点介绍Peroid,我们把Peroid的API方法的使用都列举了一遍。

import java.time.LocalDate;
import java.time.Period;
import java.time.temporal.ChronoUnit;
import java.time.temporal.Temporal;

public class PeriodTest {

    public static void main(String[] args) {
        LocalDate now = LocalDate.now();
        LocalDate now2 = now.plusDays(15);

        //addTo
        Period period = Period.ofDays(10);
        System.out.println(period+" addTo "+ now +" = "+period.addTo(now));
        System.out.println("-----------------------------------------");

        //between
        Period between = Period.between(now, now2);
        System.out.println("between:"+between);
        System.out.println("-----------------------------------------");

        //equals
        boolean equals = period.equals(between);
        System.out.println("equals:"+equals);
        Period period2 = Period.ofDays(10);
        boolean equals2 = period.equals(period2);
        System.out.println("equals比较的是值:"+equals2);
        boolean equals3 = period == period2;
        System.out.println("==比较的是地址:"+equals3);
        System.out.println("-----------------------------------------");

        //from
        Period from = Period.from(period2);
        System.out.println("from:"+from);
        System.out.println("-----------------------------------------");

        //get
        long day = period.get(ChronoUnit.DAYS);
        long month = period.get(ChronoUnit.MONTHS);
        System.out.println("get day :"+day);
        System.out.println("get month :"+month);
        /*没有对应的时间单位会报错,例如:long l = period.get(ChronoUnit.HOURS);
        Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Hours
        at java.time.Period.get(Period.java:440)
        at com.my.study.date.PeriodTest.main(PeriodTest.java:37)*/

        System.out.println("getDays:"+period.getDays());
        System.out.println("getMonths:"+period.getMonths());
        System.out.println("getYears:"+period.getYears());
        System.out.println("getChronology:"+period.getChronology());
        System.out.println("-----------------------------------------");

        //normalized:将年和月进行规范化,不对日进行规范化
        //例如:3年15个月10天,方法处理后就是:4年3个月
        Period periodNormalized = period.withMonths(15).withYears(3);
        Period normalized = periodNormalized.normalized();
        System.out.println("periodNormalized:"+periodNormalized);
        System.out.println("normalized:"+normalized);
        System.out.println("-----------------------------------------");

        //减
        Period minus = period.minus(period);
        System.out.println("minus:"+minus);
        System.out.println("minusDays 减1天:"+period.minusDays(1));
        System.out.println("-----------------------------------------");

        //加
        Period plus = period.plus(period);
        System.out.println("minus:"+plus);
        System.out.println("minusDays 加1天:"+period.plusDays(1));
        System.out.println("-----------------------------------------");

        //乘
        Period multipliedBy = period.multipliedBy(10);
        System.out.println("multipliedBy:"+multipliedBy);
        System.out.println("-----------------------------------------");

        Period of = Period.of(10, 15, 20);
        System.out.println("of:"+of);
        System.out.println("-----------------------------------------");

        LocalDate now1 = LocalDate.now();
        Temporal temporal = period.subtractFrom(now1);
        System.out.println("subtractFrom:"+temporal);
        System.out.println("-----------------------------------------");

        Period with = period.withYears(2).withMonths(5).withDays(20);
        System.out.println("period调整前:"+period);
        System.out.println("with:"+with);
        System.out.println("-----------------------------------------");
    }
}

输出:

P10D addTo 2021-07-13 = 2021-07-23
-----------------------------------------
between:P15D
-----------------------------------------
equals:false
equals比较的是值:true
==比较的是地址:false
-----------------------------------------
from:P10D
-----------------------------------------
get day :10
get month :0
getDays:10
getMonths:0
getYears:0
getChronology:ISO
-----------------------------------------
periodNormalized:P3Y15M10D
normalized:P4Y3M10D
-----------------------------------------
minus:P0D
minusDays 减1天:P9D
-----------------------------------------
minus:P20D
minusDays 加1天:P11D
-----------------------------------------
multipliedBy:P100D
-----------------------------------------
of:P10Y15M20D
-----------------------------------------
subtractFrom:2021-07-03
-----------------------------------------
period调整前:P10D
with:P2Y5M20D
-----------------------------------------

Process finished with exit code 0