What is date format of EEE, dd MMM yyyy HH:mm:ssZ?

2019-08-14 04:01发布

This question already has an answer here:

I'm trying to parse this on Android:

Fri, 02 Oct 2015 10:01:00+0300

with this format EEE, dd MMM yyyy HH:mm:ssZ, but doesn't work.

item_date = "Fri, 02 Oct 2015 18:07:00+0300";
SimpleDateFormat curFormater = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ssZ");

Date dateObj = null;
String newDateStr = item_date;
try {
  dateObj = curFormater.parse(item_date);
} catch (ParseException e) {
  e.printStackTrace();
}

1条回答
成全新的幸福
2楼-- · 2019-08-14 04:14

In order to parse a String into a Date which contains certains words (e.g. for the month), then you need an appropriate Locale corresponding to the language of these words. Since they are in english in your example you could use a language specific locale like java.util.Locale.ENGLISH or a country specific one, like java.util.Locale.US or Locale.UK.

Since you can pass the Locale to the SimpleDateFormat constructor your instantiation could look like this:

SimpleDateFormat curFormater = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ssZ", Locale.ENGLISH);

If you like to know the allowed date format words in your currently used Locale, then you can try the following snippet, which uses the DateFormatSymbols class:

DateFormatSymbols dfs = new DateFormatSymbols();
System.out.println(Arrays.toString(dfs.getShortMonths()));

This class provides corresponding methods to get months, the abbreviated months or the weekdays.
You can also pass a Locale to the DateFormatSymbols constructor to get these strings from the specific locale.

查看更多
登录 后发表回答