SimpleDateFormat将lenient设置为false的异常 [英] SimpleDateFormat Exception on setting lenient to false

查看:280
本文介绍了SimpleDateFormat将lenient设置为false的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  SimpleDateFormat f = new SimpleDateFormat(yyyy-MM-dd T'HH:mm:ss'.000Z'); 
f.setLenient(false);
String dateStr =2012-03-11T02:46:01.000Z;
f.parse(dateStr);

当lenient为true时,它工作正常。它奇怪地用于输入日期 2012-03-01T02:46:01.000Z ,即使宽松为false。
正在使用的默认时区:PST

解决方案

因为那个时间不存在于您的默认时区 - 它是日光节省时间变化的一天,时间从凌晨2点到凌晨3点,所以今天早上没有2:46。 :P



由于您正在解析UTC,请将 SimpleDateFormat 实例时区设置为UTC,如下所示:

  f.setTimeZone(TimeZone.getTimeZone(UTC)); 

,您的问题会消失。


Why is this code throwing exception of unparseable date?

SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'.000Z'");
f.setLenient(false);
String dateStr = "2012-03-11T02:46:01.000Z";
f.parse(dateStr);

It works fine when lenient is true. It strangely works for input date '2012-03-01T02:46:01.000Z' even with lenient as false. Default timezone being used : PST

解决方案

Because that time does not exist in your default time zone—it was daylight savings time change day, and time jumped from 2:00 a.m. to 3:00 a.m., so there was no 2:46 that morning. :P

Since you’re parsing UTC, set the SimpleDateFormat instance time zone to UTC like so:

f.setTimeZone(TimeZone.getTimeZone("UTC"));

and your problem will go away.

这篇关于SimpleDateFormat将lenient设置为false的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆