How to convert string to time in android?

android, time

Solution

Read SimpleDateFormat documentation (parse&format methods and pattern syntax).

http://developer.android.com/reference/java/text/SimpleDateFormat.html

String time = "530pm";
SimpleDateFormat dateFormat = new SimpleDateFormat("hmmaa"); 
SimpleDateFormat dateFormat2 = new SimpleDateFormat("hh:mm aa");
try {
    Date date = dateFormat.parse(time);

    String out = dateFormat2.format(date);
    Log.e("Time", out);
} catch (ParseException e) {
}

Problem

I am getting string from voice(Speech to text). When i speak time it's display e.g. "530pm". I want to convert only time from string. e.g. "530pm" to 05:30 PM. How can i achieve this? Thanks in advance. I try this code but not work ``` String s= "530 pm"; SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm aa"); try { Date date1 = dateFormat.parse(s); Log.e("Time", ""+date1); } catch (ParseException e) { // TODO Auto-generated catch block Log.e("Error", ""+e); } ```

Original source