我有以下字符串:dateToParse = "Fri May 16 23:59:59 BRT 2014"
,并希望使用DateFormat解析它:
DateFormat dateFormat = new SimpleDateFormat(pattern, Locale.getDefault());
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("America/Sao_Paulo"));
cal.setTime(dateFormat.parse(dateToParse));
现在我正在尝试使用pattern = "EEE MMM dd HH:mm:ss z yyyy"
,但得到此异常:
java.text.ParseException: Unparseable date: "Fri May 16 23:59:59 BRT 2014" (at offset 0)
我无法弄清楚这种模式有什么问题,特别是在索引0 ...我知道我错过了什么?感谢。
[编辑] 所以问题的一部分是我使用的是Locale.getDefault(),因此可能试图用英语中的日期格式解析英语日期...使用正确的语言环境,我仍然得到ParseException,但这次是偏移20,这意味着在解析时区时会出现问题(在我的情况下,' BRT')...
答案 0 :(得分:1)
可能是因为Locale。
尝试更改
Locale.getDefault()
到
Locale.ENGLISH
像这样
String date_ = "Fri May 16 23:59:59 BRT 2014";
DateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
Calendar date = Calendar.getInstance(TimeZone.getTimeZone("America/Sao_Paulo"));
dateFormat.setCalendar(date);
try {
date.setTime(dateFormat.parse(date_));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
答案 1 :(得分:0)
也许这种模式适合你" EEE MMM d HH:mm:ss Z yyyy"如果不看看他的网站 http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
前:
String pattern = "EEE MMM dd HH:mm:ss z yyyy";
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern, Locale.getDefault());
Calendar d = Calendar.getInstance();
try {
d.setTime(dateFormat.parse(String.valueOf(d)));
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
答案 2 :(得分:0)
public static void main(String[] args) throws ParseException {
String date = "Fri May 16 23:59:59 BRT 2014";
String pattern = "EEE MMM dd HH:mm:ss z yyyy";
DateFormat dateFormat = new SimpleDateFormat(pattern, Locale.getDefault());
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("America/Sao_Paulo"));
cal.setTime(dateFormat.parse(date));
}