Monday, August 27, 2018

Java8 - java.time.temporal.ChronoUnit enum tutorials with examples

ChronoUnit Examples

java.time.temporal.ChronoUnit is an Enumeration class introduced in java8. This is replaced with integer values representing Days, weeks.. etc. It implements TemporalUnit interface.
It is used to measure the time in Years, Months, Weeks, Days, Hours, Minutes, Seconds, Microseconds, Nanoseconds
It contains the following constants
Nanos Micros Millis Seconds Minutes Hours HalfDays Days Weeks Months
Years Decades Centuries Millennia Eras Forever

Usecases and Usage

This can be used in Date and Time API for calculating below things
specific time before
Specific time after
A number of days, years, months, hours, minutes, seconds between two dates and return range in Units.

Examples

We will see various use cases of java.time.temporal.ChronoUnit class

Display all ChronoUnits  in seconds

This is an example of available ChronoUnit and their measure unit in seconds
 for (ChronoUnit unit : ChronoUnit.values()) {
         Duration duration=unit.getDuration();
            System.out.println(unit+" is of seconds:"+duration.getSeconds());
        }
Output is
Nanos is of seconds:0
Micros is of seconds:0
Millis is of seconds:0
Seconds is of seconds:1
Minutes is of seconds:60
Hours is of seconds:3600
HalfDays is of seconds:43200
Days is of seconds:86400
Weeks is of seconds:604800
Months is of seconds:2629746
Years is of seconds:31556952
Decades is of seconds:315569520
Centuries is of seconds:3155695200
Millennia is of seconds:31556952000
Eras is of seconds:31556952000000000
Forever is of seconds:9223372036854775807

How to add/subtract to date using ChronoUnit in java?

This is an example adding and subtracting measure units to LocaldateTime
LocalDateTime now = LocalDateTime.now();
// date time 5 days ago
LocalDateTime localDateTime1 = now.minus(5, ChronoUnit.DAYS);
System.out.println(localDateTime1);
//  date time 5 years ago
LocalDateTime localDateTime2 = now.minus(5, ChronoUnit.YEARS);
System.out.println(localDateTime2);
// date time 10 days after
LocalDateTime localDateTime3 = now.plus(5, ChronoUnit.DAYS);
System.out.println(localDateTime3);
//  date time 4 months after
LocalDateTime localDateTime4 = now.plus(4, ChronoUnit.MONTHS);
System.out.println(localDateTime4);
The output of the above code execution is
2018-08-22T23:00:36.011362100
2013-08-27T23:00:36.011362100
2018-09-01T23:00:36.011362100
2018-12-27T23:00:36.011362100

How to calculate a number of years between two dates?

This is an example for finding years between two Localdates.
using ChronoUnit.YEARS.between() method returns the number of years in between dates.
LocalDate dob = LocalDate.of(1980, Month.AUGUST, 24);
LocalDate dateNow = LocalDate.now();
long numerOfYears = ChronoUnit.YEARS.between(dob, dateNow);
System.out.println(numerOfYears);
Output is
38
How to calculate the number of days between two dates?
This is an example of finding days between two Localdates.
ChronoUnit.DAYS.between() method returns the number of days in between dates.
LocalDate dob = LocalDate.of(1980, Month.AUGUST, 24);
LocalDate dateNow = LocalDate.now();
long numberOfDays = ChronoUnit.DAYS.between(dob, dateNow);
System.out.println(numberOfDays);
Output is
13882

How to get Nano/milli/seconds/minutes/Hours difference between two dates?

This is an example of finding micro time details between two dates.
ChronoUnit.between() method  with micro details gives all micro time information
LocalDateTime dateTime = LocalDateTime.of(2017, 1, 1, 0, 0);
LocalDateTime dateTime1 = LocalDateTime.now();
System.out.println("Differnce in Nano seconds" + ChronoUnit.NANOS.between(dateTime, dateTime1));
System.out.println("Differnce in Milli seconds" + ChronoUnit.MILLIS.between(dateTime, dateTime1));
System.out.println("Differnce in  seconds" + ChronoUnit.SECONDS.between(dateTime, dateTime1));
System.out.println("Differnce in  minutes" + ChronoUnit.MINUTES.between(dateTime, dateTime1));
System.out.println("Differnce in  hours" + ChronoUnit.HOURS.between(dateTime, dateTime1));
Output is

Differnce in Nano seconds52184462355462300
Differnce in Milli seconds52184462355
Differnce in  seconds52184462
Differnce in  minutes869741
Differnce in  hours14495

The same example can be written using java.time.Duration
java.time.Duration example to get hours/mins/secs/milli/nano between two dates.
java.time.Duration.between() method is used to get micro time details
LocalDateTime dateTime = LocalDateTime.of(2017, 1, 1, 0, 0);
LocalDateTime dateTime1 = LocalDateTime.now();
System.out.println("Differnce in Nano seconds" + java.time.Duration.between(dateTime, dateTime1).getNano());
System.out.println("Differnce in Milli seconds" + java.time.Duration.between(dateTime, dateTime1).toMillis());
System.out.println("Differnce in  seconds" + java.time.Duration.between(dateTime, dateTime1).getSeconds());
System.out.println("Differnce in  minutes" + java.time.Duration.between(dateTime, dateTime1).toMinutes());
System.out.println("Differnce in  hours" + java.time.Duration.between(dateTime, dateTime1).toHours());
Output is
Differnce in Nano seconds156614800
Differnce in Milli seconds52183108156
Differnce in  seconds52183108
Differnce in  minutes869718
Differnce in  hours14495

Related article


EmoticonEmoticon