Converts a string of time in the format of 12hours HH:MMa to 24 hours - Android java.util

Android examples for java.util:Date Time

Description

Converts a string of time in the format of 12hours HH:MMa to 24 hours

Demo Code

import android.util.Log;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Set;

public class Main{

    /**// w w  w. j  a v a2  s . com
     * Converts a string of time in the format of 12hours HH:MMa to 24 hours 
     * 
     * @param timeIn12Hours the time written in 12 hours
     * @return a string with the time in 24 hours HH:MM or an empty string if there was a problem.
     */
    public static String convertTo24Hours(String timeIn12Hours) {
        SimpleDateFormat displayFormat = new SimpleDateFormat("HH:mm",
                Locale.ENGLISH);
        SimpleDateFormat parseFormat = new SimpleDateFormat("hh:mma",
                Locale.ENGLISH);
        try {
            Date date = parseFormat.parse(timeIn12Hours);
            return displayFormat.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return "";
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

}

Related Tutorials