加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

windows – Java日历问题,JDK 1.6.0.22

发布时间:2020-12-13 22:37:35 所属栏目:Windows 来源:网络整理
导读:获得一年中的一周我有一个问题.在我的机器上,JDK 1.6.0.22版本安装在另一台机器1.6.0.21上.两台机器都返回不同的结果: (1.6.0.22) week is: 1 (1.6.0.21) week is: 52 对于此代码: try { Calendar current = new GregorianCalendar(); DateFormat df = new
获得一年中的一周我有一个问题.在我的机器上,JDK 1.6.0.22版本安装在另一台机器1.6.0.21上.两台机器都返回不同的结果:

(1.6.0.22) week is: 1
(1.6.0.21) week is: 52

对于此代码:

try {
         Calendar current = new GregorianCalendar();
         DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
         Date d = df.parse("2010-12-28 19:04:38 GMT");
         current.setTime(d);
         int currentWeek = current.get(Calendar.WEEK_OF_YEAR);
         System.out.println("week is: "currentWeek);
      } catch (ParseException e) {
        e.printStackTrace();
      }

为什么JDK 1.6.0.22会给出错误的结果?

解决方法

这段摘录 from the API documentation解释了为什么会出现这种差异:

Values calculated for the WEEK_OF_YEAR
field range from 1 to 53. Week 1 for a
year is the earliest seven day period
starting on getFirstDayOfWeek() that
contains at least
getMinimalDaysInFirstWeek() days from
that year.
It thus depends on the
values of getMinimalDaysInFirstWeek(),
getFirstDayOfWeek(),and the day of
the week of January 1.

并从日历的源代码:

Both firstDayOfWeek and minimalDaysInFirstWeek are locale-dependent.

因此,区域设置决定了这一点,而不是时区!显然,在某些地区,一年的第1周被认为是在去年开始的.试试这个:

Calendar cal = new GregorianCalendar();
    System.out.println(Locale.getDefault());
    System.out.println(cal.getMinimalDaysInFirstWeek());
    System.out.println(cal.getFirstDayOfWeek());

我打赌你要么在不同的语言环境中运行不同的版本,要么在这些版本之间更改语言环境数据.结果“1”甚至可能是更正确的结果,并且由于区域设置数据中的错误修复.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读