如何将毫秒转换为"mm:ss:ms"格式?
How to convert milliseconds to "mm:ss:ms" format?
我正在开发一个程序,我可以在其中搜索两个文本文件并进行比较(这是 JetBrains Academy 的一项任务)。最后我的任务是输出执行它所花费的时间,格式为“mm:ss:ms”。图片参考:
这是我为实现这一目标而编写的代码。我想这远非正确,所以我对此表示歉意,我学习 Kotlin 和编程的时间并不长。
val millis = System.currentTimeMillis()
val time = String.format("%1$tM min. %1$tS sec. %1$tL ms.", millis);
println("Start searching...")
println("Found $number. Time taken: $time ")
我得到的输出是这样的:
> Start searching...
Found 864 / 864. Time taken: 19 min. 53 sec. 611 ms.
分秒错了,不到一秒。我对 ms 有疑问,因为它们适合花费多少时间。有人可以帮我解决我做错的事吗?谢谢!
来自System.currentTimeMillis()
:
Returns the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.
所以您在输出中看到的是从 1970-01-01T00:00:00 到现在所用时间的 Hh:mm:ss
部分。
您真正想要的是所用时间,即时间测量的开始和结束之间的差异。
val start = System.currentTimeMillis()
// do your processing
val end = System.currentTimeMillis()
val time = String.format("%1$tM min. %1$tS sec. %1$tL ms.", end - start)
这应该会给你一个合适的输出。
如评论中所述,某些时区会发生奇怪的事情。而且 String.format()
似乎是相当不可配置的(至少我没有找到任何东西)。
如果您真的想安全起见,可以使用 suggested by @SergeyAfinogenov,但要进行一些小的调整:
val minutes = duration.getSeconds() / 60
val seconds = duration.getSeconds() - minutes * 60
val millis = duration.getNano() / 1_000_000
val time = String.format("%d min. %d sec. %d ms.%n", minutes, seconds, millis)
这有效地手动计算了 Duration
的不同部分(分钟、秒、毫秒),然后相应地格式化它们。
我更愿意使用 Instant 和 ChronoUnit 类:
val now = Instant.now()
//doing something
val later = Instant.now()
val time = String.format("%02d min. %02d sec. %03d ms.",
ChronoUnit.MINUTES.between(now,later),
ChronoUnit.SECONDS.between(now,later)%60,
ChronoUnit.MILLIS.between(now,later)%1000
)
我正在开发一个程序,我可以在其中搜索两个文本文件并进行比较(这是 JetBrains Academy 的一项任务)。最后我的任务是输出执行它所花费的时间,格式为“mm:ss:ms”。图片参考:
这是我为实现这一目标而编写的代码。我想这远非正确,所以我对此表示歉意,我学习 Kotlin 和编程的时间并不长。
val millis = System.currentTimeMillis()
val time = String.format("%1$tM min. %1$tS sec. %1$tL ms.", millis);
println("Start searching...")
println("Found $number. Time taken: $time ")
我得到的输出是这样的:
> Start searching...
Found 864 / 864. Time taken: 19 min. 53 sec. 611 ms.
分秒错了,不到一秒。我对 ms 有疑问,因为它们适合花费多少时间。有人可以帮我解决我做错的事吗?谢谢!
来自System.currentTimeMillis()
:
Returns the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.
所以您在输出中看到的是从 1970-01-01T00:00:00 到现在所用时间的 Hh:mm:ss
部分。
您真正想要的是所用时间,即时间测量的开始和结束之间的差异。
val start = System.currentTimeMillis()
// do your processing
val end = System.currentTimeMillis()
val time = String.format("%1$tM min. %1$tS sec. %1$tL ms.", end - start)
这应该会给你一个合适的输出。
如评论中所述,某些时区会发生奇怪的事情。而且 String.format()
似乎是相当不可配置的(至少我没有找到任何东西)。
如果您真的想安全起见,可以使用
val minutes = duration.getSeconds() / 60
val seconds = duration.getSeconds() - minutes * 60
val millis = duration.getNano() / 1_000_000
val time = String.format("%d min. %d sec. %d ms.%n", minutes, seconds, millis)
这有效地手动计算了 Duration
的不同部分(分钟、秒、毫秒),然后相应地格式化它们。
我更愿意使用 Instant 和 ChronoUnit 类:
val now = Instant.now()
//doing something
val later = Instant.now()
val time = String.format("%02d min. %02d sec. %03d ms.",
ChronoUnit.MINUTES.between(now,later),
ChronoUnit.SECONDS.between(now,later)%60,
ChronoUnit.MILLIS.between(now,later)%1000
)