I need to set the time on the current date. The time string is always in 24 hour format but the result I get is wrong:
SimpleDateFormat df = new SimpleDate
if you replace in the function SimpleDateFormat("hh") with ("HH") will format the hour in 24 hours instead of 12.
SimpleDateFormat df = new SimpleDateFormat("HH:mm");
Try this :
SimpleDateFormat df = new SimpleDateFormat("hh:mm");
Or
SimpleDateFormat df = new SimpleDateFormat("KK:mm");
Reference : SimpleDateFormat
private void setClock() {
Timeline clock = new Timeline(new KeyFrame(Duration.ZERO, e -> {
Calendar cal = Calendar.getInstance();
int second = cal.get(Calendar.SECOND);
int minute = cal.get(Calendar.MINUTE);
int hour = cal.get(Calendar.HOUR_OF_DAY);
eski_minut = minute;
if(second < 10){
time_label.setText(hour + ":" + (minute) + ":0" + second);
}else if (minute < 10){
time_label.setText(hour + ":0" + (minute) + ":0" + second);
}
else {
time_label.setText(hour + ":" + (minute) + ":" + second);}
}),
new KeyFrame(Duration.seconds(1))
);
clock.setCycleCount(Animation.INDEFINITE);
clock.play();
}
Replace this:
c1.set(Calendar.HOUR, d1.getHours());
with this:
c1.set(Calendar.HOUR_OF_DAY, d1.getHours());
Calendar.HOUR is strictly for 12 hours.
I am using fullcalendar on my project recently, I don't know what exact view effect you want to achieve, in my project I want to change the event time view from 12h format from
to 24h format.
If this is the effect you want to achieve, the solution below might help:
set timeFormat: 'H:mm'
LocalTime.parse( "10:30" ) // Parsed as 24-hour time.
Avoid the troublesome old date-time classes such as Date
and Calendar
that are now supplanted by the java.time classes.
LocalTime
The java.time classes provide a way to represent the time-of-day without a date and without a time zone: LocalTime
LocalTime lt = LocalTime.of( 10 , 30 ); // 10:30 AM.
LocalTime lt = LocalTime.of( 22 , 30 ); // 22:30 is 10:30 PM.
The java.time classes use standard ISO 8601 formats by default when generating and parsing strings. These formats use 24-hour time.
String output = lt.toString();
LocalTime.of( 10 , 30 ).toString() : 10:30
LocalTime.of( 22 , 30 ).toString() : 22:30
So parsing 10:30
will be interpreted as 10:30 AM.
LocalTime lt = LocalTime.parse( "10:30" ); // 10:30 AM.
DateTimeFormatter
If you need to generate or parse strings in 12-hour click format with AM/PM, use the DateTimeFormatter
class. Tip: make a habit of specifying a Locale
.