Getting parse exception when I'm applying a particular format to the date.
SimpleDateFormat df = new SimpleDateFormat("hh:mm a");
try {
String s=timeSlotsArrayList.get(position).getScheduledStartTime();
Date d = df.parse(s);
times.setText(df.format(d));
}
catch (ParseException e) {
e.printStackTrace();
}
AM is getting instead of PM issue image
You can try the below format: SimpleDateFormat sdf = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy", Locale. ENGLISH);
parseexception: unparseable date” error message, when you try to parse the date string into another desired format. Don't worry it's a very common error message that users generally faced while parsing a date in java using SimpleDateFormat class.
For example, using a pattern of "MM/dd/yy" and a SimpleDateFormat instance created on Jan 1, 1997, the string "01/11/12" would be interpreted as Jan 11, 2012 while the string "05/04/64" would be interpreted as May 4, 1964. During parsing, only strings consisting of exactly two digits, as defined by Character.
The parse() Method of SimpleDateFormat class is used to parse the text from a string to produce the Date. The method parses the text starting at the index given by a start position.
You didn't apply correct format to your SimpleDateFormat
. Use
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
And then parse time like below:
Date d = df.parse(s);
String time = new SimpleDateFormat("hh:mm a").format(d);
times.setText(time);
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