====HereistheoutputofthetestonWindows95==== expected=1901-04-0505:08:13.1234567 result=1901-04-0506:08:13.1234567
*/ publicvoid Test4031502() { // This bug actually occurs on Windows NT as well, and doesn't // require the host zone to be set; it can be set in Java.
String[] ids = TimeZone.getAvailableIDs(); boolean bad = false; for (int i = 0; i < ids.length; ++i) {
TimeZone zone = TimeZone.getTimeZone(ids[i]);
GregorianCalendar cal = new GregorianCalendar(zone);
cal.clear();
cal.set(1900, 15, 5, 5, 8, 13); if (cal.get(HOUR) != 5) {
logln(zone.getID() + " "
+ //zone.useDaylightTime() + " "
+ cal.get(DST_OFFSET) / (60 * 60 * 1000) + " "
+ zone.getRawOffset() / (60 * 60 * 1000)
+ ": HOUR = " + cal.get(HOUR));
bad = true;
}
} if (bad) {
errln("TimeZone problems with GC");
}
}
publicvoid Test4035301() {
GregorianCalendar c = new GregorianCalendar(98, 8, 7);
GregorianCalendar d = new GregorianCalendar(98, 8, 7); if (c.after(d)
|| c.after(c)
|| c.before(d)
|| c.before(c)
|| !c.equals(c)
|| !c.equals(d)) {
errln("Fail");
}
}
calendar.add(SECOND, 6); //This will print out todays date for MONTH and DAY_OF_MONTH //instead of the date it was set to. //This happens when adding MILLISECOND or MINUTE also
logln("MONTH: " + calendar.get(MONTH));
logln("DAY_OF_MONTH: "
+ calendar.get(DAY_OF_MONTH));
logln("MINUTE: " + calendar.get(MINUTE));
logln("SECOND: " + calendar.get(SECOND)); if (calendar.get(MONTH) != 3
|| calendar.get(DAY_OF_MONTH) != 18
|| calendar.get(SECOND) != 36) {
errln("Fail: Calendar.add misbehaves");
}
}
publicvoid Test4051765() {
Calendar cal = Calendar.getInstance();
cal.setLenient(false);
cal.set(DAY_OF_WEEK, 0); try {
cal.getTime();
errln("Fail: DAY_OF_WEEK 0 should be disallowed");
} catch (IllegalArgumentException e) { return;
}
}
/* User error - no bug here publicvoidTest4059524(){ // Create calendar for April 10, 1997 GregorianCalendarcalendar=newGregorianCalendar(); // print out a bunch of interesting things logln("ERA:"+calendar.get(calendar.ERA)); logln("YEAR:"+calendar.get(calendar.YEAR)); logln("MONTH:"+calendar.get(calendar.MONTH)); logln("WEEK_OF_YEAR:"+ calendar.get(calendar.WEEK_OF_YEAR)); logln("WEEK_OF_MONTH:"+ calendar.get(calendar.WEEK_OF_MONTH)); logln("DATE:"+calendar.get(calendar.DATE)); logln("DAY_OF_MONTH:"+ calendar.get(calendar.DAY_OF_MONTH)); logln("DAY_OF_YEAR:"+calendar.get(calendar.DAY_OF_YEAR)); logln("DAY_OF_WEEK:"+calendar.get(calendar.DAY_OF_WEEK)); logln("DAY_OF_WEEK_IN_MONTH:"+ calendar.get(calendar.DAY_OF_WEEK_IN_MONTH)); logln("AM_PM:"+calendar.get(calendar.AM_PM)); logln("HOUR:"+calendar.get(calendar.HOUR)); logln("HOUR_OF_DAY:"+calendar.get(calendar.HOUR_OF_DAY)); logln("MINUTE:"+calendar.get(calendar.MINUTE)); logln("SECOND:"+calendar.get(calendar.SECOND)); logln("MILLISECOND:"+calendar.get(calendar.MILLISECOND)); logln("ZONE_OFFSET:" +(calendar.get(calendar.ZONE_OFFSET)/(60*60*1000))); logln("DST_OFFSET:" +(calendar.get(calendar.DST_OFFSET)/(60*60*1000))); calendar=newGregorianCalendar(1997,3,10); calendar.getTime(); logln("April10,1997"); logln("ERA:"+calendar.get(calendar.ERA)); logln("YEAR:"+calendar.get(calendar.YEAR)); logln("MONTH:"+calendar.get(calendar.MONTH)); logln("WEEK_OF_YEAR:"+ calendar.get(calendar.WEEK_OF_YEAR)); logln("WEEK_OF_MONTH:"+ calendar.get(calendar.WEEK_OF_MONTH)); logln("DATE:"+calendar.get(calendar.DATE)); logln("DAY_OF_MONTH:"+ calendar.get(calendar.DAY_OF_MONTH)); logln("DAY_OF_YEAR:"+calendar.get(calendar.DAY_OF_YEAR)); logln("DAY_OF_WEEK:"+calendar.get(calendar.DAY_OF_WEEK)); logln("DAY_OF_WEEK_IN_MONTH:"+calendar.get(calendar.DAY_OF_WEEK_IN_MONTH)); logln("AM_PM:"+calendar.get(calendar.AM_PM)); logln("HOUR:"+calendar.get(calendar.HOUR)); logln("HOUR_OF_DAY:"+calendar.get(calendar.HOUR_OF_DAY)); logln("MINUTE:"+calendar.get(calendar.MINUTE)); logln("SECOND:"+calendar.get(calendar.SECOND)); logln("MILLISECOND:"+calendar.get(calendar.MILLISECOND)); logln("ZONE_OFFSET:" +(calendar.get(calendar.ZONE_OFFSET)/(60*60*1000)));// in hours logln("DST_OFFSET:" +(calendar.get(calendar.DST_OFFSET)/(60*60*1000)));// in hours }
*/ publicvoid Test4059654() {
GregorianCalendar gc = new GregorianCalendar();
publicvoid Test4070502() {
@SuppressWarnings("deprecation")
Date d = getAssociatedDate(new Date(98, 0, 30));
Calendar cal = new GregorianCalendar();
cal.setTime(d); if (cal.get(DAY_OF_WEEK) == SATURDAY
|| cal.get(DAY_OF_WEEK) == SUNDAY) {
errln("Fail: Want weekday Got " + d);
}
}
/** *Gettheassociateddatestartingfromaspecifieddate *NOTE:theunnecessary"getTime()'s"belowareawork-aroundfora *buginjdk1.1.3(andprobablyearlierversionsalso) *<p> *@paramdateThedatetostartfrom
*/ publicstatic Date getAssociatedDate(Date d) {
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(d); //cal.add(field, amount); //<-- PROBLEM SEEN WITH field = DATE,MONTH // cal.getTime(); // <--- REMOVE THIS TO SEE BUG while (true) { int wd = cal.get(DAY_OF_WEEK); if (wd == SATURDAY || wd == SUNDAY) {
cal.add(DATE, 1); // cal.getTime();
} else { break;
}
} return cal.getTime();
}
void dowTest(boolean lenient) {
GregorianCalendar cal = new GregorianCalendar();
cal.set(1997, AUGUST, 12); // Wednesday // cal.getTime(); // Force update
cal.setLenient(lenient);
cal.set(1996, DECEMBER, 1); // Set the date to be December 1, 1996 int dow = cal.get(DAY_OF_WEEK); int min = cal.getMinimum(DAY_OF_WEEK); int max = cal.getMaximum(DAY_OF_WEEK);
logln(cal.getTime().toString()); if (min != SUNDAY || max != SATURDAY) {
errln("FAIL: Min/max bad");
} if (dow < min || dow > max) {
errln("FAIL: Day of week " + dow + " out of range");
} if (dow != SUNDAY) {
errln("FAIL: Day of week should be SUNDAY Got " + dow);
}
}
@SuppressWarnings("deprecation") publicvoid Test4071385() {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date(98, JUNE, 24));
cal.set(MONTH, NOVEMBER); // change a field
logln(cal.getTime().toString()); if (!cal.getTime().equals(new Date(98, NOVEMBER, 24))) {
errln("Fail");
}
}
publicvoid Test4073929() {
GregorianCalendar foo1 = new GregorianCalendar(1997, 8, 27);
foo1.add(DAY_OF_MONTH, +1); int testyear = foo1.get(YEAR); int testmonth = foo1.get(MONTH); int testday = foo1.get(DAY_OF_MONTH); if (testyear != 1997
|| testmonth != 8
|| testday != 28) {
errln("Fail: Calendar not initialized");
}
}
publicvoid Test4083167() {
TimeZone saveZone = TimeZone.getDefault(); try {
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
Date firstDate = new Date();
Calendar cal = new GregorianCalendar();
cal.setTime(firstDate); long firstMillisInDay = cal.get(HOUR_OF_DAY) * 3600000L
+ cal.get(MINUTE) * 60000L
+ cal.get(SECOND) * 1000L
+ cal.get(MILLISECOND);
logln(" Cal2 = " + cal2.getTime().getTime());
logln(" Cal2 time in ms = " + cal2.get(MILLISECOND)); if (!cal1.equals(cal2)) {
errln("Fail: Milliseconds randomized");
}
}
publicvoid Test4095407() {
GregorianCalendar a = new GregorianCalendar(1997, NOVEMBER, 13); int dow = a.get(DAY_OF_WEEK); if (dow != THURSDAY) {
errln("Fail: Want THURSDAY Got " + dow);
}
}
publicvoid Test4096231() {
TimeZone GMT = TimeZone.getTimeZone("GMT");
TimeZone PST = TimeZone.getTimeZone("PST"); int sec = 0, min = 0, hr = 0, day = 1, month = 10, year = 1997;
Calendar cal1 = new GregorianCalendar(PST);
cal1.setTime(new Date(880698639000L)); int p;
logln("PST 1 is: " + (p = cal1.get(HOUR_OF_DAY)));
cal1.setTimeZone(GMT); // Issue 1: Changing the timezone doesn't change the // represented time. int h1, h2;
logln("GMT 1 is: " + (h1 = cal1.get(HOUR_OF_DAY)));
cal1.setTime(new Date(880698639000L));
logln("GMT 2 is: " + (h2 = cal1.get(HOUR_OF_DAY))); // Note: This test had a bug in it. It wanted h1!=h2, when // what was meant was h1!=p. Fixed this concurrent with fix // to 4177484. if (p == h1 || h1 != h2) {
errln("Fail: Hour same in different zones");
}
Calendar cal2 = new GregorianCalendar(GMT);
Calendar cal3 = new GregorianCalendar(PST);
cal2.set(MILLISECOND, 0);
cal3.set(MILLISECOND, 0);
for (int x = 0; x < 12; x++) {
GregorianCalendar gc = new GregorianCalendar(1997, x, y[x]); int m1, m2;
log((m1 = gc.get(MONTH) + 1) + "/"
+ gc.get(DATE) + "/" + gc.get(YEAR)
+ " + 1mo = ");
gc.add(MONTH, 1);
logln((m2 = gc.get(MONTH) + 1) + "/"
+ gc.get(DATE) + "/" + gc.get(YEAR)
); int m = (m1 % 12) + 1; if (m2 != m) {
errln("Fail: Want " + m + " Got " + m2);
}
}
}
publicvoid Test4100311() {
Locale locale = Locale.getDefault(); if (!TestUtils.usesGregorianCalendar(locale)) {
logln("Skipping this test because locale is " + locale); return;
}
GregorianCalendar cal = (GregorianCalendar) Calendar.getInstance();
cal.set(YEAR, 1997);
cal.set(DAY_OF_YEAR, 1);
Date d = cal.getTime(); // Should be Jan 1
logln(d.toString()); if (cal.get(DAY_OF_YEAR) != 1) {
errln("Fail: DAY_OF_YEAR not set");
}
}
publicvoid Test4103271() {
Locale locale = Locale.getDefault(); if (!TestUtils.usesGregorianCalendar(locale)) {
logln("Skipping this test because locale is " + locale); return;
}
SimpleDateFormat sdf = new SimpleDateFormat(); int numYears = 40, startYear = 1997, numDays = 15;
String output, testDesc;
GregorianCalendar testCal = (GregorianCalendar) Calendar.getInstance();
testCal.clear();
sdf.setCalendar(testCal);
sdf.applyPattern("d MMM yyyy"); boolean fail = false; for (int firstDay = 1; firstDay <= 2; firstDay++) { for (int minDays = 1; minDays <= 7; minDays++) {
testCal.setMinimalDaysInFirstWeek(minDays);
testCal.setFirstDayOfWeek(firstDay);
testDesc = ("Test" + String.valueOf(firstDay) + String.valueOf(minDays));
logln(testDesc + " => 1st day of week="
+ String.valueOf(firstDay)
+ ", minimum days in first week="
+ String.valueOf(minDays)); for (int j = startYear; j <= startYear + numYears; j++) {
testCal.set(j, 11, 25); for (int i = 0; i < numDays; i++) {
testCal.add(DATE, 1);
String calWOY; int actWOY = testCal.get(WEEK_OF_YEAR); if (actWOY < 1 || actWOY > 53) {
Date d = testCal.getTime();
calWOY = String.valueOf(actWOY);
output = testDesc + " - " + sdf.format(d) + "\t";
output = output + "\t" + calWOY;
logln(output);
fail = true;
}
}
}
}
}
// Now compute the time from the fields, and make sure we // get the same answer back. This is a round-trip test.
Date save = testCal.getTime();
testCal.clear();
testCal.set(YEAR, DATA[j + 1 + i] < 25 ? 1998 : 1997);
testCal.set(WEEK_OF_YEAR, DATA[j + 1 + i]);
testCal.set(DAY_OF_WEEK, (i % 7) + SUNDAY); if (!testCal.getTime().equals(save)) {
logln(" Parse failed: " + testCal.getTime());
fail = true;
} else {
logln(" Passed");
}
// Test field disambiguation with a few special hard-coded cases. // This shouldn't fail if the above cases aren't failing.
@SuppressWarnings("deprecation")
Object[] DISAM = { 1998, 1, SUNDAY, new Date(97, DECEMBER, 28), 1998, 2, SATURDAY, new Date(98, JANUARY, 10), 1998, 53, THURSDAY, new Date(98, DECEMBER, 31), 1998, 53, FRIDAY, new Date(99, JANUARY, 1)};
testCal.setMinimalDaysInFirstWeek(3);
testCal.setFirstDayOfWeek(SUNDAY); for (int i = 0; i < DISAM.length; i += 4) { int y = (Integer) DISAM[i]; int woy = (Integer) DISAM[i + 1]; int dow = (Integer) DISAM[i + 2];
Date exp = (Date) DISAM[i + 3];
testCal.clear();
testCal.set(YEAR, y);
testCal.set(WEEK_OF_YEAR, woy);
testCal.set(DAY_OF_WEEK, dow);
log(y + "-W" + woy + "-DOW" + dow); if (!testCal.getTime().equals(exp)) {
logln(" FAILED expect: " + exp + "\n got: " + testCal.getTime());
fail = true;
} else {
logln(" OK");
}
}
// Now try adding and rolling
Object ADD = new Object();
Object ROLL = new Object();
@SuppressWarnings("deprecation")
Object[] ADDROLL = {
ADD, 1, new Date(98, DECEMBER, 25), new Date(99, JANUARY, 1),
ADD, 1, new Date(97, DECEMBER, 28), new Date(98, JANUARY, 4),
ROLL, 1, new Date(98, DECEMBER, 27), new Date(98, JANUARY, 4),
ROLL, 1, new Date(99, DECEMBER, 24), new Date(99, DECEMBER, 31),
ROLL, 1, new Date(99, DECEMBER, 25), new Date(99, JANUARY, 9)};
testCal.setMinimalDaysInFirstWeek(3);
testCal.setFirstDayOfWeek(SUNDAY); for (int i = 0; i < ADDROLL.length; i += 4) { int amount = (Integer) ADDROLL[i + 1];
Date before = (Date) ADDROLL[i + 2];
Date after = (Date) ADDROLL[i + 3];
@SuppressWarnings("deprecation") publicvoid Test4114578() {
Locale locale = Locale.getDefault(); if (!TestUtils.usesGregorianCalendar(locale)) {
logln("Skipping this test because locale is " + locale); return;
}
int ONE_HOUR = 60 * 60 * 1000;
TimeZone saveZone = TimeZone.getDefault(); boolean fail = false; try {
TimeZone.setDefault(TimeZone.getTimeZone("PST"));
Calendar cal = Calendar.getInstance(); long onset = new Date(98, APRIL, 5, 1, 0).getTime() + ONE_HOUR; long cease = new Date(98, OCTOBER, 25, 0, 0).getTime() + 2 * ONE_HOUR;
finalint ADD = 1; finalint ROLL = 2;
long[] DATA = { // Start Action Amt Expected_change
onset - ONE_HOUR, ADD, 1, ONE_HOUR,
onset, ADD, -1, -ONE_HOUR,
onset - ONE_HOUR, ROLL, 1, ONE_HOUR,
onset, ROLL, -1, -ONE_HOUR,
cease - ONE_HOUR, ADD, 1, ONE_HOUR,
cease, ADD, -1, -ONE_HOUR, // roll() was changed to support wall-clock-based roll (JDK-8152077). The // time value may jump 2 hours by skipping non-existent wall-clock time. // Note that JDK-4114578 was a problem of add(), not roll().
cease - ONE_HOUR, ROLL, 1, ONE_HOUR * 2,
cease, ROLL, -1, -ONE_HOUR * 2};
for (int i = 0; i < DATA.length; i += 4) {
Date date = new Date(DATA[i]); int amt = (int) DATA[i + 2]; long expectedChange = DATA[i + 3];
if (fail) {
errln("Fail: roll/add misbehaves around DST onset/cease");
}
}
/** *MakesuremaximumforHOURfieldis11,not12.
*/ publicvoid Test4118384() {
Calendar cal = Calendar.getInstance(); if (cal.getMaximum(HOUR) != 11
|| cal.getLeastMaximum(HOUR) != 11
|| cal.getActualMaximum(HOUR) != 11) {
errln("Fail: maximum of HOUR field should be 11");
}
}
/** *CheckisLeapYearforBCyears.
*/ publicvoid Test4125881() {
Locale locale = Locale.getDefault(); if (!TestUtils.usesGregorianCalendar(locale)) {
logln("Skipping this test because locale is " + locale); return;
}
GregorianCalendar cal = (GregorianCalendar) Calendar.getInstance();
DateFormat fmt = new SimpleDateFormat("MMMM d, yyyy G");
cal.clear(); for (int y = -20; y <= 10; ++y) {
cal.set(ERA, y < 1 ? GregorianCalendar.BC : GregorianCalendar.AD);
cal.set(YEAR, y < 1 ? 1 - y : y);
logln(y + " = " + fmt.format(cal.getTime()) + " "
+ cal.isLeapYear(y)); if (cal.isLeapYear(y) != ((y + 40) % 4 == 0)) {
errln("Leap years broken");
}
}
}
/** *ProvethatGregorianCalendarisproleptic(itusedtocutoff *at45BC,andnothaveleapyearsbeforethen).
*/ publicvoid Test4125892() {
Locale locale = Locale.getDefault(); if (!TestUtils.usesGregorianCalendar(locale)) {
logln("Skipping this test because locale is " + locale); return;
}
GregorianCalendar cal = (GregorianCalendar) Calendar.getInstance();
DateFormat fmt = new SimpleDateFormat("MMMM d, yyyy G");
cal.clear();
cal.set(ERA, GregorianCalendar.BC);
cal.set(YEAR, 81); // 81 BC is a leap year (proleptically)
cal.set(MONTH, FEBRUARY);
cal.set(DATE, 28);
cal.add(DATE, 1); if (cal.get(DATE) != 29
|| !cal.isLeapYear(-80)) { // -80 == 81 BC
errln("Calendar not proleptic");
}
}
/** *CalendarandGregorianCalendarhashCode()methodsneedimprovement. *Calendarneedsagoodimplementationthatsubclassescanoverride, *andGregorianCalendarshouldusethatimplementation.
*/ publicvoid Test4136399() { /* Note: This test is actually more strict than it has to be. *Technically,thereisnorequirementthatunequalobjectshave *unequalhashes.Weonlyrequireequalobjectstohaveequalhashes. *Itisdesirableforunequalobjectstohavedistributedhashes,but *thereisnohardrequirementhere. * *Inthistestwemakeassumptionsaboutcertainattributesofcalendar *objectsgettingrepresentedinthehash,whichneednotalwaysbethe
* case (although it does work currently with the given test). */
Calendar a = Calendar.getInstance();
Calendar b = (Calendar) a.clone(); if (a.hashCode() != b.hashCode()) {
errln("Calendar hash code unequal for cloned objects");
}
b.setMinimalDaysInFirstWeek(7 - a.getMinimalDaysInFirstWeek()); if (a.hashCode() == b.hashCode()) {
errln("Calendar hash code ignores minimal days in first week");
}
b.setMinimalDaysInFirstWeek(a.getMinimalDaysInFirstWeek());
b.setFirstDayOfWeek((a.getFirstDayOfWeek() % 7) + 1); // Next day if (a.hashCode() == b.hashCode()) {
errln("Calendar hash code ignores first day of week");
}
b.setFirstDayOfWeek(a.getFirstDayOfWeek());
// Assume getTimeZone() returns a reference, not a clone // of a reference -- this is true as of this writing
b.getTimeZone().setRawOffset(a.getTimeZone().getRawOffset() + 60 * 60 * 1000); if (a.hashCode() == b.hashCode()) {
errln("Calendar hash code ignores zone");
}
b.getTimeZone().setRawOffset(a.getTimeZone().getRawOffset());
GregorianCalendar c = new GregorianCalendar();
GregorianCalendar d = (GregorianCalendar) c.clone(); if (c.hashCode() != d.hashCode()) {
errln("GregorianCalendar hash code unequal for clones objects");
}
Date cutover = c.getGregorianChange();
d.setGregorianChange(new Date(cutover.getTime() + 24 * 60 * 60 * 1000)); if (c.hashCode() == d.hashCode()) {
errln("GregorianCalendar hash code ignores cutover");
}
}
/** *GregorianCalendar.equals()ignorescutoverdate
*/ publicvoid Test4141665() {
GregorianCalendar cal = new GregorianCalendar();
GregorianCalendar cal2 = (GregorianCalendar) cal.clone();
Date cut = cal.getGregorianChange();
Date cut2 = new Date(cut.getTime() + 100 * 24 * 60 * 60 * 1000L); // 100 days later if (!cal.equals(cal2)) {
errln("Cloned GregorianCalendars not equal");
}
cal2.setGregorianChange(cut2); if (cal.equals(cal2)) {
errln("GregorianCalendar.equals() ignores cutover");
}
}
calendar.setTime(new Date(Long.MIN_VALUE)); int year1 = calendar.get(YEAR); int era1 = calendar.get(ERA);
calendar.setTime(new Date(Long.MAX_VALUE)); int year2 = calendar.get(YEAR); int era2 = calendar.get(ERA);
if (year1 == year2 && era1 == era2) {
errln("Fail: Long.MIN_VALUE or Long.MAX_VALUE wrapping around");
}
}
/** *MaximumvalueforYEARfieldwrong.
*/ publicvoid Test4145983() {
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
Date[] DATES = {new Date(Long.MAX_VALUE), new Date(Long.MIN_VALUE)}; for (int i = 0; i < DATES.length; ++i) {
calendar.setTime(DATES[i]); int year = calendar.get(YEAR); int maxYear = calendar.getMaximum(YEAR); if (year > maxYear) {
errln("Failed for " + DATES[i].getTime() + " ms: year="
+ year + ", maxYear=" + maxYear);
}
}
}
/** *ThisisabuginthevalidationcodeofGregorianCalendar.Asreported, *thebugseemsworsethanitreallyis,duetoabuginthewaythebug *reporttestwaswritten.InrealitythebugisrestrictedtotheDAY_OF_YEAR *field.-liu6/29/98
*/ publicvoid Test4147269() { final String[] fieldName = { "ERA", "YEAR", "MONTH", "WEEK_OF_YEAR", "WEEK_OF_MONTH", "DAY_OF_MONTH", "DAY_OF_YEAR", "DAY_OF_WEEK", "DAY_OF_WEEK_IN_MONTH", "AM_PM", "HOUR", "HOUR_OF_DAY", "MINUTE", "SECOND", "MILLISECOND", "ZONE_OFFSET", "DST_OFFSET"};
GregorianCalendar calendar = new GregorianCalendar();
calendar.setLenient(false);
@SuppressWarnings("deprecation")
Date date = new Date(1996 - 1900, JANUARY, 3); // Arbitrary date for (int field = 0; field < FIELD_COUNT; field++) {
calendar.setTime(date); // Note: In the bug report, getActualMaximum() was called instead // of getMaximum() -- this was an error. The validation code doesn't // use getActualMaximum(), since that's too costly. int max = calendar.getMaximum(field); int value = max + 1;
calendar.set(field, value); try {
calendar.getTime(); // Force time computation // We expect an exception to be thrown. If we fall through // to the next line, then we have a bug.
errln("Test failed with field " + fieldName[field]
+ ", date before: " + date
+ ", date after: " + calendar.getTime()
+ ", value: " + value + " (max = " + max + ")");
} catch (IllegalArgumentException e) {
}
}
}
/** *ReportedbugisthataGregorianCalendarwithacutoverofDate(Long.MAX_VALUE) *doesn'tbehaveasapureJuliancalendar. *CANNOTREPRODUCETHISBUG
*/ publicvoid Test4149677() {
TimeZone[] zones = {TimeZone.getTimeZone("GMT"),
TimeZone.getTimeZone("PST"),
TimeZone.getTimeZone("EAT")}; for (int i = 0; i < zones.length; ++i) {
GregorianCalendar calendar = new GregorianCalendar(zones[i]);
// Make sure extreme values don't wrap around
calendar.setTime(new Date(Long.MIN_VALUE)); if (calendar.get(ERA) != GregorianCalendar.BC) {
errln("Fail: Date(Long.MIN_VALUE) has an AD year in " + zones[i]);
}
calendar.setTime(new Date(Long.MAX_VALUE)); if (calendar.get(ERA) != GregorianCalendar.AD) {
errln("Fail: Date(Long.MAX_VALUE) has a BC year in " + zones[i]);
}
calendar.setGregorianChange(new Date(Long.MAX_VALUE)); // to obtain a pure Julian calendar
boolean is100Leap = calendar.isLeapYear(100); if (!is100Leap) {
errln("test failed with zone " + zones[i].getID());
errln(" cutover date is Date(Long.MAX_VALUE)");
errln(" isLeapYear(100) returns: " + is100Leap);
}
}
}
/** *CalendarandDateHOURbroken.IfHOURisout-of-range,Calendar *andDateclasseswillmisbehave.
*/ publicvoid Test4162587() {
TimeZone savedTz = TimeZone.getDefault();
TimeZone tz = TimeZone.getTimeZone("PST");
TimeZone.setDefault(tz);
GregorianCalendar cal = new GregorianCalendar(tz);
Date d;
try { for (int i = 0; i < 5; ++i) { if (i > 0) {
logln("---");
}
cal.clear();
cal.set(1998, APRIL, 5, i, 0);
d = cal.getTime();
String s0 = d.toString();
logln("0 " + i + ": " + s0);
cal.clear();
cal.set(1998, APRIL, 4, i + 24, 0);
d = cal.getTime();
String sPlus = d.toString();
logln("+ " + i + ": " + sPlus);
cal.clear();
cal.set(1998, APRIL, 6, i - 24, 0);
d = cal.getTime();
String sMinus = d.toString();
logln("- " + i + ": " + sMinus);
if (!s0.equals(sPlus) || !s0.equals(sMinus)) {
errln("Fail: All three lines must match");
}
}
} finally {
TimeZone.setDefault(savedTz);
}
}
/** *Adding12monthsbehavesdifferentlyfromadding1year
*/ publicvoid Test4165343() {
GregorianCalendar calendar = new GregorianCalendar(1996, FEBRUARY, 29);
Date start = calendar.getTime();
logln("init date: " + start);
calendar.add(MONTH, 12);
Date date1 = calendar.getTime();
logln("after adding 12 months: " + date1);
calendar.setTime(start);
calendar.add(YEAR, 1);
Date date2 = calendar.getTime();
logln("after adding one year : " + date2); if (date1.equals(date2)) {
logln("Test passed");
} else {
errln("Test failed");
}
}
/** *GregorianCalendar.getActualMaximum()doesnotaccountforfirstdayofweek.
*/ publicvoid Test4166109() { /* Test month: * *March1998 *SuMoTuWeThFrSa *1234567 *891011121314 *15161718192021 *22232425262728 *293031
*/ boolean passed = true; int field = WEEK_OF_MONTH;
for (int i = 0; i < years.length; i++) { boolean valid = years[i] <= maxYear;
calendar.set(field, years[i]);
Date dateAfter = calendar.getTime(); int newYear = calendar.get(field);
calendar.setTime(dateBefore); // restore calendar for next use
logln(" Year " + years[i] + (valid ? " ok " : " bad")
+ " => " + format.format(dateAfter)); if (valid && newYear != years[i]) {
errln(" FAIL: " + newYear + " should be valid; date, month and time shouldn't change");
} elseif (!valid && newYear == years[i]) {
errln(" FAIL: " + newYear + " should be invalid");
}
}
}
}
}
/** *Calendar.rollbroken *ThisbugreliesontheTimeZonebug4173604toalsobefixed.
*/ publicvoid Test4173516() {
Locale locale = Locale.getDefault(); if (!TestUtils.usesGregorianCalendar(locale)) {
logln("Skipping this test because locale is " + locale); return;
}
Calendar cal = Calendar.getInstance(PST, Locale.US);
cal.clear();
cal.set(1999, 3, 21, 15, 5, 0); // Arbitrary int h1 = cal.get(HOUR_OF_DAY);
cal.setTimeZone(EST); int h2 = cal.get(HOUR_OF_DAY); if (h1 == h2) {
errln("FAIL: Fields not updated after setTimeZone");
}
// getTime() must NOT change when time zone is changed. // getTime() returns zone-independent time in ms.
cal.clear();
cal.setTimeZone(PST);
cal.set(HOUR_OF_DAY, 10);
Date pst10 = cal.getTime();
cal.setTimeZone(EST);
Date est10 = cal.getTime(); if (!pst10.equals(est10)) {
errln("FAIL: setTimeZone changed time");
}
}
/** *Weekofyeariswrongatthestartandendoftheyear.
*/ publicvoid Test4197699() {
GregorianCalendar cal = new GregorianCalendar();
cal.setFirstDayOfWeek(MONDAY);
cal.setMinimalDaysInFirstWeek(4);
DateFormat fmt = new SimpleDateFormat("E dd MMM yyyy 'DOY='D 'WOY='w");
fmt.setCalendar(cal);
t = calendar.getTime();
calendar.set(DAY_OF_MONTH, 33);
t = calendar.getTime();
calendar.set(DAY_OF_MONTH, 0);
s = df.format(calendar.getTime()); if (!expected.equals(s)) {
errln("DAY_OF_MONTH w/o ZONE_OFFSET: expected: " + expected + ", got: " + s);
}
// The same thing must work with ZONE_OFFSET set try {
calendar.setTime(df.parse(expected));
} catch (Exception e) { thrownew RuntimeException("Unexpected parse exception", e);
}
t = calendar.getTime();
calendar.set(ZONE_OFFSET, calendar.get(ZONE_OFFSET));
calendar.set(DAY_OF_MONTH, 33);
t = calendar.getTime();
calendar.set(DAY_OF_MONTH, 0);
s = df.format(calendar.getTime()); if (!expected.equals(s)) {
errln("DAY_OF_MONTH: expected: " + expected + ", got: " + s);
}
expected = "1999/12/24"; // 0th week of 2000
calendar.clear();
Date initialDate = null; try {
initialDate = df.parse(expected);
calendar.setTime(initialDate);
} catch (Exception e) { thrownew RuntimeException("Unexpected parse exception", e);
}
t = calendar.getTime();
calendar.set(ZONE_OFFSET, calendar.get(ZONE_OFFSET)); // jump to the next year
calendar.set(WEEK_OF_YEAR, 100);
t = calendar.getTime();
calendar.set(WEEK_OF_YEAR, 0);
s = df.format(calendar.getTime()); if (!expected.equals(s)) {
errln("WEEK_OF_YEAR: expected: " + expected + ", got: " + s);
} // change the state back
calendar.clear();
calendar.setTime(initialDate);
calendar.set(ZONE_OFFSET, calendar.get(ZONE_OFFSET)); // jump to next month
calendar.set(WEEK_OF_MONTH, 7);
t = calendar.getTime();
calendar.set(WEEK_OF_MONTH, 0);
s = df.format(calendar.getTime()); if (!expected.equals(s)) {
errln("WEEK_OF_MONTH: expected: " + expected + ", got: " + s);
}
// Make sure the time fields work correctly.
calendar.clear();
df = new SimpleDateFormat("HH:mm:ss");
TimeZone tz = TimeZone.getTimeZone("GMT");
df.setTimeZone(tz);
calendar.setTimeZone(tz);
expected = "22:59:59"; try {
calendar.setTime(df.parse(expected));
} catch (Exception e) { thrownew RuntimeException("Unexpected parse exception", e);
}
t = calendar.getTime(); // time should be 22:59:59.
calendar.set(MINUTE, 61); // time should be 23:01:59.
t = calendar.getTime();
calendar.set(MINUTE, -1); // time should be back to 22:59:59.
s = df.format(calendar.getTime()); if (!expected.equals(s)) {
errln("MINUTE: expected: " + expected + ", got: " + s);
}
}
/** *4655637:Calendar.set()forDAY_OF_WEEKdoesnotreturntherightvalue * *<p>NeedtouseSimpleDateFormattotestbecauseacallto *get(int)changesinternalstatesofaCalendar.
*/ publicvoid Test4655637() {
Locale locale = Locale.getDefault(); if (!TestUtils.usesGregorianCalendar(locale)) {
logln("Skipping this test because locale is " + locale); return;
}
Calendar cal = Calendar.getInstance();
cal.setTime(new Date(1029814211523L));
cal.set(YEAR, 2001);
Date t = cal.getTime();
cal.set(MONTH, JANUARY);
t = cal.getTime();
privatevoid testRoll(Koyomi cal, int year, int month, int dayOfMonth) {
cal.clear();
cal.set(year, month, dayOfMonth);
cal.getTime(); // normalize fields
logln("Roll backwards from " + cal.toDateString()); for (int i = 0; i < 1000; i++) {
cal.roll(WEEK_OF_YEAR, -i); if (!cal.checkFieldValue(YEAR, year)) {
errln(cal.getMessage());
}
}
logln("Roll forewards from " + cal.toDateString()); for (int i = 0; i < 1000; i++) {
cal.roll(WEEK_OF_YEAR, +i); if (!cal.checkFieldValue(YEAR, year)) {
errln(cal.getMessage());
}
}
}
/** *4652830:GregorianCalendarrollbehavesunexpectedlyfordatesinBCera
*/ publicvoid Test4652830() {
Koyomi cal = new Koyomi(Locale.US);
cal.clear();
logln("BCE 9-2-28 (leap year) roll DAY_OF_MONTH++ twice");
cal.set(ERA, GregorianCalendar.BC);
cal.set(9, FEBRUARY, 28); if (cal.getActualMaximum(DAY_OF_YEAR) != 366) {
errln(" wrong actual max of DAY_OF_YEAR: got "
+ cal.getActualMaximum(DAY_OF_YEAR) + " expected " + 366);
}
cal.roll(DAY_OF_MONTH, +1); if (!cal.checkFieldValue(ERA, GregorianCalendar.BC)
|| !cal.checkDate(9, FEBRUARY, 29)) {
errln(cal.getMessage());
}
cal.roll(DAY_OF_MONTH, +1); if (!cal.checkFieldValue(ERA, GregorianCalendar.BC)
|| !cal.checkDate(9, FEBRUARY, 1)) {
errln(cal.getMessage());
}
}
/** *4740554:GregorianCalendar.getActualMaximumisinconsistentwithnormalization
*/ publicvoid Test4740554() {
logln("1999/(Feb+12)/1 should be normalized to 2000/Feb/1 for getActualMaximum");
Koyomi cal = new Koyomi(Locale.US);
cal.clear();
cal.set(1999, FEBRUARY + 12, 1); if (!cal.checkActualMaximum(DAY_OF_YEAR, 366)) {
errln(cal.getMessage());
} if (!cal.checkActualMaximum(DAY_OF_MONTH, 29)) {
errln(cal.getMessage());
}
}
cal.clear(); // Make sure to use Gregorian dates (before and after the // set() call) for testing
cal.set(250000, JANUARY, 1);
checkTimeCalculation(cal, HOUR_OF_DAY, Integer.MIN_VALUE,
(long) Integer.MIN_VALUE * 60 * 60 * 1000);
privatevoid checkTimeCalculation(Koyomi cal, int field, int value, long expectedDelta) { long time = cal.getTimeInMillis();
cal.set(field, value); long time2 = cal.getTimeInMillis(); if ((time + expectedDelta) != time2) {
String s = value == Integer.MAX_VALUE ? "Integer.MAX_VALUE" : "Integer.MIN_VALUE";
errln("set(" + Koyomi.getFieldName(field) + ", " + s + ") failed." + " got " + time2
+ ", expected " + (time + expectedDelta));
}
}
/** *4722650:Calendar.equalscanthrowanexceptioninnon-lenient *(piggy-backtestsforcompareTo()whichisnewin1.5)
*/ publicvoid Test4722650() {
Calendar cal1 = new GregorianCalendar();
cal1.clear();
Calendar cal2 = new GregorianCalendar();
cal2.clear();
cal2.setLenient(false);
cal1.set(2003, OCTOBER, 31);
cal2.set(2003, OCTOBER, 31); try { if (cal1.equals(cal2)) {
errln("lenient and non-lenient shouldn't be equal. (2003/10/31)");
} if (cal1.compareTo(cal2) != 0) {
errln("cal1 and cal2 should represent the same time. (2003/10/31)");
}
} catch (IllegalArgumentException e) {
errln("equals threw IllegalArugumentException with non-lenient");
}
cal1.set(2003, OCTOBER, 32);
cal2.set(2003, OCTOBER, 32); try { if (cal1.equals(cal2)) {
errln("lenient and non-lenient shouldn't be equal. (2003/10/32)");
} if (cal1.compareTo(cal2) != 0) {
errln("cal1 and cal2 should represent the same time. (2003/10/32)");
}
} catch (IllegalArgumentException e) {
errln("equals threw IllegalArugumentException with non-lenient");
}
cal1 = Calendar.getInstance(Locale.of("th", "TH"));
cal1.setTimeInMillis(0L);
cal2 = Calendar.getInstance(Locale.US);
cal2.setTimeInMillis(0L); if (cal1.equals(cal2)) {
errln("Buddhist.equals(Gregorian) shouldn't be true. (millis=0)");
} if (cal1.compareTo(cal2) != 0) {
errln("cal1 (Buddhist) and cal2 (Gregorian) should represent the same time. (millis=0)");
}
}
/** *4738710:API:Calendarcomparisonmethodsshouldbeimproved
*/ publicvoid Test4738710() {
Calendar cal0 = new GregorianCalendar(2003, SEPTEMBER, 30);
Comparable<Calendar> cal1 = new GregorianCalendar(2003, OCTOBER, 1);
Calendar cal2 = new GregorianCalendar(2003, OCTOBER, 2); if (!(cal1.compareTo(cal0) > 0)) {
errln("!(cal1 > cal0)");
} if (!(cal1.compareTo(cal2) < 0)) {
errln("!(cal1 < cal2)");
} if (cal1.compareTo(new GregorianCalendar(2003, OCTOBER, 1)) != 0) {
errln("cal1 != new GregorianCalendar(2003, OCTOBER, 1)");
}
if (cal0.after(cal2)) {
errln("cal0 shouldn't be after cal2");
} if (cal2.before(cal0)) {
errln("cal2 shouldn't be before cal0");
}
if (cal0.after(0)) {
errln("cal0.after() returned true with an Integer.");
} if (cal0.before(0)) {
errln("cal0.before() returned true with an Integer.");
} if (cal0.after(null)) {
errln("cal0.after() returned true with null.");
} if (cal0.before(null)) {
errln("cal0.before() returned true with null.");
}
}
void sub4633646(Koyomi cal) {
cal.getTime();
cal.set(WEEK_OF_MONTH, 1); if (cal.isLenient()) { if (!cal.checkDate(2001, DECEMBER, 31)) {
errln(cal.getMessage());
} if (!cal.checkFieldValue(WEEK_OF_MONTH, 6)) {
errln(cal.getMessage());
}
} else { try {
Date d = cal.getTime();
errln("didn't throw IllegalArgumentException in non-lenient");
} catch (IllegalArgumentException e) {
}
}
}
/** *4846659:Calendar:Bothset()androll()don'tworkforAM_PMtimefield *(Partiallyfixedonlyrollasof1.5)
*/ publicvoid Test4846659() {
Koyomi cal = new Koyomi();
cal.clear();
cal.set(2003, OCTOBER, 31, 10, 30, 30);
cal.getTime(); // Test roll()
cal.roll(AM_PM, +1); // should turn to PM if (!cal.checkFieldValue(HOUR_OF_DAY, 10 + 12)) {
errln("roll: AM_PM didn't change to PM");
}
cal.clear();
cal.set(2003, OCTOBER, 31, 10, 30, 30);
cal.getTime(); // Test set()
cal.set(AM_PM, PM); // should turn to PM if (!cal.checkFieldValue(HOUR_OF_DAY, 10 + 12)) {
errln("set: AM_PM didn't change to PM");
}
cal.clear();
cal.set(2003, OCTOBER, 31, 10, 30, 30);
cal.getTime();
cal.set(AM_PM, PM);
cal.set(HOUR, 9); if (!cal.checkFieldValue(HOUR_OF_DAY, 9 + 12)) {
errln("set: both AM_PM and HOUT didn't change to PM");
}
}
/** *4822110:GregorianCalendar.get()returnsanincorrectdateaftersetFirstDayOfWeek()
*/ publicvoid Test4822110() {
Koyomi cal = new Koyomi(Locale.US); // June 2003 // S M Tu W Th F S // 1 2 3 4 5 6 7 // 8 9 10 11 12 13 14 // 15 16 17 18 19 20 21 // 22 23 24 25 26 27 28 // 29 30
cal.clear(); // 6/1 to 6/7 should be the 1st week of June.
cal.set(2003, JUNE, 2);
cal.getTime(); // Let cal calculate time.
cal.setFirstDayOfWeek(MONDAY); // Now 6/2 to 6/8 should be the 2nd week of June. Sunday of // that week is 6/8.
logln("1: " + cal.get(WEEK_OF_MONTH) + ", " + cal.get(DAY_OF_MONTH));
cal.set(DAY_OF_WEEK, SUNDAY);
logln("1st Sunday of June 2003 with FirstDayOfWeek=MONDAY"); if (!cal.checkDate(2003, JUNE, 8)) {
errln(cal.getMessage());
}
}
// Serialize date1
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(date1);
byte[] buffer = baos.toByteArray();
// Deserialize it
ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
ObjectInputStream ois = new ObjectInputStream(bais);
GregorianCalendar date2 = (GregorianCalendar) ois.readObject();
if (!date1.equals(date2)) {
errln("date1.equals(date2) != true");
} if (date1.hashCode() != date2.hashCode()) {
errln("inconsistent hashCode() value (before=0x"
+ Integer.toHexString(date1.hashCode())
+ ", after=0x" + Integer.toHexString(date2.hashCode()) + ")");
}
}
/** *4980088:GregorianCalendar.getActualMaximumdoesn'tthrowexception
*/ publicvoid Test4980088() {
GregorianCalendar cal = new GregorianCalendar(); try { int x = cal.getMaximum(100);
errln("getMaximum(100) didn't throw an exception.");
} catch (IndexOutOfBoundsException e) {
logln("getMaximum: " + e.getClass().getName() + ": " + e.getMessage());
}
try { int x = cal.getLeastMaximum(100);
errln("getLeastMaximum(100) didn't throw an exception.");
} catch (IndexOutOfBoundsException e) {
logln("getLeastMaximum: " + e.getClass().getName() + ": " + e.getMessage());
}
try { int x = cal.getActualMaximum(100);
errln("getActualMaximum(100) didn't throw an exception.");
} catch (IndexOutOfBoundsException e) {
logln("getActualMaximum: " + e.getClass().getName() + ": " + e.getMessage());
}
try { int x = cal.getMinimum(100);
errln("getMinimum(100) didn't throw an exception.");
} catch (IndexOutOfBoundsException e) {
logln("getMinimum: " + e.getClass().getName() + ": " + e.getMessage());
}
try { int x = cal.getGreatestMinimum(100);
errln("getGreatestMinimum(100) didn't throw an exception.");
} catch (IndexOutOfBoundsException e) {
logln("getGreatestMinimum: " + e.getClass().getName() + ": " + e.getMessage());
}
try { int x = cal.getActualMinimum(100);
errln("getActualMinimum(100) didn't throw an exception.");
} catch (IndexOutOfBoundsException e) {
logln("getActualMinimum: " + e.getClass().getName() + ": " + e.getMessage());
}
}
/** *4965624:GregorianCalendar.isLeapYear(1000)returnsincorrectvalue
*/ publicvoid Test4965624() { // 5013094: This test case needs to use "GMT" to specify // Gregorian cutover dates.
TimeZone savedZone = TimeZone.getDefault();
TimeZone.setDefault(TimeZone.getTimeZone("GMT")); try {
Map<Date, Boolean> data = new HashMap<>();
data.put(getGregorianDate(999, OCTOBER, 1), Boolean.FALSE);
data.put(getGregorianDate(1000, JANUARY, 1), Boolean.FALSE);
data.put(getGregorianDate(1000, FEBRUARY, 1), Boolean.FALSE);
data.put(getGregorianDate(1000, FEBRUARY, 28), Boolean.FALSE);
data.put(getGregorianDate(1000, MARCH, 1), Boolean.TRUE);
data.put(getGregorianDate(1001, JANUARY, 1), Boolean.TRUE);
data.put(getGregorianDate(1001, JANUARY, 6), Boolean.TRUE);
data.put(getGregorianDate(1001, MARCH, 1), Boolean.TRUE);
data.keySet().forEach(d -> { boolean expected = data.get(d);
GregorianCalendar cal = new GregorianCalendar();
cal.setGregorianChange(d); if (cal.isLeapYear(1000) != expected) {
errln("isLeapYear(1000) returned " + cal.isLeapYear(1000)
+ " with cutover date (Julian) " + d);
}
});
} finally {
TimeZone.setDefault(savedZone);
}
}
// Note that we can't use Date to produce Gregorian calendar dates // before the default cutover date. static Date getGregorianDate(int year, int month, int dayOfMonth) {
GregorianCalendar g = new GregorianCalendar(); // Make g a pure Gregorian calendar
g.setGregorianChange(new Date(Long.MIN_VALUE));
g.clear();
g.set(year, month, dayOfMonth); return g.getTime();
}
/** *5006864:DefinetheminimumvalueofDAY_OF_WEEK_IN_MONTHas1
*/ publicvoid Test5006864() {
GregorianCalendar cal = new GregorianCalendar(); int min = cal.getMinimum(DAY_OF_WEEK_IN_MONTH); if (min != 1) {
errln("GregorianCalendar.getMinimum(DAY_OF_WEEK_IN_MONTH) returned "
+ min + ", expected 1.");
}
min = cal.getGreatestMinimum(DAY_OF_WEEK_IN_MONTH); if (min != 1) {
errln("GregorianCalendar.getGreatestMinimum(DAY_OF_WEEK_IN_MONTH) returned "
+ min + ", expected 1.");
}
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.