且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Twitter 日期无法解析?

更新时间:2023-09-19 10:09:58

你的格式字符串对我有用,见:

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

输出:

格林威治标准时间 2010 年 12 月 3 日星期五 18:26:07

Fri Dec 03 18:26:07 GMT 2010

更新

Roland Illig 是对的:SimpleDateFormat 是 Locale 相关的,所以只需使用明确的英语语言环境:SimpleDateFormat sf = new SimpleDateFormat(TWITTER,Locale.ENGLISH);

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