简体   繁体   English

同一地区在不同的计算机上为同一日期提供不同的星期数

[英]The same locale provides different week numbers for the same date on different machines

For a java application the week number is important to the end user. 对于Java应用程序,星期数对最终用户很重要。 However for some reason for the same date and locale the week numbers differ between my machine and that of the end user. 但是,由于相同的日期和语言环境,由于某些原因,我的机器与最终用户的机器之间的星期数有所不同。

We both run Windows 10 64bit and jre1.8.0_101 我们都运行Windows 10 64bit和jre1.8.0_101

I created a small program to illustrate the problem, and it yields different results between both machines. 我创建了一个小程序来说明问题,并且两台计算机之间会产生不同的结果。

Calendar calUs = Calendar.getInstance(Locale.US);
String usToday = "this week US: \t\t\t" + calUs.get(Calendar.WEEK_OF_YEAR) + " (should be 35 on 24-8-2016)";

Calendar calDe = Calendar.getInstance(Locale.GERMAN);
String deToday = "this week DE: \t\t\t" + calDe.get(Calendar.WEEK_OF_YEAR) + " (should be 34 on 24-8-2016)";

Calendar calDef = Calendar.getInstance();
String defToday = "this week default default: \t" + calDef.get(Calendar.WEEK_OF_YEAR)
        + " (should be 34 on 24-8-2016)";

System.out.println("Default locale: " + Locale.getDefault() + "\t(should be nl_NL)");
System.out.println(usToday);
System.out.println(deToday);
System.out.println(defToday);

On my machine this yields: 在我的机器上,结果如下:

Default locale: nl_NL (should be nl_NL) 默认语言环境:nl_NL(应为nl_NL)

this week US: 35 (should be 35 on 24-8-2016) 本周美国:35岁(2016年8月24日应为35岁)

this week DE: 34 (should be 34 on 24-8-2016) 本周DE:34(应该是2016年8月24日的34)

this week default default: 34 (should be 34 on 24-8-2016) 本周的默认默认值:34(2016年8月24日应该为34)

But for the end user we see the wrong results: 但是对于最终用户,我们看到了错误的结果:

Default locale: nl_NL (should be nl_NL) 默认语言环境:nl_NL(应为nl_NL)

this week US: 35 (should be 35 on 24-8-2016) 本周美国:35岁(2016年8月24日应为35岁)

this week DE: 35 (should be 34 on 24-8-2016) 本周DE:35(应该是2016年8月24日的34)

this week default default: 35 (should be 34 on 24-8-2016) 这周的预设预设值:35(应该是2016年8月24日的34)

I already tried changing the first day of week and first week of year settings in the windows registry to try and reproduce the issue, without success. 我已经尝试更改Windows注册表中的一周的第一天和一年的第一周的设置,以尝试重现该问题,但没有成功。 In MS Outlook we get the correct week number on both machines. 在MS Outlook中,我们在两台计算机上都获得正确的星期数。 This leads me to believe that the problem is restricted to java somehow. 这使我相信该问题某种程度上仅限于Java。

What am I missing here? 我在这里想念什么?

I just checked my Joda time test program on my colleague's machine, that works as it should. 我刚刚在同事的机器上检查了我的Joda时间测试程序,该程序应能正常工作。 So looks like we'll work towards changing the main program to use that library (and possibly switch to Time once we get to Java 8) 因此,看起来我们将努力更改主程序以使用该库(并且一旦进入Java 8,就可能切换到Time)。

I suggest not putting too much time into getting the Calendar class to cooperate when dealing with week numbers in Java. 我建议在处理Java中的周数时,不要花太多时间让Calendar类进行合作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM