How to set time to 24 hour format in Calendar

后端 未结 9 1386
迷失自我
迷失自我 2020-12-25 12:10

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         


        
相关标签:
9条回答
  • 2020-12-25 12:42

    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");
    
    0 讨论(0)
  • 2020-12-25 12:42

    Try this :

    SimpleDateFormat df = new SimpleDateFormat("hh:mm");
    

    Or

    SimpleDateFormat df = new SimpleDateFormat("KK:mm");
    

    Reference : SimpleDateFormat

    0 讨论(0)
  • 2020-12-25 12:43

    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();
    }
    
    0 讨论(0)
  • 2020-12-25 12:46

    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.

    0 讨论(0)
  • 2020-12-25 12:52

    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'

    0 讨论(0)
  • 2020-12-25 12:55

    tl;dr

    LocalTime.parse( "10:30" )  // Parsed as 24-hour time.
    

    java.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.
    

    ISO 8601

    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.

    0 讨论(0)
提交回复
热议问题