Converting TimeUnit to ChronoUnit?

Gili picture Gili · Oct 27, 2014 · Viewed 7.8k times · Source

Java 8 introduced ChronoUnit which is largely equivalent to TimeUnit introduced in Java 5.

Is there an existing function for converting a TimeUnit to ChronoUnit? (Yes, I know how to write my own)

Answer

Chriss picture Chriss · Mar 2, 2016

Java 9

In Java 9 the TimeUnit API got extended and allows to convert between TimeUnit and ChronoUnit:

TimeUnit.toChronoUnit() // returns a ChronoUnit
TimeUnit.of(ChronoUnit chronoUnit) // returns a TimeUnit

see: JDK-8141452