Converting XX:XX AM/PM to 24 Hour Clock

前端 未结 6 1714
抹茶落季
抹茶落季 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 16:58

    Here is a link to the SimpleDateFormat Javadoc

    This is the way to go :

    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    
    public class TimeParsing {
    
        public static void main(String[] args) {
            try {
                // Declare a date format for parsing
                SimpleDateFormat dateParser = new SimpleDateFormat("h:mm a");
    
                // Parse the time string
                Date date = dateParser.parse("3:30 PM");
    
                // Declare a date format for printing
                SimpleDateFormat dateFormater = new SimpleDateFormat("HH:mm");
    
                // Print the previously parsed time
                System.out.println(dateFormater.format(date));
    
            } catch (ParseException e) {
                System.err.println("Cannot parse this time string !");
            }
        }
    }
    

    Console output is : 15:30

    0 讨论(0)
  • 2021-02-15 17:04

    Try

      String time = "3:30 PM";
    
        SimpleDateFormat date12Format = new SimpleDateFormat("hh:mm a");
    
        SimpleDateFormat date24Format = new SimpleDateFormat("HH:mm");
    
        System.out.println(date24Format.format(date12Format.parse(time)));
    

    output:

    15:30
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-15 17:16

    Mine did not work till I added Locale like this:

    SimpleDateFormat date12Format = new SimpleDateFormat("hh:mm aa", Locale.US);
    
    0 讨论(0)
  • 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;
        }
    }
    
    0 讨论(0)
  • 2021-02-15 17:24
    SimpleDateFormat inFormat = new SimpleDateFormat("hh:mm aa");
    SimpleDateFormat outFormat = new SimpleDateFormat("HH:mm");
    String time24 = outFormat.format(inFormat.parse(yourTimeString));
    

    also you can read more about converting times here http://deepeshdarshan.wordpress.com/2012/08/17/how-to-change-time-from-12-hour-format-to-24-hour-format-in-java/

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