Java Date Time API Series 9----Difference between Period and Duration of the new Date Time API class in the java.time package in Jdk8

1.Period

final modifier, thread safe, date-based amount of time in ISO-8601 calendar system, such as 2 years, 3 months, 4 days.

Main attributes: number of years, months, days.

    /**
     * The number of years.
     */
    private final int years;
    /**
     * The number of months.
     */
    private final int months;
    /**
     * The number of days.
     */
    private final int days;

For amount of time, compare 2 dates.

For example:

     LocalDate localDate1 = LocalDate.of(2019, 11, 15);
        LocalDate localDate2 = LocalDate.of(2020, 1, 1);
        Period p = Period.between(localDate1, localDate2);
        System.out.println("years:"+p.getYears()+" months:"+p.getMonths()+" days:"+p.getDays());

Output:

years:0 months:1 days:17

2.Duration

final modification, thread-safe, time-based amount of time, such as "34.5 seconds".

Main properties: seconds, nanoseconds

    /**
     * The number of seconds in the duration.
     */
    private final long seconds;
    /**
     * The number of nanoseconds in the duration, expressed as a fraction of the
     * number of seconds. This is always positive, and never exceeds 999,999,999.
     */
    private final int nanos;

For amount of time, compare 2 times.

For example:

        LocalDateTime localDateTime1 = LocalDateTime.of(2019, 11, 15, 0, 0);
        LocalDateTime localDateTime2 = LocalDateTime.of(2019, 11, 15, 10, 30);
        Duration d = Duration.between(localDateTime1, localDateTime2);
        System.out.println("days:"+d.toDays());
        System.out.println("hours:"+d.toHours());
        System.out.println("minutes:"+d.toMinutes());
        System.out.println("millis:"+d.toMillis());

Output:

days:0
hours:10
minutes:630
millis:37800000

3. Differences between Period and Duration

(1) Different inclusion attributes

Period contains years, months, days, while Duration only contains seconds, nanoseconds.

Period can only return years, months, days; Duration can return days, hours, minutes, milliseconds, and so on.

(2) Different types of between methods can be used

Period can only use LocalDate, and Duration can use all classes that contain the time part and implement the Temporal interface, such as LocalDateTime, LocalTime, Instant, and so on.

Period:

public static Period between(LocalDate startDateInclusive, LocalDate endDateExclusive)

 

Duration:

public static Duration between(Temporal startInclusive, Temporal endExclusive)

 

(3) Differences in the number of days that between obtains

From the example above, you can see that:

Period. p.getDays() will only get the days attribute value when it gets the days, instead of counting the months and years into days. There will not be a case where the days are 365 days after 2200.1.1 and 2019.1.1 comparisons.

    public int getDays() {
        return days;
    }

 

When Duration d.toDays() gets days, the second attribute is converted to days.

    public long toDays() {
        return seconds / SECONDS_PER_DAY;
    }

 

Therefore, Duration is the only way to get the total difference between two days.

(4) Period has a method to get the total number of months, why is there no method to get the total number of days?

Period has a way to get the total number of months:

    public long toTotalMonths() {
        return years * 12L + months;  // no overflow
    }

Why is there no total days method?

Period obtained after between does not record leap year information between two dates. There are leap years. Days per year are not necessarily 365 days, so the calculation is not accurate.

Keywords: Java Attribute

Added by lordshoa on Fri, 27 Dec 2019 22:10:46 +0200