我不想在代码中编写 24 * 60 * 60 * 1000
,而是使用更具可读性的静态 DAY
变量。
我知道我可以自己通过编写来定义变量
private static final long DAY = 24*60*60*1000;
然而,在我看来,这似乎是一个基本功能。标准库中是否包含它?
最佳答案
您可以尝试Duration API java.time
Java 8 中的包 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/