Get the next week of the year in Java using java.util.Calendar

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(); // I'm Locale.US and TimeZone "America/New_York" c.clear(); c.set(Calendar.YEAR, year); c.set(Calendar.WEEK_OF_YEAR, week); c.add(Calendar.WEEK_OF_YEAR, 1); return new int[] {c.get(Calendar.YEAR), c.get(Calendar.WEEK_OF_YEAR)} } 

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!

+4
source share
3 answers

At this point, I am going to assume that this is indeed a mistake. I filled out a bug report with Oracle here:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7197761

+1
source

Have you considered the fact that the weekly ISO8601 algorithm believes that the first week of the year will be the one that has at least 4 days during this year?

So, if January 1 is on thurdsay, this week is actually considered week 52 of the previous year, not one week of the current year.

You might want to at least consider joda time for such a calculation, since it has proper ISO standard processing.

  LocalDate ld = new LocalDate(); ld = ld.withWeekOfWeekyear(29); ld = ld.withWeekyear(2012); System.out.println(ld.getWeekOfWeekyear()); System.out.println(ld.getWeekyear()); // 29 // 2012 System.out.println(ld.plusWeeks(1).getWeekOfWeekyear()); System.out.println(ld.plusWeeks(1).getWeekyear()); // 30 // 2012 

And it will work across the borders of the year.

+2
source

Just do an explicit check to see if there is a rollover.

 return new int[] {c.get(Calendar.WEEK_OF_YEAR) == 1 ? c.get(Calendar.YEAR) + 1 : c.get(Calendar.YEAR), c.get(Calendar.WEEK_OF_YEAR)}; 

As far as I can tell, the reason WEEK_OF_YEAR not added correctly is because it is not an actual Calendar property like SECOND , MINUTE , MONTH , DAY or YEAR . This is a derived property similar to WEEK_OF_MONTH . One way around this is to use c.add(Calendar.DAY, 7) to add exactly 7 days instead of increasing WEEK_OF_YEAR .

+1
source

All Articles