Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing a Twitter date with Joda Time

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?

like image 716
Bart van Heukelom Avatar asked Apr 03 '13 12:04

Bart van Heukelom


1 Answers

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

like image 101
Reimeus Avatar answered Oct 14 '22 10:10

Reimeus