7

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 ?

4

2 回答 2

12

这可能是由于语言环境。在 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);

会给你

France week of month is 4
USA week of month is 5
UK week of month is 4
于 2012-11-07T20:37:10.257 回答
0

仔细检查它设置的年份,如果一个设置为 2012 年,另一个设置为 2013 年,这将解释差异。

您能否让应用程序记录日历对象中保存的日期并将其发布在这里,以确保它们具有正确的信息。

于 2012-11-07T18:58:27.547 回答