What is the maximum value for Java Duration

I tried to create max Duration in Java 8 using Duration.ofMillis (Long.MAX_VALUE) but got a long overflow. How would I programmatically get the equivalent of Duration.MAX_VALUE if it exists?

Edit: A long overflow was probably caused by an attempt to add to the value, and not during construction. Apologies for the lack of reproducible code.

+6
source share
2 answers

It looks like Duration is stored in seconds (up to Long.MAX_VALUE ) and nanoseconds (up to 999,999,999 ). Then the longest possible duration:

 Duration d = Duration.ofSeconds(Long.MAX_VALUE, 999_999_999); 

When I print it ( System.out.print(d) ), I get the following:

 PT2562047788015215H30M7.999999999S 

which means: 2562047788015215 hours, 30 minutes and 7.999999999 seconds.

+3
source

According to Javadoc :

Duration uses nanosecond resolution with a maximum value of seconds that can be held for a long time.

A duration range requires storing a number larger than a long one. To do this, the class stores long representing seconds and int representing a nanosecond second, which will always be between 0 and 999 999 999. The model has a directed duration, which means that the duration can be negative.

+5
source

All Articles