6

現在のタイムスタンプをミリ秒から秒に丸める方法は?

これがミリ秒単位の現在のタイムスタンプである場合 -

1384393612958

秒単位で四捨五入するとこんな感じになりますか?

Time in MS rounded off to nearest Second = 1384393612000

Java と C++ の両方でこれを行う必要があるかもしれません。

4

5 に答える 5

7

Python を使用している場合:

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

print new_number

基本的には、整数を使用し、1000 で割って (ミリ秒を削るため)、1000 を掛けて ms 値を秒に丸めます。

于 2013-12-04T20:25:33.660 に答える
4

Java では、Calendar を次のように使用できます。

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

別の方法として(C++でも機能します)、次のことができます。

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

500 ミリ秒を追加すると、数値を適切に丸めることができます。

于 2013-12-05T15:05:27.440 に答える
0
This is needed to convert java date object to mysql datetime formatted string in sql queries.

変換:

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();

実際のクエリ文字列には次が含まれます。

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

于 2017-11-15T08:19:38.280 に答える