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);
}
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) {
}
final String NEW_FORMAT = " hh:mm";
final String OLD_FORMAT = "hh:mm:ss";
String formatDate;
SimpleDateFormat sdf = new SimpleDateFormat(OLD_FORMAT);
Date d = null;
try {
d = sdf.parse(Value);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sdf.applyPattern(NEW_FORMAT);
formatDate=sdf.format(d);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With