Created
February 20, 2014 21:22
-
-
Save aweijnitz/9123459 to your computer and use it in GitHub Desktop.
Parsing UTC time from Twitter's created_at field
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.text.SimpleDateFormat; | |
import java.text.ParseException; | |
import java.util.Date; | |
import java.util.Locale; | |
/** Parse UTC time from Twitter's created_at field. | |
* | |
* Compile and run: javac TwitterDateParser.java && java TwitterDateParser | |
* | |
* See: | |
* - http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html | |
* - http://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html | |
* - http://docs.oracle.com/javase/7/docs/api/java/util/Locale.html | |
* - https://dev.twitter.com/docs/platform-objects/tweets | |
*/ | |
public class TwitterDateParser { | |
public static Date parseTwitterUTC(String date) | |
throws ParseException { | |
String twitterFormat="EEE MMM dd HH:mm:ss ZZZZZ yyyy"; | |
// Important note. Only ENGLISH Locale works. | |
SimpleDateFormat sf = new SimpleDateFormat(twitterFormat, Locale.ENGLISH); | |
sf.setLenient(true); | |
return sf.parse(date); | |
} | |
public static void main (String[] args) | |
throws Exception { | |
System.out.println( parseTwitterUTC("Wed Aug 27 13:08:45 +0000 2008").toString() ); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
"EEE MMM dd HH:mm:ss ZZZ yyyy" with Java 8 DateTimeFormatter