Java日历问题,JDK 1.6.0.22 [英] Java calendar problem, JDK 1.6.0.22

查看:196
本文介绍了Java日历问题,JDK 1.6.0.22的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,获得一周的一周。在我的机器上安装JDK 1.6.0.22版本,在另一台机器上1.6.0.21。两个机器返回不同的结果:

I have a problem with getting the week of year. On my machine JDK 1.6.0.22 version is installed, on another machine 1.6.0.21. And both machines return different results:


(1.6.0.22)周是:1
(1.6.0.21) 52

(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会给出错误的结果?

Why does JDK 1.6.0.22 give the wrong result?

推荐答案

此摘录 解释为什么会出现这种差异:

This excerpt from the API documentation explains why this difference can occur:


WEEK_OF_YEAR
字段的范围为1到53. 第一周为
年是最早的七天期间
从getFirstDayOfWeek()开始,
至少包含$ b $
因此取决于getMinimalDaysInFirstWeek(),
getFirstDayOfWeek()的
值,以及每周
的日期。

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.

日历的源代码



And from the source code of Calendar:


第一个DayOfWeek和minimalDaysInFirstWeek都是区域设置相关的。

Both firstDayOfWeek and minimalDaysInFirstWeek are locale-dependent.

所以这是确定这一点的区域设置,而不是时区!显然,在某些地区,一年的第1周被认为是从上一年开始。尝试执行此操作:

So it's the locale that determines this, not the time zone! Apparently, in some locales, week 1 of a year is considered to begin in the previous year. Try running this:

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

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

I bet that either you're running the different versions in different locales, or the locale data changed between these versions. The result "1" could even be the more correct one and due to a bug fix in the locale data.

这篇关于Java日历问题,JDK 1.6.0.22的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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