How to Work with Periods and Durations in the Java 8 Date/Time API

When working with date and time objects in Java 8 you will probably need to work with date periods and time durations. A date period is measured in days, months, and years, while a time duration is measured in hours, minutes, and seconds. A duration can be computed using the Duration class, and a period can be computed using the Period class. Both classes are located in the java.time package. To understand how to work with periods and durations in the Java 8 API, follow these four steps.

  1. Open your text editor and create the Java program that will create date and time objects for period and duration processing. Type in the following Java statements:
    import java.time.*;
    import java.time.format.*;
    import java.time.temporal.*;
    public class WorkWithPeriodsAndDurations {
    	public static void main (String args[]) {
    		// Create two dates for period processing:
    		LocalDate july4th2017 = LocalDate.of(2017, Month.JULY, 4);
    		LocalDate august10th2018 = LocalDate.of(2018, Month.AUGUST, 10);
    		DateTimeFormatter format=DateTimeFormatter.ofPattern("MM/dd/yy");
    		// Compute period:
    		Period period=Period.between(july4th2017, august10th2018);
    		System.out.format("Period between %s and %s is %d year, %d month, %d days \n" , july4th2017.format(format), august10th2018.format(format),
    			period.getYears(), period.getMonths(), period.getDays());
    		// Add one month to August 10, 2018:
    		LocalDate, ChronoUnit.MONTHS);
    		System.out.format("Adding one month to %s yields %s \n", august10th2018.format(format), august10th2018plusOneMonth.format(format));
    		// Subtract one month from August 10, 2018:
    		LocalDate, ChronoUnit.MONTHS);
    		System.out.format("Subtracting one month from %s yields %s \n", august10th2018.format(format), august10th2018minusOneMonth.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);	

    The program instantiates two LocalDate objects and then uses the period method of the Period class to compute the period between the two dates. When calling the period method, pass the earliest date first in the parameter list. Note that when displaying the period you can request the years, months, and days by calling the appropriate getter method. The program also instantiates two LocalTime objects and then uses the duration method of the Duration class to compute the duration between the two times. When calling the duration method, pass the earliest date first in the parameter list to return a positive time duration. The program also passes the later time first in order to display a negative time duration. Note that when the duration is displayed the output is prefixed with "PT" (period of time) followed by the formatted hour, minute, and second components of the duration.

  2. Save your file as
  3. Open a command prompt and navigate to the directory containing your new Java program. Then type in the command to compile the source and hit Enter.
    Compile Program with Periods and Durations
  4. You are ready to test your Java program. Type in the command to run the Java runtime launcher and hit Enter.
    Run Program with Periods and Durations
    Notice the output shows results of computing date periods and time durations. Note that a negative time period is generated when the beginning of a time duration is later than the end of the duration.
Author: Stephen Withrow

Stephen has over 30 years of experience in training, development, and consulting in a variety of technology areas including Python, Java, C, C++, XML, JavaScript, Tomcat, JBoss, Oracle, and DB2. His background includes design and implementation of business solutions on client/server, Web, and enterprise platforms. Stephen has a degree in Computer Science and Physics from Florida State University.

About Webucator

Webucator provides instructor-led training to students throughout the US and Canada. We have trained over 90,000 students from over 16,000 organizations on technologies such as Microsoft ASP.NET, Microsoft Office, Azure, Windows, Java, Adobe, Python, SQL, JavaScript, Angular and much more. Check out our complete course catalog.