简体   繁体   中英

conversion of 24 hours clock in 12 hours JAVAFX

i want to make an analogue clock which displays system time. My code is working perfectly but the only issue i am having is that i want to show time in 12 Hours format but its showing in 24 hours format.

 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();
}

Use Calendar.HOUR instead of Calendar.HOUR_OF_DAY


From Javadocs for Hour :

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

From Javadocs for HOUR_OF_DAY :

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

Assuming, from your use of JavaFX, that you're also using Java 8:

Let the venerable Calendar class retire. Use java.time :

LocalTime currTime = LocalTime.now();
this.hour   = currTime.get(ChronoField.HOUR_OF_AMPM);
this.minute = currTime.getMinute();
this.second = currTime.getSecond();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM