Java 如何以毫秒为单位将时间戳舍入到最接近的秒数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20385067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:33:06  来源:igfitidea点击:

How to round off timestamp in milliseconds to nearest seconds?

javac++timestampmillisecondsseconds

提问by AKIWEB

How to round off the current timestamp in milliseconds to seconds?

如何以毫秒为单位将当前时间戳舍入到秒?

If this is the current timestamp in milliseconds I have -

如果这是以毫秒为单位的当前时间戳,我有 -

1384393612958

The if I am rounding off to nearest second then will it be like this?

如果我四舍五入到最接近的秒,那么会是这样吗?

Time in MS rounded off to nearest Second = 1384393612000

I might need to do this both in Java and C++.

我可能需要在 Java 和 C++ 中都这样做。

采纳答案by Suman

If you are using Python:

如果您使用的是 Python:

old_number = 1384393612958
new_number = 1000 * (old_number / 1000)

print new_number

Basically you want to use an integer number, divide by one thousand (to shave off the milli-seconds), and then multiple by thousand to get the ms value rounded to seconds.

基本上你想使用一个整数,除以一千(以减少毫秒),然后乘以千来得到四舍五入到秒的 ms 值。

回答by Luca S.

In Java you can use Calendar to something like this:

在 Java 中,你可以使用 Calendar 来做这样的事情:

Calendar cal = Calendar.getInstance().setTimeInMillis(millisec);
int seconds = cal.get(Calendar.SECONDS);

Alternatively (and can work for C++ too) you can do:

或者(也可以用于 C++)你可以这样做:

int sec = ((millisec + 500) / 1000);

adding 500 ms allows you to round the number properly.

添加 500 毫秒可以让您正确地舍入数字。

回答by AravindR

This is needed to convert java date object to mysql datetime formatted string in sql queries.

Conversion:

转换:

Calendar cal = Calendar.getInstance();
cal.setTime(this.id.getCreatedOn());
if (cal.get(Calendar.MILLISECOND) >= 500 ) {
  System.out.println("Round off milliseconds to seconds");
  cal.set(Calendar.SECOND, cal.get(Calendar.SECOND) + 1);
}
Date roundedCreatedOn = cal.getTime();

Actual query string contains:

实际查询字符串包含:

createdOn = '" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(roundedCreatedOn)+ "'"

createdOn = '" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(roundedCreatedOn)+ "'"

回答by Basil Bourque

tl;dr

tl;博士

Instant.ofEpochMilli( 1_384_393_612_958L ) 
       .truncatedTo( ChronoUnit.SECONDS )
       .toEpochMilli() 

java.time

时间

The modern approach in Java uses the java.time classes.

Java 中的现代方法使用 java.time 类。

The Instantclass represents a point on the timeline in UTC with a resolution of nanoseconds.

Instant类表示UTC时间线与纳秒的分辨率上的点。

Instant instant = Instant.ofEpochMilli( 1_384_393_612_958L ) ;
Instant instantTrunc = instant.truncatedTo( ChronoUnit.SECONDS ) ;
long millis = instantTrunc.toEpochMilli() ;

回答by James Gentes

In javascript, using the moment.jslibrary:

在 javascript 中,使用moment.js库:

moment(timestamp).startOf('second')