我想制作一个显示系统时间的模拟时钟。我的代码运行良好,但我遇到的唯一问题是我想以 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/