-
Bug
-
Resolution: Fixed
-
P4
-
1.2.2
-
b08
-
generic
-
generic
-
Verified
After the Calendar fields have been normalized, changing a date by setting DAY_OF_WEEK and DAY_OF_WEEK_IN_MONTH doesn't work if DAY_OF_WEEK_IN_MONTH is set before DAY_OF_WEEK.
Test case:
public static Date nextNthDayOf(int nth, int dayOfWeek, int hour, int minute, int second) {
Calendar cal = new GregorianCalendar();
int nthWeek = cal.get(cal.DAY_OF_WEEK_IN_MONTH);
System.out.println("dowim=" + nthWeek + ", nth=" + nth);
if (nthWeek > nth) {
cal.add(cal.MONTH, +1);
} else if (nthWeek == nth) {
int dow = cal.get(cal.DAY_OF_WEEK);
if (dow >= dayOfWeek) {
cal.add(cal.MONTH, +1);
}
}
cal.set(cal.DAY_OF_WEEK_IN_MONTH, nth);
cal.set(cal.DAY_OF_WEEK, dayOfWeek);
cal.set(cal.HOUR_OF_DAY, hour);
cal.set(cal.MINUTE, minute);
cal.set(cal.SECOND, second);
cal.set(cal.MILLISECOND, 0);
return cal.getTime();
}
Test case:
public static Date nextNthDayOf(int nth, int dayOfWeek, int hour, int minute, int second) {
Calendar cal = new GregorianCalendar();
int nthWeek = cal.get(cal.DAY_OF_WEEK_IN_MONTH);
System.out.println("dowim=" + nthWeek + ", nth=" + nth);
if (nthWeek > nth) {
cal.add(cal.MONTH, +1);
} else if (nthWeek == nth) {
int dow = cal.get(cal.DAY_OF_WEEK);
if (dow >= dayOfWeek) {
cal.add(cal.MONTH, +1);
}
}
cal.set(cal.DAY_OF_WEEK_IN_MONTH, nth);
cal.set(cal.DAY_OF_WEEK, dayOfWeek);
cal.set(cal.HOUR_OF_DAY, hour);
cal.set(cal.MINUTE, minute);
cal.set(cal.SECOND, second);
cal.set(cal.MILLISECOND, 0);
return cal.getTime();
}
- relates to
-
JDK-6513925 (cal) Provide way to get "first Tuesday in month" from java.util.Calendar
-
- Closed
-