Java8 - Java.time.Period类示例

258 阅读2分钟

在本教程中,您将了解到java.time.Period的例子以及如何在java 8中使用该类的例子。

什么是java中的周期?

在java8中使用DateTime API时,需要找出两个日期之间的时间量。

period 是以years,months,days, 和time 的持续时间来衡量的。它是一个基于值的类。所以,Period 的两个对象的平等检查可能会产生不可预知的结果。

以下是一个签名

public final class Period extends Object implements ChronoPeriod,   
Serializable 

Java.time.Period和Java.time.Duration之间的区别是什么?

period 是基于Date 的时间测量,并以Years,MonthsDays 来测量。而Duration 是基于Time 的测量,以hours,minutes,secondsnanoseconds 来测量。

Period``Duration 更适用于基于机器的纳秒计算的时间测量。寻找一个人的年龄是 的一个例子。
Period

有很多关于Period的使用例子。

如何创建一个Period对象?

period 对象可以通过以下创建方法来创建。

of(),ofMonths(),ofDays(),ofYears() 方法用来创建一个period 的实例。

import java.time.Instant;
import java.time.Period;

public class PeriodObjectCreateExample {
    public static void main(String[] args) {
        Period ofYears = Period.ofYears(5);
        System.out.println(ofYears.getYears());
        Period ofMonths = Period.ofMonths(11);
        System.out.println(ofMonths.getMonths());
        Period ofDays = Period.ofDays(20);
        System.out.println(ofDays.getDays());
        Period ofWeeks = Period.ofWeeks(6);
        System.out.println(ofWeeks.getDays());
        Period periodInYearMonthsTDays = Period.of(5, 11, 25);
        System.out.println("Total Period: " + periodInYearMonthsTDays);
    }
}

输出

5  
11  
20  
42  
Total Period: P5Y11M25D  

minus() minusDays() minusMonths() minusYears() 是对当前日期减去特定单位。
plus() plusDays() plusMonths() plusYears() 是对当前日期增加特定单位。

import java.time.LocalDate;
import java.time.Period;

public class AddSubtractPeriodExample {
    public static void main(String[] args) {
        LocalDate localDate = LocalDate.now();
        System.out.println(localDate);
        LocalDate add5Days = localDate.plus(Period.ofDays(5));
        System.out.println("Add 5 days"+add5Days);
        // date before one week
        LocalDate oneweekbefore = localDate.minus(Period.ofWeeks(1));
        System.out.println("subtract one week "+oneweekbefore);
        // Number of dates between two dates
        Period period = Period.between(oneweekbefore, add5Days);
        System.out.println("difference in days"+period.getDays());
        System.out.println("difference in months "+period.getMonths());
    }
}

输出

2018-08-28  
Add 5 days2018-09-02  
subtract one week 2018-08-21  
difference in days12  
difference in months 0  

如何使用Period类来计算年/月/日的年龄?

period 类提供了以下各种方法

  • getYears(): 返回这个时期的年数
  • getMonths(): 返回这个时期的月数
  • getDays(): 返回该时期的天数
import java.time.LocalDate;
import java.time.Month;
import java.time.Period;


public class CalculateAge {
    public static void main(String[] args) {
        LocalDate currentDate = LocalDate.now();
        LocalDate birthday = LocalDate.of(1995, Month.JANUARY, 1);
        Period period = Period.between(birthday, currentDate);
        System.out.println("Age is " + period.getYears() + " years, " + period.getMonths() +
                " months, and " +period.getDays() +
                " days old");

    }
}

输出

Age is 23 years, 7 months, and 27 days old  

结论

最后,在本教程中,你学到了java.time.Duration类在java8中的应用以及使用该类的多个实例