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

2019-08-14 03:34发布

问题:

This question already has an answer here:

  • How to parse date string to Date? [duplicate] 6 answers

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:

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.