gpt4 book ai didi

Java 1.7,时钟格式不起作用

转载 作者:行者123 更新时间:2023-12-01 17:06:37 25 4
gpt4 key购买 nike

我尝试了一个非常简单的时钟(并且它有效)。但问题是,我得到的时间可能是:18:7,而不是 18:07。我尝试使用 < 10 的整数测试进行纠正,但由于某种原因它说它永远不会运行。你能告诉我为什么它不能比较以及如何纠正它吗?顺便说一句,这是代码:)

import javafx.animation.*;
import javafx.event.*;
import javafx.scene.control.Label;
import javafx.util.Duration;
import java.util.Calendar;

public class DigitalClock extends Label {

static private String addhour;
static private String addmin;
static private String addmonth;
static private String addday;

// constructor
public DigitalClock() {
bindToTime();
}

void bindToTime() {
Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(0),
new EventHandler<ActionEvent>() {
@SuppressWarnings("unused")
@Override
public void handle(ActionEvent actionEvent) {
Calendar time = Calendar.getInstance();
if (Calendar.HOUR_OF_DAY < 10) {
addhour = " 0";
} else {
addhour = " ";
}
if (Calendar.MINUTE < 10) {
addmin = ":0";
} else {
addmin = ":";
}
if ((Calendar.MONTH + 1) < 10) {
addmonth = ".0";
} else {
addmonth = ".";
}
if (Calendar.DAY_OF_MONTH < 10) {
addday = ".0";
} else {
addday = ".";
}
setText(addhour + time.get(Calendar.HOUR_OF_DAY)
+ addmin + time.get(Calendar.MINUTE)
+ "\n" + time.get(Calendar.YEAR)
+addmonth + (time.get(Calendar.MONTH) + 1)
+ addday + time.get(Calendar.DAY_OF_MONTH));
} // end of handle
} // end of EventHandler
), // end of new KeyFrame
new KeyFrame(Duration.seconds(1))); // end of Timeline
timeline.setCycleCount(Animation.INDEFINITE);
timeline.play();
} // end of bindToTime
} // end of class DigitalClock

感谢您的宝贵时间。

最佳答案

Calendar.MINUTE只是 Calendar 中的一个常数类,而不是 time 中的分钟数对象。

因此,而不是 Calendar.MINUTE < 10你应该写time.get(Calendar.MINUTE) < 10 .

尽管如此,我同意可怕的袋熊:尝试SimpleDateFormat首先。

关于Java 1.7,时钟格式不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25302177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com