Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Maximum value for Java Duration

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

Edit: The long overflow was likely caused by an attempt to add to the value instead of during construction. Apologies for not having reproducible code.

like image 797
Novaterata Avatar asked Jul 06 '16 15:07

Novaterata


People also ask

What is the max value of long in Java?

long: The long data type is a 64-bit signed two's complement integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive). Use this data type when you need a range of values wider than those provided by int.

What is the duration of Java?

A Duration represents a directed distance between two points on the time-line. A negative duration is expressed by the negative sign of the seconds part. A duration of -1 nanosecond is stored as -1 seconds plus 999,999,999 nanoseconds.

What is PT duration Java?

PT means 'Period of Time'.

How do you set a long max value in Java?

int v = 123543; int calc = -9876345; long: long is a signed 64-bit type and is useful for those occasions where an int type is not large enough to hold the desired value. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive).


2 Answers

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

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.

like image 91
Alexey Avatar answered Sep 18 '22 13:09

Alexey


Simple:

Duration maxDur = ChronoUnit.FOREVER.getDuration();
like image 34
Nick J. R. T. Avatar answered Sep 19 '22 13:09

Nick J. R. T.