Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

For a java application the week number is important to the end user. 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

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)

this week US: 35 (should be 35 on 24-8-2016)

this week DE: 34 (should be 34 on 24-8-2016)

this week default default: 34 (should be 34 on 24-8-2016)

But for the end user we see the wrong results:

Default locale: nl_NL (should be nl_NL)

this week US: 35 (should be 35 on 24-8-2016)

this week DE: 35 (should be 34 on 24-8-2016)

this week default default: 35 (should be 34 on 24-8-2016)

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. In MS Outlook we get the correct week number on both machines. This leads me to believe that the problem is restricted to java somehow.

What am I missing here?

like image 942
steveman Avatar asked Aug 24 '16 11:08

steveman


1 Answers

I just checked my Joda time test program on my colleague's machine, that works as it should. 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)

I suggest not putting too much time into getting the Calendar class to cooperate when dealing with week numbers in Java.

like image 177
steveman Avatar answered Nov 10 '22 10:11

steveman