-
Bug
-
Resolution: Fixed
-
P3
-
5.0
-
b28
-
generic
-
generic
GregorianCalendar.setGregorianChange doesn't adjust dates after setting the cutover date. The following test program fails.
import java.util.*;
public class CutoverTest {
public static void main(String[] args) {
GregorianCalendar cal = new GregorianCalendar();
Date x = new Date(2003-1900, 10-1, 1);
cal.setTime(x);
int[] orig = getDate(cal);
// Change the cutover date to yesterday, which shouldn't affect the date.
cal.setGregorianChange(new Date(x.getTime() - (24*3600*1000)));
int[] d1 = getDate(cal);
if (!compareDates(d1, orig)) {
throw new RuntimeException("Shouldn't change date: got " + toString(d1)
+ ", expected " + toString(orig));
}
// Change the cutover date to tomorrow, which should change the date.
cal.setGregorianChange(new Date(x.getTime() + (24*3600*1000)));
d1 = getDate(cal);
int[] expected = new int[] { 2003, 9, 18};
if (!compareDates(d1, expected)) {
throw new RuntimeException("should change date: got " + toString(d1)
+ ", expected " + toString(expected));
}
}
static int[] getDate(Calendar cal) {
int[] date = new int[3];
date[0] = cal.get(cal.YEAR);
date[1] = cal.get(cal.MONTH) + 1;
date[2] = cal.get(cal.DAY_OF_MONTH);
return date;
}
static boolean compareDates(int[] d1, int[] d2) {
for (int i = 0; i < d1.length; i++) {
if (d1[i] != d2[i]) {
return false;
}
}
return true;
}
static String toString(int[] date) {
return date[0] + "/" + date[1] + "/" + date[2];
}
}
###@###.### 2003-09-26
import java.util.*;
public class CutoverTest {
public static void main(String[] args) {
GregorianCalendar cal = new GregorianCalendar();
Date x = new Date(2003-1900, 10-1, 1);
cal.setTime(x);
int[] orig = getDate(cal);
// Change the cutover date to yesterday, which shouldn't affect the date.
cal.setGregorianChange(new Date(x.getTime() - (24*3600*1000)));
int[] d1 = getDate(cal);
if (!compareDates(d1, orig)) {
throw new RuntimeException("Shouldn't change date: got " + toString(d1)
+ ", expected " + toString(orig));
}
// Change the cutover date to tomorrow, which should change the date.
cal.setGregorianChange(new Date(x.getTime() + (24*3600*1000)));
d1 = getDate(cal);
int[] expected = new int[] { 2003, 9, 18};
if (!compareDates(d1, expected)) {
throw new RuntimeException("should change date: got " + toString(d1)
+ ", expected " + toString(expected));
}
}
static int[] getDate(Calendar cal) {
int[] date = new int[3];
date[0] = cal.get(cal.YEAR);
date[1] = cal.get(cal.MONTH) + 1;
date[2] = cal.get(cal.DAY_OF_MONTH);
return date;
}
static boolean compareDates(int[] d1, int[] d2) {
for (int i = 0; i < d1.length; i++) {
if (d1[i] != d2[i]) {
return false;
}
}
return true;
}
static String toString(int[] date) {
return date[0] + "/" + date[1] + "/" + date[2];
}
}
###@###.### 2003-09-26