SimpleDateFormat 在特定日期失败
在Java 1.8上,此代码失败了
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
dateFormat.setLenient(false);
dateFormat.setTimeZone(TimeZone.getTimeZone("America/New_York"));
dateFormat.parse("03/11/2007 02:05:01");
,03:05:01
正确解析
On java 1.8 this code fails
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
dateFormat.setLenient(false);
dateFormat.setTimeZone(TimeZone.getTimeZone("America/New_York"));
dateFormat.parse("03/11/2007 02:05:01");
Interesting, that 03:05:01
parses correctly
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,那是因为2007-03-11T02:05:01在美国/new_york时区从未发生过。
The spring-forward daylight saving change occurred at 2007-03-11T07:00:00Z, so anyone watching a time-zone-aware clock would have seen:
You've told the
SimpleDateFormat
to handle the input strictly, then given it a date/time that didn't exist, so it's reasonable for it to fail.重要的是,无论您要如何处理这个问题,我都会强烈地鼓励您离开旧版
date
/日历
/code>/dateformat
类型,然后使用java.Time
。Yes, that's because 2007-03-11T02:05:01 never occurred in the America/New_York time zone.
The spring-forward daylight saving change occurred at 2007-03-11T07:00:00Z, so anyone watching a time-zone-aware clock would have seen:
You've told the
SimpleDateFormat
to handle the input strictly, then given it a date/time that didn't exist, so it's reasonable for it to fail.Importantly, regardless of how you want to handle this, I would strongly encourage you to move off the legacy
Date
/Calendar
/DateFormat
types, and usejava.time
instead.