Search code examples
androiddatesimpledateformatdate-parsingsamsung-galaxy

Issue in Convert time from 12 hours format to 24 hours in Smasung galaxy s6 Edge


Using this method to convert time "02:00 PM" = "14:00" This method is working well with Moto g3, Moto g4, Lenovo K3 note, Samsung Galaxy J3 ..please help me out to fix this issue...

 public static String convertTimeto24hours(String time) {
    String timein24hours = "";
    try {

        SimpleDateFormat displayFormat = new SimpleDateFormat("HH:mm");
        SimpleDateFormat parseFormat = new SimpleDateFormat("hh:mm a");
        Date date = null;
        try{
            date = parseFormat.parse(time);
            System.out.println(parseFormat.format(date) + " = " + displayFormat.format(date));
        }catch (ParseException e){
            e.printStackTrace();
        }
        timein24hours = "" + displayFormat.format(date);

        return timein24hours;
    } catch (Exception e) {
        print(e);
        Log.error("convertTimeto24hours",e);
    }
    return timein24hours;
}

Solution

  • you need to set the Locale . because when you don't set that, system pick up default locale. here is simple code:

    SimpleDateFormat displayFormat = new SimpleDateFormat("HH:mm",Locale.US);
    SimpleDateFormat parseFormat = new SimpleDateFormat("hh:mm a",Locale.US);