Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting TimeUnit to ChronoUnit?

Tags:

java

java-time

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)

like image 458
Gili Avatar asked Oct 27 '14 05:10

Gili


2 Answers

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

like image 52
Chriss Avatar answered Oct 22 '22 02:10

Chriss


At one stage during the development, you could could construct a Duration from a TimeUnit. https://github.com/ThreeTen/threeten/blob/3b4c40e3e7a5dd7a4993ee19e1c156e4e65432b3/src/main/java/javax/time/Duration.java#L293 However this was removed for the final version of the code in Java SE 8.

I don't know of any pre-packaged routine to do the conversion, but it should be added to ThreeTen-Extra, probably in Temporals.

UPDATE: This was fixed by https://github.com/ThreeTen/threeten-extra/issues/22

like image 14
JodaStephen Avatar answered Oct 22 '22 01:10

JodaStephen