Period Class In Java 8

Period and Duration are two other important classes introduced in java 8 under java.time package.  As Period uses date based values (years, months, days) while a Duration uses seconds or nanoseconds to define an amount of time. A Period is the amount of time between two LocalDate. It is same concept like Duration which is quantity or amount of time between two Instant. And this have same kind of method as Duration but work with LocalDate instead on Instant. Like Durations, Periods can contain negative values if the end point occurs before the starting point.

1) Period Class Example:

If I say when Sardar Patel was born? I can create Period like below.

I am creating a Period between his date of birth and now, until is method which is used to create Period of time until now. And If I want to number of time passed in years I need to call getYears() method.

I can also call until method with special unit ChronoUnit.DAYS then it will return me long which is number of days since Sardar Patel was born.

Also check our example post related to creating Period instance different ways in Java 8.

Leave a Reply

Your email address will not be published. Required fields are marked *