Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: why TimeUnit have missing enums?

I just wonder, why some enums are missing on the TimeUnit class of Java?

Actually the max timeunit is DAY while i would like to use stuff like WEEK, YEAR...

like image 705
Sebastien Lorber Avatar asked Jun 07 '12 16:06

Sebastien Lorber


2 Answers

The TimeUnit.DAYS constant represents an interval of 24 hours, not a calendar day (midnight to midnight). Anything beyond a day (a week, a month, a year) is a calendar unit, not a time unit. The duration of calendar units depends on the calendar in use: for example, a week can be longer or shorter by an hour on weeks when daylight savings time goes in and out of effect, leap years are longer by a day, and so on. That is why including calendar units into TimeUnit enumeration would not make sense.

like image 57
Sergey Kalinichenko Avatar answered Nov 02 '22 17:11

Sergey Kalinichenko


In Java 8 ChronoUnit was introduced, you have in one enum all the usual time units between NANOS (nanoseconds) and CENTURIES, MILLENIA, and even FOREVER: https://docs.oracle.com/javase/8/docs/api/java/time/temporal/ChronoUnit.html (cf. also Julien Kronegg's comment to the chosen answer)

like image 26
Gismo Ranas Avatar answered Nov 02 '22 19:11

Gismo Ranas