Java Twitter Timestamp to Unix Timestamp

This post was written by Brandon on December 11, 2010
Posted Under: Java,Twitter

The Twitter API is very unorganized, everything is in obscure formats that need to be converted into something that can easily be worked with.
The Date stamp returned on all tweets is in a format that is not easily search able through a database, or compared to.

Here is a simple method that will convert the Twitter time string to a unix timestamp:

  public long twitterStampToTimestamp(String twitterStamp){
    long timestamp = 0;
    try {
      DateFormat formatter ;
      Date date;
      formatter = new SimpleDateFormat("EEE MMM d kk:mm:ss Z yyyy");
      date = (Date)formatter.parse(twitterStamp);
      java.sql.Timestamp timeStampDate = new Timestamp(date.getTime());
      timestamp = date.getTime() / 1000;
    } catch (Exception e){e.printStackTrace();}
    return timestamp;

That should help anyone who needs to convert the twitter timestamp to a unix timestamp in Java.

Also, this can easily be adapted to other languages, as the main structure is found in the Date format: "EEE MMM d kk:mm:ss Z yyyy"

Add a Comment

required, use real name
required, will not be published
optional, your blog address