GregorianCalendar: Jumping from Tuesday to Sunday

I have a GregorianCalendar instance for Tuesday September 2nd. The badge is checked in milliseconds and is ok. I want another calendar to be next Sunday (7) at 23:59:59. So:

GregorianCalendar currentCalendar = MyClock.INSTANCE.getCurrentCalendar();
GregorianCalendar nextSunday =
    (GregorianCalendar)currentCalendar.clone();
// GregorianCalendar uses Sunday as first day of week, so we must
// advance one week
int currentWeek = nextSunday.get(GregorianCalendar.WEEK_OF_YEAR);
nextSunday.set(GregorianCalendar.WEEK_OF_YEAR,
    currentWeek + this.THIS_WEEK);
nextSunday.set(GregorianCalendar.DAY_OF_WEEK, GregorianCalendar.SUNDAY);
nextSunday.set(GregorianCalendar.HOUR_OF_DAY, 23);
nextSunday.set(GregorianCalendar.MINUTE, 59);
nextSunday.set(GregorianCalendar.SECOND, 59);
nextSunday.set(GregorianCalendar.MILLISECOND, 0);

      

So, since Sunday is day # 1 of the week for GregorianCalendar and I am in week of year 36, I add one week and then set the day to Sunday.

The real problem is now: when running on my development machine with OpenJDK 1.7.0_55, it works fine. If I go to my test machine with OpenJDK 1.7.0_51, everything goes wrong:

Adds one week to day 9 and then ships on Sunday 14th instead of Sunday 7th.

I don't know if I'm doing it right or wrong: what really kills me is that the result is machine dependent, and I didn't find a difference in GregorianCalendar in these versions of OpenJDK. Any explanation for this behavior?

PD: Please stick to GregorianCalendar. I know, a little shitty, but I don't want to use Joda calendar or any other currently in development.

EDIT: I found the setWeekDate method (year, week_of_year, day_of_week). One would think that setting the year, week, and day of the week to the same method would give it success. It doesn't happen: it still goes from 2nd to 14th. What did the monkey write this?

+3


source to share


2 answers


I changed your code a bit:

SimpleDateFormat sdf = new SimpleDateFormat("dd MM yyyy - HH:mm:ss.SSSS Z");
GregorianCalendar currentCalendar = (GregorianCalendar) Calendar.getInstance();
currentCalendar.set(Calendar.DAY_OF_MONTH, 2);
System.out.println(sdf.format(currentCalendar.getTime()));
GregorianCalendar nextSunday = (GregorianCalendar) currentCalendar.clone();
// GregorianCalendar uses Sunday as first day of week, so we must
// advance one week
int currentWeek = nextSunday.get(GregorianCalendar.WEEK_OF_YEAR);
nextSunday.set(GregorianCalendar.WEEK_OF_YEAR, currentWeek + 1);
nextSunday.set(GregorianCalendar.DAY_OF_WEEK, GregorianCalendar.SUNDAY);
nextSunday.set(GregorianCalendar.HOUR_OF_DAY, 23);
nextSunday.set(GregorianCalendar.MINUTE, 59);
nextSunday.set(GregorianCalendar.SECOND, 59);
nextSunday.set(GregorianCalendar.MILLISECOND, 0);
System.out.println(sdf.format(nextSunday.getTime()));

      

Output:



02 12 2014 - 19:40:46.0250 +0200
07 12 2014 - 23:59:59.0000 +0200

      

It is right. However, I have two reasons:

  • check the value of this.THIS_WEEK. I replaced it with a value of 1 and it works fine on my machine.
  • check timezone on both machines (GMT + 2 in my case). Since both machines use the same code that both initializes the values ​​and uses them, there should be no problem. But if you use millisecond values ​​on another computer (for example, expose the value through a web service or something else), you may run into problems.
+2


source


I would try using the java.util.Calendar add (int field, int amount) method.



nextSunday.add (Calendar.DAY_OF_MONTH, 5). One line of code instead of three.

0


source







All Articles