3
2

More than 3 years have passed since last update.

TwitterのcreatedAt(String)をDateにparseする

Last updated at Posted at 2014-12-14

Twitter Kitで取得できるTwitterクラスのcreatedAtは、型がStringなのでDateとして扱うためにはparseする必要があります。

まぁ普通にSimpleDateFormatを使ってparseするだけでいいんですが、ちょっとハマったのでメモを残します。

private static final String DATE_FORMAT_TWITTER = "EEE MMM dd HH:mm:ss ZZZZZ yyyy";

public static Date convertToDate(String twitterDate) {
    // 第2引数のLocaleをセットしないとエラーになるので注意。
    SimpleDateFormat sf = new SimpleDateFormat(DATE_FORMAT_TWITTER, Locale.ENGLISH);
    try {
        return sf.parse(twitterDate);
    } catch (ParseException e) {
        Log.e(TAG, e.getMessage());
        return null;
    }
}
3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2