cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);会产生异常数据。

<p><a href="http://static.oschina.net/uploads/img/201307/27011628_B4oK.jpg"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="QQ图片20130727010711" border="0" alt="QQ图片20130727010711" src="http://static.oschina.net/uploads/img/201307/27011629_m5NE.jpg" width="438" height="226" /></a><a href="http://static.oschina.net/uploads/img/201307/27011809_NGZL.jpg"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="QQ图片20130727010658" border="0" alt="QQ图片20130727010658" src="http://static.oschina.net/uploads/img/201307/27011810_Mk9e.jpg" width="391" height="233" /></a></p> <p>&#160;</p> <p>右边的图输出的结果是有问题,理应输出的结果为&#160; 2018-12-24,查阅了资料以后发现 ,这算是JDK(准确地说是 GregorianCalendar)的一个bug(或是不方便之处)。</p> <p>jdk的bug系统里面有人提过这样的bug,但是好像处理结果挺奇怪的。见 <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6289214">JDK-6289214 : (cal) Calendar returns bad values if a first get is not call before a set of a weekday</a></p> <p>其中提出问题者使用的代码为:</p> <p><a href="http://static.oschina.net/uploads/img/201307/27011632_4bbz.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://static.oschina.net/uploads/img/201307/27011634_Bejx.png" width="477" height="250" /></a></p> <p>JDK官方给出的诊断方案为:&#160; 简单地概括成一句话就是:在set(DAY_OF_WEEK)方法之前先调用一个get方法,例如 cal.get(Calendar.DAY_OF_WEEK);</p> <p><a href="http://static.oschina.net/uploads/img/201307/27011634_nUd1.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://static.oschina.net/uploads/img/201307/27011636_NowQ.png" width="611" height="183" /></a></p> <p>&#160;</p> <p><font color="#ff0000">另: Calendar cal = Calendar.getInstance();&#160; 得到的 cal 对象 其实是子类 GregorianCalendar&#160; 类型的。</font></p>

转载于:https://my.oschina.net/yafey/blog/147417

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值