How to set time to 24 hour format in Calendar

后端 未结 9 1425
迷失自我
迷失自我 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:56

    Here you will get all kinds of time related problems. I hope this will solve your problem.

    public class MyClass {
    
        public static void main(String[] args) {
    
    
            Calendar cal = Calendar.getInstance();
    
            // To get the current hour
            int hour = cal.get(Calendar.HOUR_OF_DAY);
            System.out.println("hour: " + hour);
    
            // To get the current time in 12 hours format
            SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a",Locale.US);
            String a = sdf.format(cal.getTime());
            System.out.println("Time: " + a);
    
            // To get the desired time in 12 hours format from 23 hours format
            cal.set(Calendar.HOUR_OF_DAY, 24);
            SimpleDateFormat sdf1 = new SimpleDateFormat("hh:mm a",Locale.ENGLISH);
            String a1 = sdf1.format(cal.getTime());
            System.out.println("Time: " + a1);
    
            /*  H Hour in day (0-23) 
                k Hour in day (1-24) 
            */
    
            //To get the desired time in 24 hours format as 0-23 or 1-24
            cal.set(Calendar.HOUR_OF_DAY, 24);
            SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm",Locale.ENGLISH);
            SimpleDateFormat sdf3 = new SimpleDateFormat("kk:mm",Locale.ENGLISH);
            String a2 = sdf2.format(cal.getTime());
            String a3 = sdf3.format(cal.getTime());
            System.out.println("Time: " + a2);
            System.out.println("Time: " + a3);
    
            //For example, time like 12:30 PM. How can i convert to 24 hours time in java?
    
            SimpleDateFormat bigFormat = new SimpleDateFormat("kk:mm");
            SimpleDateFormat smallFormat = new SimpleDateFormat("hh:mm a");
    
            Date date = null;
            try {
                date = smallFormat.parse("12:30 AM");
            } catch (ParseException e) {
                e.printStackTrace();
            }        
            System.out.println(smallFormat.format(date) + " = " + bigFormat.format(date));
    
        }
    
    }
    
    0 讨论(0)
  • 2020-12-25 13:04

    use SimpleDateFormat df = new SimpleDateFormat("HH:mm"); instead

    UPDATE

    @Ingo is right. is's better use setTime(d1);

    first method getHours() and getMinutes() is now deprecated

    I test this code

    SimpleDateFormat df = new SimpleDateFormat("hh:mm");
      Date d1 = df.parse("23:30");
      Calendar c1 = GregorianCalendar.getInstance();
      c1.setTime(d1);
      System.out.println(c1.getTime());
    

    and output is ok Thu Jan 01 23:30:00 FET 1970

    try this

    SimpleDateFormat df = new SimpleDateFormat("KK:mm aa");
      Date d1 = df.parse("10:30 PM");
      Calendar c1 = GregorianCalendar.getInstance(Locale.US);
      SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");  
      c1.setTime(d1);
      String str = sdf.format(c1.getTime());
      System.out.println(str);
    
    0 讨论(0)
  • 2020-12-25 13:06

    You can set the calendar to use only AM or PM using

    calendar.set(Calendar.AM_PM, int);

    0 = AM

    1 = PM

    Hope this helps

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