我正在尝试将字符串转换为日期,但我还没有得到它。
我的字符串格式为:
"Fri Jun 13 10:24:01 BRT 2014"
我在 Google 上搜索并找到了此解决方案,但仍然继续捕获异常。
这是我的代码:
java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("EEE MMM dd HH:mm:ss 'BRT' yyyy", Locale.getDefault());
df.setTimeZone(TimeZone.getTimeZone("BRT"));
try {
return df.parse(dateString);
} catch (Exception e) {
return null;
}
最佳答案
您需要:
SimpleDateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
df.setTimeZone(TimeZone.getTimeZone("BRT"));
DateFormat utcFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Date d = df.parse("Fri Jun 13 10:24:01 BRT 2014");
System.out.println(utcFormat.format(d)); // output: 2014-06-13T12:24:01+0200
考虑以下更正:
<Locale.ENGLISH
而不是Locale.getDefault()
可以可靠地解析“Jun”或“Fri”等英文名称使用模式符号 z 而不是文字“BRT”,否则解析器无法将字符串“BRT”解释为时区名称的缩写,该时区名称被解释为巴西标准时间(巴西标准时间)您的情况只是解析为文字,因此没有考虑 UTC-03:00 的时区偏移。
关于java - 将字符串转换为日期 BRT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24213324/