Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unparseable date: "2013-07-11T13:41:22.000Z" (at offset 23)

Tags:

java

Can anybody tell me why in the world I got this exception?

08-28 08:47:05.246: D/DateParser(4238): String received for parsing is 2013-08-05T12:13:49.000Z

private final static String DATE_FORMAT_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";   public static Date parseDate(String stringToParse) {         Date date = null;         try {             date = new SimpleDateFormat(DATE_FORMAT_PATTERN).parse(stringToParse);         } catch (ParseException e) {             Logger.logError(TAG, e);         }         return null;     }  08-28 08:47:05.246: E/DateParser(4238): Exception: java.text.ParseException: Unparseable date: "2013-08-05T12:13:49.000Z" (at offset 23) 
like image 216
Eugene Avatar asked Aug 28 '13 08:08

Eugene


People also ask

How do you solve an Unparseable date?

Try this: SimpleDateFormat in = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy"); in. setTimeZone(TimeZone. getTimeZone("Asia/Calcutta")); //or Asia/Jerusalem String s2 = "Fri Oct 23 11:07:08 IST 2015"; Date date = in.

What is DateFormat?

DateFormat is an abstract class for date/time formatting subclasses which formats and parses dates or time in a language-independent manner. The date/time formatting subclass, such as SimpleDateFormat , allows for formatting (i.e., date -> text), parsing (text -> date), and normalization.


2 Answers

try using

String DATE_FORMAT_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" 

The Z at the end is usually the timezone offset. If you you don't need it maybe you can drop it on both sides.

like image 65
Scary Wombat Avatar answered Oct 13 '22 05:10

Scary Wombat


Use X instead of Z at the end of the format string:

yyyy-MM-dd'T'HH:mm:ss.SSSX 

to parse ISO-8601 format timezone offsets.

(Only works if you use Java 7. See this question).

like image 35
Jesper Avatar answered Oct 13 '22 03:10

Jesper