Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calendar.WEEK_OF_MONTH gives different results on two different devices

I have two devices HTC with android 2.3.5 and Samsung with 2.3.6 now the problem i am facing is i need the date's week of month.

So i've written this code and installed on both the phones. and set the system date as

27th Jan 2013

  Calendar calendar = Calendar.getInstance();
  int weekOfMonth =  calendar.get(Calendar.WEEK_OF_MONTH);
  Log.i(TAG,"weekOfMonth = "+weekOfMonth);

now on HTC the output is

 weekOfMonth = 5

while on samsung running the same code produces

 weekOfMonth = 4

this really is screwing my logic n calculations ahead.

am i doing something wrong ?

like image 852
Shashank Degloorkar Avatar asked Nov 07 '12 18:11

Shashank Degloorkar


1 Answers

It is probably due to Locales. In Java

        Calendar calFr = Calendar.getInstance(TimeZone.getTimeZone("Europe/Paris"), Locale.FRANCE);
        Calendar calUs = Calendar.getInstance(TimeZone.getTimeZone("US/Eastern"), Locale.US);
        Calendar calUk = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.UK);
        calFr.set(2013, Calendar.JANUARY, 27);
        calUs.set(2013, Calendar.JANUARY, 27);
        calUk.set(2013, Calendar.JANUARY, 27);

        int weekOfMonthFr = calFr.get(Calendar.WEEK_OF_MONTH);
        int weekOfMonthUs = calUs.get(Calendar.WEEK_OF_MONTH);
        int weekOfMonthUk = calUk.get(Calendar.WEEK_OF_MONTH);
        System.out.println("France week of month is " + weekOfMonthFr);
        System.out.println("USA week of month is " + weekOfMonthUs);
        System.out.println("UK week of month is " + weekOfMonthUk);

will give you

France week of month is 4
USA week of month is 5
UK week of month is 4
like image 75
NickT Avatar answered Oct 17 '22 22:10

NickT