java - JAVAFX 在 12 小时内转换 24 小时制

标签 java javafx

我想制作一个显示系统时间的模拟时钟。我的代码运行良好,但我遇到的唯一问题是我想以 12 小时格式显示时间,但它以 24 小时格式显示。

 void setCurrentTime() {
       Calendar calendar = new GregorianCalendar();
// Set current hour, minute, and second
this.hour = calendar.get(Calendar.HOUR_OF_DAY);
this.minute = calendar.get(Calendar.MINUTE);
this.second = calendar.get(Calendar.SECOND);
paintClock();
}

最佳答案

使用 Calendar.HOUR 而不是 Calendar.HOUR_OF_DAY


来自 Hour 的 Javadocs :

Field number for get and set indicating the hour of the morning or afternoon. HOUR is used for the 12-hour clock (0 - 11)

来自 HOUR_OF_DAY 的 Javadocs :

Field number for get and set indicating the hour of the day. HOUR_OF_DAY is used for the 24-hour clock.

关于java - JAVAFX 在 12 小时内转换 24 小时制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32932216/

相关文章:

java - JavaFx ColorPicker 中是否可以获取 'new color' ?

java - 使用 SerializationUtils.clone() 时出现 ClassNotFoundException

Java Swing : Changing color of a ball on mouseEntered

java - 如何通过xpath获取重复标签的值,就像键值对

java - Linux Mint Java 和 JavaFX

java - ComboBox 占位符 NullPointerException JavaFX

java.util.ConcurrentModificationException - 如何使用迭代器进行删除

java - ArrayIndexOutOfBoundsException 即将到来

java.lang.NoClassDefFoundError : org/hamcrest/SelfDescribing in Intellij

javafx-2 - 如何在选项卡 Pane 之间拖放选项卡节点