I would like to have a function that returns the next week and year, given the week and year. It looks something like this:
public static int[] getNextWeek(int year, int week) { Calendar c = Calendar.getInstance();
This sometimes does not work during the borders of the year. It seems to depend on what day you call it and for what parameters you use! For example, if I refer to it with 2012 and the 52nd week, then I expect the result to be the year 2013 and week 1. If you call it today (Tuesday July 17, 2012), it will work. If you set the day of the week to yesterday, it is not; and strangely leads to the year 2012 of week 1. Strange. What's happening? It seems to refer to the day of the week because it does not work if it is used on SUNDAY or MONDAY, which are the last two days of 2012! If I set the day of the week to the last day of the week, the function seems to work; before calling Calendar.add () I do:
// Must set to last day of week; initially set to first day due to API c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // SUNDAY c.add(Calendar.DAY_OF_WEEK, 6); // Must roll forward not backwards
It seems like there is no such weirdness if I create the getPreviousWeek method. Is this a java.util.Calendar error? Next time, I think I will use Joda's time!
source share