Java 持续时间的最大值是多少
What is the Maximum value for Java Duration
我尝试使用 Duration.ofMillis(Long.MAX_VALUE) 在 Java 8 中创建最大持续时间,但出现了长时间溢出。
如果 Duration.MAX_VALUE 存在,我将如何以编程方式获得它的等价物?
编辑:长溢出很可能是由于尝试添加到值而不是在构造期间引起的。抱歉没有可重现的代码。
根据 Javadoc:
The duration uses nanosecond resolution with a maximum value of the seconds that can be held in a long.
The range of a duration requires the storage of a number larger than a long. To achieve this, the class stores a long representing seconds and an int representing nanosecond-of-second, which will always be between 0 and 999,999,999. The model is of a directed duration, meaning that the duration may be negative.
看起来 Duration
以秒(最多 Long.MAX_VALUE
)和纳秒(最多 999,999,999
)存储。那么可能的最大持续时间是:
Duration d = Duration.ofSeconds(Long.MAX_VALUE, 999_999_999);
当我打印它时 (System.out.print(d)
) 我得到以下信息:
PT2562047788015215H30M7.999999999S
这意味着:2562047788015215 小时 30 分钟 7.999999999 秒。
简单:
Duration maxDur = ChronoUnit.FOREVER.getDuration();
我尝试使用 Duration.ofMillis(Long.MAX_VALUE) 在 Java 8 中创建最大持续时间,但出现了长时间溢出。 如果 Duration.MAX_VALUE 存在,我将如何以编程方式获得它的等价物?
编辑:长溢出很可能是由于尝试添加到值而不是在构造期间引起的。抱歉没有可重现的代码。
根据 Javadoc:
The duration uses nanosecond resolution with a maximum value of the seconds that can be held in a long.
The range of a duration requires the storage of a number larger than a long. To achieve this, the class stores a long representing seconds and an int representing nanosecond-of-second, which will always be between 0 and 999,999,999. The model is of a directed duration, meaning that the duration may be negative.
看起来 Duration
以秒(最多 Long.MAX_VALUE
)和纳秒(最多 999,999,999
)存储。那么可能的最大持续时间是:
Duration d = Duration.ofSeconds(Long.MAX_VALUE, 999_999_999);
当我打印它时 (System.out.print(d)
) 我得到以下信息:
PT2562047788015215H30M7.999999999S
这意味着:2562047788015215 小时 30 分钟 7.999999999 秒。
简单:
Duration maxDur = ChronoUnit.FOREVER.getDuration();