Converting XX:XX AM/PM to 24 Hour Clock

前端 未结 6 1726
抹茶落季
抹茶落季 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:23

    You can easily use this method to convert AM/PM time to 24-hour format. Simply pass 12Hour format time to this method.

    public static String convert_AM_PM_TimeTo_24(String ampmtime){
        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a");
        Date testTime = null;
        try {
            testTime = sdf.parse(ampmtime);
            SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
            String newFormat = formatter.format(testTime);
            return newFormat;
        }catch(Exception ex){
            ex.printStackTrace();
            return ampmtime;
        }
    }
    

提交回复
热议问题