8

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?

steveman
  • 139
  • 7
  • 2
    I'm also seeing case 1. You could let the other user do a `toString` of the calendar object. And see if you can find a difference in those. – Jorn Vernee Aug 24 '16 at 12:00
  • 1
    You maty query those Calendar objects using `getMinimalDaysInFirstWeek()` and `getFirstDayOfWeek()` (try to think of more get methods to try). I wouldn’t know, but I would expect `Calendar` to pick these up from the operating system’s settings rather than from the locale you provide in Java. – Ole V.V. Aug 24 '16 at 12:15
  • 1
    Depending on when this code was run (time, not date), it might be useful to set the 2 machines into the same timezone. Also, you could see what the first week of the year is determined to be for the 2 cases. – N.T. Aug 24 '16 at 12:23
  • I did a side-by-side comparison of the TimeZone object, which holds the day offsets it looks like, and these match for my and the other computer. However, for the default and DE locales I see that getFirstDayOfWeek() differ, on my machine it's 2, and on the problem machine it's 1. The US locale has 1 in both cases. – steveman Aug 24 '16 at 13:58
  • I managed to do the side by side comparison of the whole Calendar toString() output just now, and there's interesting differences there. the US calendars match, but the default and DE ones differ: id="Europe/Berlin" myvalue --- WrongValue firstDayOfWeek=2 --- firstDayOfWeek=1, minimalDaysInFirstWeek=4, --- minimalDaysInFirstWeek=1, And probably as a result of the above these also differ: WEEK_OF_YEAR=34, --- WEEK_OF_YEAR=35, – steveman Aug 24 '16 at 14:15
  • http://stackoverflow.com/questions/24828014/week-of-year-inconsistent-on-different-machines Although I'm using the same version of javaw to execute the test programs, it could be possible that some other contamination between my java8 and her java7 is going on, somehow. I'll check this out. – steveman Aug 24 '16 at 14:20
  • 1
    You may try to change the date/time settings in Windows control panel (first day of week, minimum number of days in week 1) and see if the behaviour of your Java program changes or not. – Ole V.V. Aug 24 '16 at 20:57
  • @OleV.V. we did some tests with it, but java is unaffected by those changes, either through the clock/calendar settings or by altering the registry keys directly (and rebooting just for good measure). Which actually makes me wonder where java gets the time/calendar information from. – steveman Aug 25 '16 at 07:58
  • Okay, really strange and unsettling development, it turns out that the US week number as provided by the java Calendar library is incorrect! This week is week 34, not 35... So that's even wrong on my machine. I did a test with Joda time (We're on java 7 for the time being, so using java 8 Time isn't possible yet) and that shows week 34 on my machine, So when my colleague is back on Monday I'll give it a go on that machine as well. – steveman Aug 26 '16 at 08:51

1 Answers1

1

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.

steveman
  • 139
  • 7