Question

Twitter gives me a date such as "Wed, 27 Mar 2013 15:12:14 +0000". I'm trying to parse it with:

DateTimeFormat.forPattern("EEE, dd MMM yyyy HH:mm:ss ZZZZZ").withLocale(Locale.ENGLISH);

But it fails:

Invalid format: "Wed, 03 Apr 2013 10:35:35 +0000" is malformed at "+0000"

I've tried replacing ZZZZZ with z, Z, and ZZZ, but no change. Can these dates be parsed this way?

Was it helpful?

Solution

Although you've said you've used a single Z in the format pattern, this works:

DateTimeFormatter format = 
    DateTimeFormat.forPattern("EEE, dd MMM yyyy HH:mm:ss Z").withLocale(Locale.ENGLISH);
DateTime dateTime = format.parseDateTime("Wed, 27 Mar 2013 15:12:14 +0000");

When parsing this format a single Z timezone character will suffice, 4 is invalid:

Z time zone offset/id zone -0800; -08:00; America/Los_Angeles

For more see the javadoc

OTHER TIPS

While the accepted answer is correct, The twitter dates that I'm getting from the API are in a different. This alternate parser works for that format:

DateTimeFormatter format = DateTimeFormat.forPattern("EE MMM dd HH:mm:ss Z yyyy").withLocale(Locale.ENGLISH)
String input = "Sat Jan 14 01:50:54 +0000 2017"
DateTime dateTime = format.parseDateTime(input)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top