当前位置: 首页 > 工具软件 > Cal.com > 使用案例 >

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

马星阑
2023-12-01

<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

 类似资料: