Routines between the Java period and the duration

I'm not sure I get the intricacies between Java Periodand Duration.

When I read the Oracle explanation , it says that I can find out how many days from that birthday are like this (using the date example, they b):

LocalDate today = LocalDate.now();
LocalDate birthday = LocalDate.of(1960, Month.JANUARY, 1);
Period birthdayPeriod = Period.between(birthday, today);
int daysOld = birthdayPeriod.getDays();

But, as they note, this does not take into account the time zone in which you were born and the time zone in which you are. But this is a computer, and we can be exact, right? So would I use Duration?

ZoneId bornIn = ZoneId.of("America/New_York");
ZonedDateTime born = ZonedDateTime.of(1960, Month.JANUARY.getValue(), 1, 2, 34, 56, 0, bornIn);
ZonedDateTime now = ZonedDateTime.now();
Duration duration = Duration.between(born, now);
long daysPassed = duration.toDays();

Now the actual points are accurate, but if I understand this correctly, the days may not correspond to calendar days, for example. with DST etc.

, , , ? , , LocalDate, ZonedDateTime, Duration.

ZoneId bornIn = ZoneId.of("America/New_York");
ZonedDateTime born = ZonedDateTime.of(1960, Month.JANUARY.getValue(), 1, 2, 34, 56, 0, bornIn);
ZonedDateTime now = ZonedDateTime.now();
ZonedDateTime nowNormalized=now.withZoneSameInstant(born.getZone());
Period preciseBirthdayPeriod = Period.between(born.toLocalDate(), nowNormalized.toLocalDate());
int preciseDaysOld = preciseBirthdayPeriod.getDays();

, .

+4
2

JavaDoc Period , :

ISO-8601, , , "2 , 3 4 ".

, .

, Interval ThreeTen-Extra, :

.

- , "[...] Java 8, ".

Duration , toDuration() .

, :

ZoneId bornIn = ZoneId.of("America/New_York");
ZonedDateTime born = ZonedDateTime.of(1960, Month.JANUARY.getValue(), 1, 2, 34, 56, 0, bornIn);
ZonedDateTime now = ZonedDateTime.now();
Interval interval = Interval.of(born.toInstant(), now.toInstant());
long daysPassed = interval.toDuration().toDays();
+3

Java-8 Period Duration - .

  • java.time.Period .
  • java.time.Duration ( ) , , 24 = 86400 .

, , , , - . , Period ( , getDays() - . ).

. , () Duration, .

Period, ( Period) . ( ):

    Period preciseBirthdayPeriod =
        Period.between(born.toLocalDate(), nowNormalized.toLocalDate());
    int preciseDaysOld = preciseBirthdayPeriod.getDays();
    System.out.println(preciseDaysOld); // 13
    System.out.println(preciseBirthdayPeriod); // P56Y11M13D

, preciseBirthdayPeriod.getDays(), . , . 11 56 . , , , (. , "3 , 2 4 " ).

, , , ( : , - ). Java-8-time-library: Period AND Duration. Threeten-Extra-class Interval , long daysPassed = interval.toDuration().toDays(); - (1 == 24 ), , ..

:

Period -. @swiedsw Duration. . , TemporalAmount ( ).

:

Time4J, , . :

Timezone bornZone = Timezone.of(AMERICA.NEW_YORK);
Moment bornTime =
    PlainTimestamp.of(1960, net.time4j.Month.JANUARY.getValue(), 1, 22, 34, 56).in(
        bornZone
    );
Moment currentTime = Moment.nowInSystemTime();
MomentInterval interval = MomentInterval.between(bornTime, currentTime);

MachineTime<TimeUnit> mt = interval.getSimpleDuration();
System.out.println(mt); // 1797324427.356000000s [POSIX]

net.time4j.Duration<?> duration =
    interval.getNominalDuration(
        bornZone, // relevant if the moments are crossing a DST-boundary
        CalendarUnit.YEARS,
        CalendarUnit.MONTHS,
        CalendarUnit.DAYS,
        ClockUnit.HOURS,
        ClockUnit.MINUTES
    );

// P56Y11M12DT12H52M (12 days if the birth-time-of-day is after current clock time)
// If only days were specified above then the output would be: P20801D
System.out.println(duration);
System.out.println(duration.getPartialAmount(CalendarUnit.DAYS)); // 12

, , , , .., . ( ) - ( SI-, Time4J 1972 ).

+3

Source: https://habr.com/ru/post/1663631/