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

1

6

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

Siren answered 6/2, 2020 at 10:16 Comment(6)
How did you format df?Nardoo
Updated the code @Md.AsaduzzamanSiren
Are those two different issues? How can you get any AM or PM in the image when you get a parse exception first?Plast
As an aside consider throwing away the long outmoded and notoriously troublesome SimpleDateFormat and friends, and adding ThreeTenABP to your Android project in order to use java.time, the modern Java date and time API. It is so much nicer to work with.Plast
Does this answer your (first) question? Converting String to Date using SimpleDateFormat is returning random date [duplicate]. Maybe in conjunction with this? Date format conversion Android.Plast
Does this answer your question? What is this date format? 2011-08-12T20:17:46.384ZMyelencephalon
N
6

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);
Nardoo answered 6/2, 2020 at 10:24 Comment(4)
I need only time from the string,No need dateSiren
Worked but 12:30 is getting AM instead of PM. I added image below of my code.Siren
Use HH:mm:ss instead of hh:mm:ss for 24 hour formatNardoo
I got another issue related to the date. I want to reduce one day for particular date. But It's reducing for 1 month. df = new SimpleDateFormat("EEEE, dd MMMM YYYY"); Date prevDate = df.parse(dateText.getText().toString()); Calendar c = Calendar.getInstance(); c.setTime(prevDate); c.add(Calendar.DAY_OF_YEAR, -1); Date d=c.getTime(); dateText.setText(df.format(d));Siren

© 2022 - 2025 — McMap. All rights reserved.