JavaFX:将持续时间格式化为字符串?
是否有某种内置方法或简单函数可以将 Duration 转换为 hh:mm:ss 格式的字符串? 例如,我正在寻找将 123402 ms
的持续时间转换为“2:03
”字符串的东西。
Is there some kind of built-in method or a simple function that will convert Duration into a string in the hh:mm:ss
format? For example, I am looking for something that would convert a Duration of 123402 ms
into a String of "2:03
".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
或者,您可以使用 java.util.Formatter 中的标志。
这将导致前导 0,如“02.03”,对于 dur =“123402ms”。
Alternatively, you can use the flags from java.util.Formatter.
This will result in a leading 0, as in "02.03", for dur = "123402ms".
您可以使用 String.valueOf(dur) 将 Duration 转换为字符串。 然后,您可以使用 Java 格式化类来重新格式化字符串。 由于 JavaFX 将持续时间格式化为字符串的方式(例如“123402.0ms”),您必须截断 String 的末尾。 如果 JavaFX 有 Long.valueOf(dur) 函数,那么这会更容易。
请参阅下面的示例:
You can convert a Duration to a String using String.valueOf(dur). You can then use the Java formatting classes to reformat the String. You have to chop the end of String because of the way JavaFX formats durations as strings (eg '123402.0ms'). If JavaFX had a Long.valueOf(dur) function then this would be easier.
See sample below: