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...
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With