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

梦想的初衷 提交于 2019-12-13 01:29:39

问题


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.



来源:https://stackoverflow.com/questions/32911677/what-is-date-format-of-eee-dd-mmm-yyyy-hhmmssz

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!