Java: Duration and Period Calculation

Almost every developer handle Duration and Period issues while need to do any calculation based on Date and Time. In Java 8 introduces new classes to handle Duration and period.

  • Period : A date period is measured in days, months, and years. Period can be calculated by java.time.Period class.
  • Duration: A time duration is measured in hours, minutes, and seconds. Duration can be calculated by java.time.Duration class.

Period period= Period.between(localDate1, localDate2);

Duration duration=Duration.between(localTime1, localTime2);

Duration.toString() method return duration as P1Y10M6D. Here duration is 1 year, 10 month and 6 days.

Period.toString() method return period as PT3H10M40S. Here duration is 3 hour, 10 minute and 40 seconds.

Example

Below is example to calculate Period and Duration  between two dates and times.

package com.date;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Month;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

public class Java8DurationAndPeriodExample {

public static void main(String[] args) {

LocalDate june9th2016 = LocalDate.of(2016, Month.JUNE, 9);
LocalDate April15th2017 = LocalDate.of(2017, Month.APRIL, 15);
DateTimeFormatter format=DateTimeFormatter.ofPattern("MM/dd/yy");
// Compute period:
Period period=Period.between(April15th2017, june9th2016);
System.out.format("Period between %s and %s is %d year, %d month, %d days n" , April15th2017.format(format), june9th2016.format(format), period.getYears(), period.getMonths(), period.getDays());

// Add one month to August 10, 2018:
LocalDate April15th2017plusOneMonth=April15th2017.plus(1, ChronoUnit.MONTHS);
System.out.format("Adding one month to %s yields %s n", April15th2017.format(format), April15th2017plusOneMonth.format(format));

// Subtract one month from August 10, 2018:
LocalDate April15th2017minusOneMonth=April15th2017.plus(-1, ChronoUnit.MONTHS);
System.out.format("Subtracting one month from %s yields %s n", April15th2017.format(format), April15th2017minusOneMonth.format(format));

// Create two times for duration processing:
LocalTime twelveFifteenPM = LocalTime.of(12, 15, 9);
LocalTime twoTwentyFiveAM = LocalTime.of(2, 25, 0);
DateTimeFormatter timeFormat=DateTimeFormatter.ofPattern("HH:mm:ss a");
Duration duration=Duration.between(twoTwentyFiveAM, twelveFifteenPM);

System.out.format("Duration between %s and %s is %s n", twoTwentyFiveAM.format(timeFormat), twelveFifteenPM.format(timeFormat), duration);
duration=Duration.between(twelveFifteenPM, twoTwentyFiveAM);
System.out.format("Duration between %s and %s is %s n", twelveFifteenPM.format(timeFormat), twoTwentyFiveAM.format(timeFormat), duration);
	<span 				data-mce-type="bookmark" 				id="mce_SELREST_start" 				data-mce-style="overflow:hidden;line-height:0" 				style="overflow:hidden;line-height:0" 			></span>}
}

Output


Period between 04/15/17 and 06/09/16 is 0 year, -10 month, -6 days 
Adding one month to 04/15/17 yields 05/15/17 
Subtracting one month from 04/15/17 yields 03/15/17 
Duration between 02:25:00 AM and 12:15:09 PM is PT9H50M9S 
Duration between 12:15:09 PM and 02:25:00 AM is PT-9H-50M-9S