Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
239 views
in Technique[技术] by (71.8m points)

java - Twitter date unparseable?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...