如何在Java中实时计算时间?

时间:2019-06-14 08:44:57

标签: java javafx time real-time-clock

我有一个可以实时显示时间计数的代码,但是问题是显示格式本身不合适。当前显示类似1:1:1的内容,需要显示类似01:01:01

的内容

这是我的代码:

ZonedDateTime currentTime = ZonedDateTime.now(ZoneId.of("Europe/Berlin"));
Timeline clock = new Timeline(new KeyFrame(Duration.ZERO, e -> {
    String currentDay = String.format("%tA", currentTime);
    String second = String.valueOf(currentTime.now().getSecond());
    String minute = String.valueOf(currentTime.now().getMinute());
    String hour = String.valueOf(currentTime.now().getHour());
    String CEST = String.format("%tZ", currentTime);
    String day = String.format("%td", currentTime);
    String month = String.format("%tB", currentTime);
    int Year = currentTime.now().getYear();
    currentDateTime.setText(currentDay + ", " + hour + ":" + (minute) + ":" + second + ", " + CEST + " " + day + " " + month + " " + Year);
}),
        new KeyFrame(Duration.seconds(1))
);
clock.setCycleCount(Animation.INDEFINITE);
clock.play();

告诉我如何格式化输出?

0 个答案:

没有答案