Java 8  Period  Duration  Java  
   Technology    Programming

Duration and Period in Java

 MAY 2, 2019   by iamabhishek

Duration class and Period class are two new classes introduced in Java 8 for allowing developers to use the date in various different ways. These classes can be used to represent a given amount of time or maybe difference between dates.

Duration and Period class in Java 8

The Period class basically represent a period which can be between two dates or independent of dates for example a period of 2 years 3 months and 4 days.

The Duration class is time based as we deal with hours, minutes and seconds. Hence, we can say that if your requirement is to store some value which is in hours or minutes then use the Duration class, else if your requirement is to have value in days, months or even years which can be difference between two dates then go for the Period class.

Duration Class

This class as the name suggests is used to store duration of time generally in seconds or nanoseconds and is used for representing shorter timespans.

We can instantiate a Duration class object in mnay different ways, the easiest is using the methods ofDays(), ofHours(), ofMillis(), ofMinutes(), ofNanos() and ofSeconds(). Let's see a few examples,

// duration object for a day i.e. 24 hours

Duration dd = Duration.ofDays(1);

// duration object for 1 hour

Duration dh = Duration.ofHours(1);

Duration dm = Duration.ofMinutes(60);

So there are many different ways of initialising it. We can even parse a text sequence providing a time duration to create a Duration class object.

// for 24 hours or 1 day

Duration usingChar = Duration.parse("1D");

The easiest of all and also most used method is between() method where in we create a duration class to represent the duration between two instants of time.

// using between() method

Instant first = Instant.parse("2019-04-03T10:15:30.00Z")

Instant second= Instant.parse("2019-04-03T10:20:30.00Z")

Duration duration = Duration.between(first, second);

The Duration class also provides us with many useful methods to access the time or to convert the time into various different formats like:

  • getSeconds() can be used to get the duration in seconds.

  • getNano() to get duration in nano seconds.

  • Then there is toNanos(), toMillis(), toMinutes(), toHours() etc to convert the time in different formats.

  • There are other funtions to add or subtract time to any existing Duration class object like plusNanos(), plusMillis(), plusMinutes(), plusHours() etc and minusNanos(), minusMillis(), minusMinutes(), minusHours() etc.

Period Class

Just like the Duration class the Period class can also be initialised using the method between() to represent the period between any two dates. Period class is used when we need to store longer period of time like year, months or days. Let's take an example,

// start and end date 10th April and 15th April

LocalDate start = LocalDate.of(2019, 4, 10);

LocalDate end = LocalDate.of(2019, 4, 15);

Period period = Period.between(start, end);

We can also create a Period class object directly by providing the number of days, months, weeks or year etc using the functions of(), ofDays(), ofMonths(), ofYears() and ofWeeks() etc.

Period days = Period.ofDays(30);

Period months = Period.ofMonths(15);

Period weeks = Period.ofWeeks(5);

We can also initialise a Period class object using a character sequence like following:

Period charYears = Period.parse("1Y");

Also, just like the Duration class, the Period class also provides several methods to access the value stored in it in various different formats like getYears(), getDays(), getMonths() etc.


So now we know 2 new ways of playing around with time and date in Java and also ew just covered two new classes introduced in Java 8.



Made with by Abhishek Ahlawat

© 2019 Studytonight.   All rights reserved. Protection Status