圆日期长值
Round date long value
在我的 Java 项目中,我在 long
中使用了日期,例如它是 12136219
并通过如下创建 Date
对象:
long time = 12136219;
Date date = new Date(time);
表示日期为 Thu Jan 01 04:22:16 CET 1970
。我如何将日期(以长表示形式)舍入到分钟?
例如,如果秒数为 <30
,我想实现 Thu Jan 01 04:22:00 CET 1970
;如果秒数为 >=30
,我想实现 Thu Jan 01 04:23:00 CET 1970
,但我想绕过这个 long time = 12136219
表示.有什么想法吗?
当您从 long
创建 Date
时,long 表示自 1970 年 1 月 1 日以来的毫秒数。一分钟有 60*1000 毫秒。这应该足以形成您需要的舍入算法。
使用 Calendar.set
重置秒和毫秒
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
cal.add(Calendar.MINUTES, calendar.get(Calendar.SECOND) >= 30 ? 1 : 0)
currentDate = cal.getTimeInMillis();
Sets the given calendar field to the given value. The value is not interpreted by this method regardless of the leniency mode.
由于时间
"milliseconds since the standard base time known as "纪元”,即 1970 年 1 月 1 日,00:00:00 GMT”
您可以这样计算秒数:
secondsInMillis = time % (60 * 1000) //get remainder (modulo): seconds * milliseconds
if (secondsInMillis < 30000) {
time -= secondsInMillis; //round down
} else {
time += (60000 - secondsInMillis); // round up
}
您应该在 Date 对象上执行此操作。由于各种困难,包括一个月的长度(28、29、30 或 31),在时间纪元中没有简单的方法来计算它。
不要重新发明轮子。使用 java.time.Instant
表示时间瞬间:
Instant i = Instant.ofEpochMilli(time);
i = i.plusSeconds(30).truncatedTo(ChronoUnit.MINUTES);
Instant
不提供舍入,只提供截断。但是,添加 30 秒然后截断可以得到您想要的结果。如果您需要返回毫秒数,很简单:
time = i.toEpochMilli();
System.out.println(time);
使用你问题中的数字打印
12120000
(这等于 CET 中的 1970-01-01T03:22:00Z 或 1970-01-01T04:22+01:00[Europe/Paris] 的瞬间,或预期的向下舍入你的 04:22:16 CET
.)
PS 我非常相信像 Time4J 这样的库会提供舍入功能,因此您不需要添加和截断的技巧。很遗憾,我没有经验可以为您提供详细信息。
在我的 Java 项目中,我在 long
中使用了日期,例如它是 12136219
并通过如下创建 Date
对象:
long time = 12136219;
Date date = new Date(time);
表示日期为 Thu Jan 01 04:22:16 CET 1970
。我如何将日期(以长表示形式)舍入到分钟?
例如,如果秒数为 <30
,我想实现 Thu Jan 01 04:22:00 CET 1970
;如果秒数为 >=30
,我想实现 Thu Jan 01 04:23:00 CET 1970
,但我想绕过这个 long time = 12136219
表示.有什么想法吗?
当您从 long
创建 Date
时,long 表示自 1970 年 1 月 1 日以来的毫秒数。一分钟有 60*1000 毫秒。这应该足以形成您需要的舍入算法。
使用 Calendar.set
重置秒和毫秒Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
cal.add(Calendar.MINUTES, calendar.get(Calendar.SECOND) >= 30 ? 1 : 0)
currentDate = cal.getTimeInMillis();
Sets the given calendar field to the given value. The value is not interpreted by this method regardless of the leniency mode.
由于时间 "milliseconds since the standard base time known as "纪元”,即 1970 年 1 月 1 日,00:00:00 GMT” 您可以这样计算秒数:
secondsInMillis = time % (60 * 1000) //get remainder (modulo): seconds * milliseconds
if (secondsInMillis < 30000) {
time -= secondsInMillis; //round down
} else {
time += (60000 - secondsInMillis); // round up
}
您应该在 Date 对象上执行此操作。由于各种困难,包括一个月的长度(28、29、30 或 31),在时间纪元中没有简单的方法来计算它。
不要重新发明轮子。使用 java.time.Instant
表示时间瞬间:
Instant i = Instant.ofEpochMilli(time);
i = i.plusSeconds(30).truncatedTo(ChronoUnit.MINUTES);
Instant
不提供舍入,只提供截断。但是,添加 30 秒然后截断可以得到您想要的结果。如果您需要返回毫秒数,很简单:
time = i.toEpochMilli();
System.out.println(time);
使用你问题中的数字打印
12120000
(这等于 CET 中的 1970-01-01T03:22:00Z 或 1970-01-01T04:22+01:00[Europe/Paris] 的瞬间,或预期的向下舍入你的 04:22:16 CET
.)
PS 我非常相信像 Time4J 这样的库会提供舍入功能,因此您不需要添加和截断的技巧。很遗憾,我没有经验可以为您提供详细信息。