java8 - ChronoUnit enum class examples

Java8 java.time.ChronoUnit enum Class example

What is ChronoUnit in java?

java.time.temporal.ChronoUnit is an Enumeration class introduced in java8. It is used to measure the time in Years, Months, Weeks, Days, Hours, Minutes, Seconds, Microseconds, Nanoseconds. Before java8, developers used integer values for Days, Weeks, hours, etc. With the java8 version, These integer values represent with ChronoUnit enumeration constants. It implements the TemporalUnit interface.

It contains the following constants

  • Nanos
  • Micros
  • Millis
  • Seconds
  • Minutes
  • Hours
  • HalfDays
  • Days
  • Weeks
  • Months
  • Years
  • Decades
  • Centuries
  • Millennia
  • Eras
  • Forever

Some of the methods

  • values(): return the ChronoUnit array with enum constants

  • isDateBased(): It checks checked unit is based on the Date unit

  • isTimeBased() This check checked unit is based on the time unit

  • isSupportedBy() if the given unit is supported by the temporal unit

  • between() method calculate the time between two temporal objects, Time returned in long in measuring unit

  • isDurationEstimated() returns true or false based on duration estimated unit

  • getDuration() returns Duration object unit in ISO time units

It use in Date and Time API for calculating below things

  • a specific time before
  • Specific time after
  • Many days, years, months, hours, minutes, seconds between two dates and return range in Units.

ChronoUnit Examples

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

Display all ChronoUnits in seconds

It 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  
holidays 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?

It is an example of adding and subtracting measure units to LocaldateTime

LocalDateTime now = LocalDateTime.now();  
// date time 5 days ago  
LocalDateTime localDateTime1 = now.minus(5, ChronoUnit.DAYS);  
//  date time 5 years ago  
LocalDateTime localDateTime2 = now.minus(5, ChronoUnit.YEARS);  
// date time 10 days after  
LocalDateTime localDateTime3 = now.plus(5, ChronoUnit.DAYS);  
//  date time 4 months after  
LocalDateTime localDateTime4 = now.plus(4, ChronoUnit.MONTHS);  

The output of the above code execution is


How to calculate the number of years between two dates?

It is an example of finding years between two [LocalDate)(/java8-ten-localdate-examples) objects.

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);  

Output is


How to calculate the number of days between two dates?

It 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);  

Output is


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

It 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  


You learned Chrono unit enumeration object and its methods and examples for calculating time date difference between different temporal objects.

Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
You'll get a notification every time a post gets published here.