Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Calendar returns different dates

Tags:

java

I do the following:

Calendar c = Calendar.getInstance(TimeZone.getTimeZone("Europe/Berlin"));
c.setFirstDayOfWeek(Calendar.MONDAY);
c.set(Calendar.DAY_OF_MONTH, 9);
c.set(Calendar.MONTH, 3);
c.set(Calendar.YEAR, 2011);
c.set(Calendar.HOUR_OF_DAY, 10);
c.set(Calendar.MINUTE, 20);
int week = c.get(Calendar.WEEK_OF_YEAR)

On my local machine my Timezone is Europe/Berlin. On my server it is UTC. Running this code on local box returns week = 14. Running this code on my server returns week = 15.

I am out of ideas - can somebody explain that to me? This causes me big trouble :-(

Thanks for any pointers.

Cheers, Christian

like image 523
Christian Avatar asked Oct 10 '22 07:10

Christian


1 Answers

It is your Locale. When having a de_DE java locale the week is 14, when having an en_US locale the week is 15. Set both your machines to the same locale and they should work now.

I only tested my locale vs a German one, and could not find a reliable list online of which locales had the shifted week counting, but I'm certain this is the problem for you.

To see your locale programatically:

 Locale.getDefault();

To change your locale in Windows 7:

 Control Panel --> Region & Language --> Location Tab  --> Current Location
like image 146
BVSmallman Avatar answered Oct 19 '22 08:10

BVSmallman