java - Java中有一个全局变量来表示一天的长度吗?

标签 java time

我不想在代码中编写 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/

相关文章:

java - 为什么在这个例子中我们不能使用图形对象绘制字符串

linux - 如何递归查找并列出具有子目录和时间的目录中的最新修改文件

java - 如何给最终变量赋值

java - 使用 Mongo Cursor 时更新文档的正确方法是什么?

java - 这个java接口(interface)策略合理吗?

go - 如何获得一天中的秒数

python - 如何利用时间在 pygame 中制作随机箭头?

php - 一旦我计算出要输出的两位数应该不同,那么php中的excel表

javascript - 未设置系统日期时如何获取客户端的时区?

java - 在 Websphere 上使用 Jasper-Reports 时出现 "Bad major version"错误