Converting XX:XX AM/PM to 24 Hour Clock

前端 未结 6 1734
抹茶落季
抹茶落季 2021-02-15 16:41

I have searched google and I cannot find out how you can take a string: xx:xx AM/PM (ex. 3:30 PM) and change it so that it is now in 24 hours.

So for e

6条回答
  •  一向
    一向 (楼主)
    2021-02-15 17:07

    try this: 
    
    String string = "3:35 PM";
        Calendar calender = Calendar.getInstance();
        DateFormat format = new SimpleDateFormat( "hh:mm aa");
        Date date;
        date = format.parse( string );
        calender.setTime(date);
    
        System.out.println("Hour: " + calender.get(Calendar.HOUR_OF_DAY));
        System.out.println("Minutes: " + calender.get(Calendar.MINUTE))
    

    ;

    works fine and same result as what you would like.

提交回复
热议问题