Skip to content

Instantly share code, notes, and snippets.

@aweijnitz
Created February 20, 2014 21:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save aweijnitz/9123459 to your computer and use it in GitHub Desktop.
Save aweijnitz/9123459 to your computer and use it in GitHub Desktop.
Parsing UTC time from Twitter's created_at field
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() );
}
}
@dminkovsky
Copy link

"EEE MMM dd HH:mm:ss ZZZ yyyy" with Java 8 DateTimeFormatter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment