Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is adding weeks to java.time.Instant not supported?

The following piece of code:

Instant inFourWeeks = Instant.now().plus(4L, ChronoUnit.WEEKS);

Throws an exception:

java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Weeks

Why are weeks unsupported? I understand why months and years are not supported, because their duration in smaller units may vary. But a week has constant duration (7 days) and I can achieve the same by writing:

Instant inFourWeeks = Instant.now().plus(4L * 7L, ChronoUnit.DAYS);
like image 431
Snackoverflow Avatar asked Feb 24 '19 11:02

Snackoverflow


People also ask

What is Java instant time?

In Java language, the Instant Class is used to represent the specific time instant on the current timeline. The Instant Class extends the Object Class and implements the Comparable interface.

How do you add instant days?

Instant plus() method in Java An immutable copy of a instant where a time unit is added to it can be obtained using the plus() method in the Instant class in Java. This method requires two parameters i.e. time to be added to the instant and the unit in which it is to be added.


2 Answers

It throws UnsupportedTemporalTypeException 7 days week is not universal and constant. It may vary of different calendar system. For example, look on the Akan calendar system which uses 6 days a week.

like image 153
Amit Bera Avatar answered Nov 15 '22 15:11

Amit Bera


The Instant class deals with absolute time, and tries to avoid all ambiguities related to how different calendar systems, regions and cultures group and interpret it.

Some calendar systems have different week lengths, some have different month groupings, have years that start on a different date, and adjust for leap years and leap seconds in different ways (if at all, like in the case of the Julian calendar which had too many leap years and drifted from the 'physical' phenomena they were supposed to be in sync with, like the seasons, solstices and equinox).

To avoid these problems the Instant class allows you to use the more precisely defined and standardised units like the seconds, minutes, hours and days.

Leap seconds are 'smoothened' out in Java, over the last 1000 seconds of the day on which they occur, so from a programmer's perspective they do not exist. (Computer clocks are not that accurate anyway, and need to sync frequently with NTP.)

1 day is assumed to be 24 SI hours, with 1 SI hour defined as 60 SI minutes, 1 SI minute defined as 60 SI seconds, and 1 SI second being 9,192,631,770 radiation periods of Caesium-133. 24hrs is actually the mean Solar Day (time elapsed between two successive 'noons'), because due to elliptical orbits, the orbit of the sun itself, and variations in orbit speed, each solar day could be slightly longer or shorter.

One important thing you have to be careful of is Daylight Saving. In those special days, a day is 25 hours or 23 hours, depending on which direction the clock moves. However, the Instant class doesn't care about this, it will still move 24 hours if you add 1 day over the daylight savings boundary. It doesn't carry any timezone or regional information (DST is country specific).

like image 26
jbx Avatar answered Nov 15 '22 17:11

jbx