Converting XX:XX AM/PM to 24 Hour Clock

21,156

Solution 1

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

Solution 2

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.

Solution 3

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

Solution 4

Mine did not work till I added Locale like this:

SimpleDateFormat date12Format = new SimpleDateFormat("hh:mm aa", Locale.US);

Solution 5

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/

Share:
21,156
user3505931
Author by

user3505931

Updated on July 21, 2022

Comments

  • user3505931
    user3505931 almost 2 years

    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 example the previous time would be "15:30". I have looked into simply using if then statements to manipulate the string, however it seems very tedious. Is there any easy way to do this?

    Input: 3:30 PM
    Expected Output:  15:30
    
  • Matej Špilár
    Matej Špilár about 10 years
    looks like i was few seconds late :(
  • Braj
    Braj about 10 years
    what is meaning of aa?
  • Braj
    Braj about 10 years
    I haven't found any this at SimpleDateFormat.
  • Braj
    Braj about 10 years
    You're most welcome. For more info have a look at SimpleDateFormat.
  • Matej Špilár
    Matej Špilár about 10 years
  • Bala Vishnu
    Bala Vishnu about 6 years
    Some phones strangly give "Parse exception" for this, kindly use new SimpleDateFormat("hh:mm aa"); if you find that error