Period & Duration classes represent difference between 2 instances of temporal objects i.e. objects that represent date, time, zone etc.
- Period – Difference between two temporal objects in measured years plus months plus days ex: 1 year and 2 months and 3 days.
- Duration – Different between two temporal objects in seconds but can be interpreted in days, hours, minutes, seconds, millis. ex: 1 day or 24 hours etc.
Period Examples:
Period between LocalDate objects
1 2 3 4 5 6 7 8 9 |
LocalDate birthDate = LocalDate.of(1981, 1, 10); LocalDate today = LocalDate.now(); Period age = Period.between(birthDate, today); System.out.println( "Your age is " + age.getYears() + " years " + age.getMonths() + " months " + age.getDays() + " days."); |
1 |
Your age is 37 years 11 months 22 days. |
Period between LocalDateTime objects.
- Period is representative of difference in years, months & days. It can’t count time units.
- So Period does not directly provide method to calculate based on LocalDateTime.
- There is a way to get Period between LocalDateTime i.e. convert LocalDateTime to LocalDate so that there is no time involved & then Period can be calculated between LocalDate.
1 2 3 4 5 6 7 8 |
LocalDateTime dt1 = LocalDateTime.of(2018, Month.MARCH, 1, 0, 0, 0); LocalDateTime dt2 = LocalDateTime.of(2019, Month.JULY, 1, 0, 0, 0); Period periodBetLocalDateTime = Period.between(dt1.toLocalDate(), dt2.toLocalDate()); System.out.println("Period between LocalDateTime is " + periodBetLocalDateTime.getYears() + " years " + periodBetLocalDateTime.getMonths() + " months " + periodBetLocalDateTime.getDays() + " days."); |
1 |
Period between LocalDateTime is 1 years 4 months 0 days. |
Period between YearMonth
- Unlike earlier example where we had extra information of time, in this case we have missing information i.e. days. We only have Year & Month information.
- So again Period can’t logically be derived from YearMonth & Period class does not provide facility to do so.
- But again, you can convert it to LocalDate by taking “first day of month” as a filler day.
1 2 3 4 5 6 7 8 9 10 |
YearMonth start = YearMonth.of(2017, Month.JULY); YearMonth end = YearMonth.of(2019, Month.JANUARY); Period periodBetYearMonth = Period.between(LocalDate.of(start.getYear(), start.getMonth(), 1), LocalDate.of(end.getYear(), end.getMonth(), 1)); System.out.println("Period between YearMonth is " + periodBetYearMonth.getYears() + " years " + periodBetYearMonth.getMonths() + " months " + periodBetYearMonth.getDays() + " days."); |
1 |
Period between YearMonth is 1 years 6 months 0 days. |
Period between ZonedDateTime & OffsetDateTime
- In this case, we have additional information of time & zone which Period can’t use because it is representation in years, months & days.
- So here as well, we can convert it into LocalDate & calculate Period.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
LocalDateTime dt_1 = LocalDateTime.of(2018, Month.MARCH, 1, 0, 0, 0); ZonedDateTime zd1 = ZonedDateTime.of(dt_1, ZoneId.of("Asia/Kolkata")); LocalDateTime dt_2 = LocalDateTime.of(2019, Month.JULY, 1, 0, 0, 0); ZonedDateTime zd2 = ZonedDateTime.of(dt_2, ZoneId.of("Asia/Kolkata")); Period periodBetZonedDateTime = Period.between(zd1.toLocalDate(), zd2.toLocalDate()); System.out.println("Period between ZonedDateTime is " + periodBetZonedDateTime.getYears() + " years " + periodBetZonedDateTime.getMonths() + " months " + periodBetZonedDateTime.getDays() + " days."); OffsetDateTime od1 = OffsetDateTime.of(dt_1, ZoneOffset.of("-08:00")); OffsetDateTime od2 = OffsetDateTime.of(dt_2, ZoneOffset.of("-08:00")); Period periodBetOffsetDateTime = Period.between(od1.toLocalDate(), od2.toLocalDate()); System.out.println("Period between OffsetDateTime is " + periodBetOffsetDateTime.getYears() + " years " + periodBetOffsetDateTime.getMonths() + " months " + periodBetOffsetDateTime.getDays() + " days."); |
1 2 |
Period between ZonedDateTime is 1 years 4 months 0 days. Period between OffsetDateTime is 1 years 4 months 0 days. |
Period Between Instant
- instant is moment in time stored in milliseconds which is kind of machine time.
- To calculate period, we have to bring it to human readable date i.e. LocalDate.
1 2 3 4 5 6 7 8 9 10 |
Instant i1 = Instant.now(); Instant i2 = Instant.now().minusMillis(1000*60*60*24); Period periodBetInstant = Period.between( LocalDate.ofInstant(i2, ZoneId.of("America/Los_Angeles")), LocalDate.ofInstant(i1, ZoneId.of("America/Los_Angeles"))); System.out.println("Period between Instant is " + periodBetInstant.getYears() + " years " + periodBetInstant.getMonths() + " months " + periodBetInstant.getDays() + " days."); |
1 |
Period between Instant is 0 years 0 months 1 days. |
Duration Examples
Duration between LocalDateTime
1 2 3 4 5 6 7 8 |
LocalDateTime dttm1 = LocalDateTime.of(2018, Month.MARCH, 1, 0, 0, 0); LocalDateTime dttm2 = LocalDateTime.of(2019, Month.JULY, 1, 0, 0, 0); Duration durationBetLocalDateTime = Duration.between(dttm1, dttm2); System.out.println("Duration between LocalDateTime in days = " + durationBetLocalDateTime.toDays()); System.out.println("Duration between LocalDateTime in hours = " + durationBetLocalDateTime.toHours()); System.out.println("Duration between LocalDateTime in seconds = " + durationBetLocalDateTime.toSeconds()); |
1 2 3 |
Duration between LocalDateTime in days = 487 Duration between LocalDateTime in hours = 11688 Duration between LocalDateTime in seconds = 42076800 |
Duration between LocalTime
1 2 3 4 5 6 |
LocalTime officetStart = LocalTime.of(9, 00); LocalTime officeEnd = LocalTime.of(17, 00); Duration officeHours = Duration.between(officetStart, officeEnd); System.out.println("Office hours are " + officeHours.toHours()); |
1 |
Office hours are 8 |
Duration between ZonedDateTime and OffsetDateTime
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
LocalDateTime dt_1 = LocalDateTime.of(2018, Month.MARCH, 1, 0, 0, 0); ZonedDateTime zd1 = ZonedDateTime.of(dt_1, ZoneId.of("Asia/Kolkata")); LocalDateTime dt_2 = LocalDateTime.of(2019, Month.JULY, 1, 0, 0, 0); ZonedDateTime zd2 = ZonedDateTime.of(dt_2, ZoneId.of("Asia/Kolkata")); Duration durationBetZonedDateTime = Duration.between(zd1, zd2); System.out.println("Duration between ZonedDateTime in days = " + durationBetZonedDateTime.toDays()); System.out.println("Duration between ZonedDateTime in hours = " + durationBetZonedDateTime.toHours()); System.out.println("Duration between ZonedDateTime in seconds = " + durationBetZonedDateTime.toSeconds()); OffsetDateTime od1 = OffsetDateTime.of(dt_1, ZoneOffset.of("-08:00")); OffsetDateTime od2 = OffsetDateTime.of(dt_2, ZoneOffset.of("-08:00")); Duration durationBetOffsetDateTime = Duration.between(zd1, zd2); System.out.println("Duration between OffsetDateTime in days = " + durationBetOffsetDateTime.toDays()); System.out.println("Duration between OffsetDateTime in hours = " + durationBetOffsetDateTime.toHours()); System.out.println("Duration between OffsetDateTime in seconds = " + durationBetOffsetDateTime.toSeconds()); |
1 2 3 4 5 6 |
Duration between ZonedDateTime in days = 487 Duration between ZonedDateTime in hours = 11688 Duration between ZonedDateTime in seconds = 42076800 Duration between OffsetDateTime in days = 487 Duration between OffsetDateTime in hours = 11688 Duration between OffsetDateTime in seconds = 42076800 |
Duration between Instant
1 2 3 4 5 6 7 8 |
Instant i1 = Instant.now(); Instant i2 = Instant.now().minusMillis(1000*60*60*24); Duration durationBetInstant = Duration.between(i2, i1); System.out.println("Duration between Instant in days = " + durationBetInstant.toDays()); System.out.println("Duration between Instant in hours = " + durationBetInstant.toHours()); System.out.println("Duration between Instant in seconds = " + durationBetInstant.toSeconds()); |
1 2 3 |
Duration between Instant in days = 1 Duration between Instant in hours = 24 Duration between Instant in seconds = 86400 |
Duration between LocalDate
- Duration is calculated in seconds, but LocalDate only has day, month & year. It doesn’t have time information which makes it ineligible for Duration calculation.
- If you try to calculate duration between LocalDate objects directly you will get exception java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Seconds
- If you still want to calculate duration between LocalDate then you can convert it to LocalDateTime by taking filler “time” as start of day.
1 2 3 4 5 6 7 |
LocalDate birthDate = LocalDate.of(1981, 1, 10); LocalDate today = LocalDate.now(); Duration agetDuration = Duration.between( birthDate.atStartOfDay(), today.atStartOfDay()); System.out.println("Age in hours is " + agetDuration.toHours()); |
1 |
Age in hours is 332904 |