JavaFX:将持续时间格式化为字符串?

发布于 2024-08-01 22:19:22 字数 123 浏览 1 评论 0原文

是否有某种内置方法或简单函数可以将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

宛菡 2024-08-08 22:19:22

或者,您可以使用 java.util.Formatter 中的标志。

action: function() {
    txt = "{%tM dur}.{%tS dur}"
}

这将导致前导 0,如“02.03”,对于 dur =“123402ms”。

Alternatively, you can use the flags from java.util.Formatter.

action: function() {
    txt = "{%tM dur}.{%tS dur}"
}

This will result in a leading 0, as in "02.03", for dur = "123402ms".

一身仙ぐ女味 2024-08-08 22:19:22

您可以使用 String.valueOf(dur) 将 Duration 转换为字符串。 然后,您可以使用 Java 格式化类来重新格式化字符串。 由于 JavaFX 将持续时间格式化为字符串的方式(例如“123402.0ms”),您必须截断 String 的末尾。 如果 JavaFX 有 Long.valueOf(dur) 函数,那么这会更容易。

请参阅下面的示例:

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.control.Button;
import java.text.SimpleDateFormat;
import java.util.Date;

var fmt = new SimpleDateFormat("m:ss");
var dur: Duration = 123402ms;
var txt: String = String.valueOf(dur);

Stage {
    title : "Duration Switch"
    scene: Scene {
        width: 400
        height: 200
        content: [
            Text {
                font : Font {
                    size: 24
                }
                x: 10, y: 30
                content: bind "Duration={txt}"
            },
            Button {
                translateY: 140
                text: "Switch"
                action: function() {
                    var durStr = String.valueOf(dur);
                    durStr = durStr.substring(0, durStr.indexOf("."));
                    var date = new Date(Long.parseLong(durStr));
                    txt = fmt.format(date);
                }
            }
        ]
    }
}

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:

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.control.Button;
import java.text.SimpleDateFormat;
import java.util.Date;

var fmt = new SimpleDateFormat("m:ss");
var dur: Duration = 123402ms;
var txt: String = String.valueOf(dur);

Stage {
    title : "Duration Switch"
    scene: Scene {
        width: 400
        height: 200
        content: [
            Text {
                font : Font {
                    size: 24
                }
                x: 10, y: 30
                content: bind "Duration={txt}"
            },
            Button {
                translateY: 140
                text: "Switch"
                action: function() {
                    var durStr = String.valueOf(dur);
                    durStr = durStr.substring(0, durStr.indexOf("."));
                    var date = new Date(Long.parseLong(durStr));
                    txt = fmt.format(date);
                }
            }
        ]
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文