Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter date unparseable?

Tags:

java

date

twitter

I want to convert the date string in a Twitter response to a Date object, but I always get a ParseException and I cannot see the error!?!

Input string: Thu Dec 23 18:26:07 +0000 2010

SimpleDateFormat Pattern:

EEE MMM dd HH:mm:ss ZZZZZ yyyy

Method:

public static Date getTwitterDate(String date) {

SimpleDateFormat sf = new SimpleDateFormat(TWITTER);
sf.setLenient(true);
Date twitterDate = null;
try {
    twitterDate = sf.parse(date);
} catch (Exception e) {}
     return twitterDate;
}

I also tried this: http://friendpaste.com/2IaKdlT3Zat4ANwdAhxAmZ but that gives the same result.

I use Java 1.6 on Mac OS X.

Cheers,

Andi

like image 694
dabuki Avatar asked Dec 23 '10 19:12

dabuki


1 Answers

Your format string works for me, see:

public static Date getTwitterDate(String date) throws ParseException {

  final String TWITTER="EEE MMM dd HH:mm:ss ZZZZZ yyyy";
  SimpleDateFormat sf = new SimpleDateFormat(TWITTER);
  sf.setLenient(true);
  return sf.parse(date);
  }

public static void main (String[] args) throws java.lang.Exception
    {
      System.out.println(getTwitterDate("Thu Dec 3 18:26:07 +0000 2010"));          
    }

Output:

Fri Dec 03 18:26:07 GMT 2010

UPDATE

Roland Illig is right: SimpleDateFormat is Locale dependent, so just use an explicit english Locale: SimpleDateFormat sf = new SimpleDateFormat(TWITTER,Locale.ENGLISH);

like image 57
Michael Konietzka Avatar answered Oct 06 '22 03:10

Michael Konietzka