Reputation: 5084
I get a Unparseable date when trying to parse a Date string that is sent to my android client.
This is the exception:
java.text.ParseException: Unparseable date: "2018-09-18T00:00:00Z" (at offset 19) at java.text.DateFormat.parse(DateFormat.java:571)
The date format my C# based backend sends (The C# object Property is DateTime):
2018-09-18T00:00:00Z
My Java Code where it fails:
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa");
String targetDate = "2018-09-18T00:00:00Z";
Date date = dateFormat.parse(targetDate));
How can I change my code to parse the exact format sent by the backend?
Upvotes: 3
Views: 7588
Reputation: 151
The date format sent by your backend follows ISO-8601 instant format.
You can use LocalDate.parse(targetDate, DateTimeFormatter.ISO_INSTANT)
to parse it.
LocalDate supports only SDK 26+, android 8 and higher
Upvotes: 2
Reputation: 69440
You have to change the format String to:
SimpleDateFormat dateFormatParse = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'");
String targetDate = "2018-09-18T00:00:00Z";
Date dateString = dateFormatParse.parse(targetDate));
Upvotes: 7