user1004147
user1004147

Reputation: 323

Java/Android Calendar, add 1 week on Sundays

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?

Upvotes: 1

Views: 2075

Answers (1)

user1004147
user1004147

Reputation: 323

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

Upvotes: 1

Related Questions