Java无法解析的日期:美国/芝加哥时区 [英] Java Unparseable date: America/Chicago Timezone
问题描述
我的应用程序尝试解析美国/芝加哥时区的日期时遇到麻烦.我复习了许多其他ParseException问题,我想我已经用Java解析了日期的所有常见陷阱.以下代码适用于我遇到的所有其他日期/时间,但是由于某些原因,美国/芝加哥时区的所有日期都是不可解析的.
I'm having trouble with my app trying to parse a date with the America/Chicago timezone. I have reviewed a bunch of other ParseException questions, and I think I've gotten all the common gotchas with date parsing in Java. The following code works for all other date/times I've encountered, but all the dates with the America/Chicago timezone are for some reason unparseable.
我为dateFormat设置了语言环境,这没关系.我检查了其他各个部分是否正常,删除了不可见的控制字符,重新键入了字符串以确保没有复制/粘贴有害字符,但每次都会引发异常.
I set the locale for the dateFormat, and that didn't matter. I checked that each of the other parts were fine, I removed invisible control characters, I re-typed the string to make sure no nefarious characters were copy/pasted in, yet it throws an exception every time.
有什么想法吗?
dateFormat = new SimpleDateFormat("EEE MMM dd k:mm:ss zzz ");
dateString = "Wed May 02 15:45:47 America/Chicago ";
try {
entry.setDateTaken(dateFormat.parse(dateString));
} catch (java.text.ParseException e) { }
我有不想放弃Joda路线的理由,这是我不应该要做的非常简单的事情.
I have my reasons for wanting to not go the Joda route, and this is a simple enough thing I shouldn't have to.
推荐答案
我相信问题是您使用的是Olson时区ID,而我却没有思考 SimpleDateFormat
支持解析它们.当然,使用您的代码时,欧洲/伦敦"也存在相同的问题,因此它不是特定于芝加哥的.您可以举一个可以使用 起作用的时区标识符样式的示例吗?
I believe the problem is that you're using Olson time zone IDs, and I don't think SimpleDateFormat
supports parsing those. Certainly using your code I have the same problem with "Europe/London" so it's not specific to Chicago. Can you give any examples with that style of time zone identifier which do work?
这篇关于Java无法解析的日期:美国/芝加哥时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!