gpt4 book ai didi

java - Java 中是否有一天的长度的全局变量?

转载 作者:行者123 更新时间:2023-11-30 08:02:10 25 4
gpt4 key购买 nike

我不想在我的代码中编写 24 * 60 * 60 * 1000 而是使用更具可读性的静态 DAY 变量。

我知道我可以自己通过写来定义变量

private static final long DAY =  24*60*60*1000;

然而,这对我来说似乎是一个基本功能。它是否包含在标准库的某个地方?

最佳答案

你可以试试 Duration Java 8 中 java.time 包的 API java.time.Duration.ofDays(1).getSeconds()

只需将其存储在公共(public)静态字段中:
public static final long SECONDS_IN_DAY = Duration.ofDays(1).getSeconds()

您感兴趣的方法是 ofDays(long) method:

Obtains a Duration representing a number of standard 24 hour days. The seconds are calculated based on the standard definition of a day, where each day is 86400 seconds which implies a 24 hour day. The nanosecond in second field is set to zero.

Parameters: days - the number of days, positive or negative
Returns: a Duration, not null
Throws: ArithmeticException - if the input days exceeds the capacity of Duration

接着调用 getSeconds() method在相同的 Duration API 上:

Gets the number of seconds in this duration. The length of the duration is stored using two fields - seconds and nanoseconds. The nanoseconds part is a value from 0 to 999,999,999 that is an adjustment to the length in seconds. The total duration is defined by calling this method and getNano().

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.

Returns: the whole seconds part of the length of the duration, positive or negative

关于java - Java 中是否有一天的长度的全局变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37112973/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com