Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.text.parseException: Unparseable date : 2020-02-06T08:00:00

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

AM is getting instead of PM issue image

like image 514
Priyanka Avatar asked Feb 06 '20 10:02

Priyanka


People also ask

How do you handle Unparseable date exception in Java?

You can try the below format: SimpleDateFormat sdf = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy", Locale. ENGLISH);

What is Unparseable date?

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.

How do I format a simple date?

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.

What does SimpleDateFormat parse do?

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.


1 Answers

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);
like image 102
Md. Asaduzzaman Avatar answered Sep 24 '22 16:09

Md. Asaduzzaman