2

OK, so my code has worked good until the day became Sunday.

I'm working on an app that uses the Calendar util allot, so it functioning the way i think it does is important to me! The problem:

    import java.util.Calendar;

    ...

    Calendar test = Calendar.getInstance();

    test.setFirstDayOfWeek(Calendar.MONDAY);

    Log.e("WEEEK TEST:", ""+ test.get(Calendar.WEEK_OF_YEAR));

    test.add(Calendar.WEEK_OF_YEAR, 1);

    Log.e("WEEEK TEST:", ""+ test.get(Calendar.WEEK_OF_YEAR));

Outputs this:

     06-01 14:04:07.636  12005-12005/test.app E/WEEEK TEST:﹕ 23
     06-01 14:04:07.636  12005-12005/test.app E/WEEEK TEST:﹕ 23

How can this even happen, and how do i fix it?

user1004147
  • 315
  • 1
  • 2
  • 10
  • Is your device date and time correct? Did you make that test today? – joao2fast4u Jun 01 '14 at 14:07
  • Yes i tested it today, but i don't think it matters how my phone is configured because I'm adding a week, which should add a week whatever the date on my phone is. Shouldn't it? How can this week be 23, and then in a week, it's also week 23? – user1004147 Jun 01 '14 at 14:24
  • I found a "hack": One has to add -1, then add 1 week, before using the Calendar after setFirstDayOfWeek(). – user1004147 Jun 01 '14 at 14:34
  • Then, you must explain your hack as an answer. It's a hack, yet it is still a solution. :D – joao2fast4u Jun 02 '14 at 00:21

1 Answers1

1
Calendar test = Calendar.getInstance();
test.add(Calendar.WEEK_OF_YEAR, -1);
test.add(Calendar.WEEK_OF_YEAR, 1);
test.setFirstDayOfWeek(Calendar.MONDAY);

Now "test" should work correctly

user1004147
  • 315
  • 1
  • 2
  • 10