3 *******************************************************************************
4 * Copyright (C) 2000-2009, International Business Machines Corporation and *
5 * others. All Rights Reserved. *
6 *******************************************************************************
8 package com.ibm.icu.dev.test.calendar;
9 import java.io.ByteArrayInputStream;
10 import java.io.ByteArrayOutputStream;
11 import java.io.IOException;
12 import java.io.ObjectInputStream;
13 import java.io.ObjectOutputStream;
14 import java.util.Date;
15 import java.util.HashSet;
16 import java.util.Locale;
17 import java.util.MissingResourceException;
19 import com.ibm.icu.impl.Utility;
20 import com.ibm.icu.text.DateFormat;
21 import com.ibm.icu.text.NumberFormat;
22 import com.ibm.icu.text.SimpleDateFormat;
23 import com.ibm.icu.util.Calendar;
24 import com.ibm.icu.util.GregorianCalendar;
25 import com.ibm.icu.util.SimpleTimeZone;
26 import com.ibm.icu.util.TimeZone;
27 import com.ibm.icu.util.ULocale;
31 * @bug 4031502 4035301 4040996 4051765 4059654 4061476 4070502 4071197 4071385
32 * 4073929 4083167 4086724 4092362 4095407 4096231 4096539 4100311 4103271
33 * 4106136 4108764 4114578 4118384 4125881 4125892 4136399 4141665 4142933
34 * 4145158 4145983 4147269 4149677 4162587 4165343 4166109 4167060 4173516
35 * 4174361 4177484 4197699 4209071 4288792
37 public class CalendarRegression extends com.ibm.icu.dev.test.TestFmwk {
39 public static void main(String[] args) throws Exception {
40 new CalendarRegression().run(args);
42 static final String[] FIELD_NAME = {
43 "ERA", "YEAR", "MONTH", "WEEK_OF_YEAR", "WEEK_OF_MONTH",
44 "DAY_OF_MONTH", "DAY_OF_YEAR", "DAY_OF_WEEK",
45 "DAY_OF_WEEK_IN_MONTH", "AM_PM", "HOUR", "HOUR_OF_DAY",
46 "MINUTE", "SECOND", "MILLISECOND", "ZONE_OFFSET",
47 "DST_OFFSET", "YEAR_WOY", "DOW_LOCAL", "EXTENDED_YEAR",
48 "JULIAN_DAY", "MILLISECONDS_IN_DAY"
53 Synopsis: java.sql.Timestamp constructor works wrong on Windows 95
55 ==== Here is the test ====
56 public static void main (String args[]) {
57 java.sql.Timestamp t= new java.sql.Timestamp(0,15,5,5,8,13,123456700);
58 logln("expected=1901-04-05 05:08:13.1234567");
62 ==== Here is the output of the test on Solaris or NT ====
63 expected=1901-04-05 05:08:13.1234567
64 result=1901-04-05 05:08:13.1234567
66 ==== Here is the output of the test on Windows95 ====
67 expected=1901-04-05 05:08:13.1234567
68 result=1901-04-05 06:08:13.1234567
71 public void Test4031502() {
73 // This bug actually occurs on Windows NT as well, and doesn't
74 // require the host zone to be set; it can be set in Java.
75 String[] ids = TimeZone.getAvailableIDs();
77 for (int i=0; i<ids.length; ++i) {
78 TimeZone zone = TimeZone.getTimeZone(ids[i]);
79 GregorianCalendar cal = new GregorianCalendar(zone);
81 cal.set(1900, 15, 5, 5, 8, 13);
82 if (cal.get(Calendar.HOUR) != 5) {
83 logln("Fail: " + zone.getID() + " " +
84 zone.useDaylightTime() + "; DST_OFFSET = " +
85 cal.get(Calendar.DST_OFFSET) / (60*60*1000.0) + "; ZONE_OFFSET = " +
86 cal.get(Calendar.ZONE_OFFSET) / (60*60*1000.0) + "; getRawOffset() = " +
87 zone.getRawOffset() / (60*60*1000.0) +
88 "; HOUR = " + cal.get(Calendar.HOUR));
90 cal.set(1900, 15, 5, 5, 8, 13);
91 if (cal.get(Calendar.HOUR) != 5) {
92 logln("Fail: " + zone.getID() + " " +
93 zone.useDaylightTime() + "; DST_OFFSET = " +
94 cal.get(Calendar.DST_OFFSET) / (60*60*1000.0) + "; ZONE_OFFSET = " +
95 cal.get(Calendar.ZONE_OFFSET) / (60*60*1000.0) + "; getRawOffset() = " +
96 zone.getRawOffset() / (60*60*1000.0) +
97 "; HOUR = " + cal.get(Calendar.HOUR));
99 cal.set(1900, 15, 5, 5, 8, 13);
100 logln("ms = " + cal.getTime() + " (" + cal.getTime().getTime() + ")");
101 cal.get(Calendar.HOUR);
102 java.util.GregorianCalendar cal2 = new java.util.GregorianCalendar(java.util.TimeZone.getTimeZone(ids[i]));
104 cal2.set(1900, 15, 5, 5, 8, 13);
105 cal2.get(Calendar.HOUR);
106 logln("java.util.GC: " + zone.getID() + " " +
107 zone.useDaylightTime() + "; DST_OFFSET = " +
108 cal2.get(Calendar.DST_OFFSET) / (60*60*1000.0) + "; ZONE_OFFSET = " +
109 cal2.get(Calendar.ZONE_OFFSET) / (60*60*1000.0) + "; getRawOffset() = " +
110 zone.getRawOffset() / (60*60*1000.0) +
111 "; HOUR = " + cal.get(Calendar.HOUR));
112 logln("ms = " + cal2.getTime() + " (" + cal2.getTime().getTime() + ")");
114 } else if (false) { // Change to true to debug
115 logln("OK: " + zone.getID() + " " +
116 zone.useDaylightTime() + " " +
117 cal.get(Calendar.DST_OFFSET) / (60*60*1000) + " " +
118 zone.getRawOffset() / (60*60*1000) +
119 ": HOUR = " + cal.get(Calendar.HOUR));
122 if (bad) errln("TimeZone problems with GC");
124 } catch (MissingResourceException e) {
125 warnln("Could not load data. "+ e.getMessage());
129 public void Test4035301() {
132 GregorianCalendar c = new GregorianCalendar(98, 8, 7);
133 GregorianCalendar d = new GregorianCalendar(98, 8, 7);
141 } catch (Exception e) {
142 // TODO Auto-generated catch block
143 warnln("Could not load data. "+ e.getMessage());
147 public void Test4040996() {
149 String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
150 SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
151 pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
152 pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
153 Calendar calendar = new GregorianCalendar(pdt);
155 calendar.set(Calendar.MONTH,3);
156 calendar.set(Calendar.DAY_OF_MONTH,18);
157 calendar.set(Calendar.SECOND, 30);
159 logln("MONTH: " + calendar.get(Calendar.MONTH));
160 logln("DAY_OF_MONTH: " +
161 calendar.get(Calendar.DAY_OF_MONTH));
162 logln("MINUTE: " + calendar.get(Calendar.MINUTE));
163 logln("SECOND: " + calendar.get(Calendar.SECOND));
165 calendar.add(Calendar.SECOND,6);
166 //This will print out todays date for MONTH and DAY_OF_MONTH
167 //instead of the date it was set to.
168 //This happens when adding MILLISECOND or MINUTE also
169 logln("MONTH: " + calendar.get(Calendar.MONTH));
170 logln("DAY_OF_MONTH: " +
171 calendar.get(Calendar.DAY_OF_MONTH));
172 logln("MINUTE: " + calendar.get(Calendar.MINUTE));
173 logln("SECOND: " + calendar.get(Calendar.SECOND));
174 if (calendar.get(Calendar.MONTH) != 3 ||
175 calendar.get(Calendar.DAY_OF_MONTH) != 18 ||
176 calendar.get(Calendar.SECOND) != 36)
177 errln("Fail: Calendar.add misbehaves");
178 } catch (Exception e) {
179 warnln("Could not load data. "+ e.getMessage());
183 public void Test4051765() {
185 Calendar cal = Calendar.getInstance();
186 cal.setLenient(false);
187 cal.set(Calendar.DAY_OF_WEEK, 0);
190 errln("Fail: DAY_OF_WEEK 0 should be disallowed");
192 catch (IllegalArgumentException e) {
195 } catch (Exception e) {
196 // TODO Auto-generated catch block
197 warnln("Could not load data. "+ e.getMessage());
202 * User error - no bug here public void Test4059524() { // Create calendar
203 * for April 10, 1997 GregorianCalendar calendar = new GregorianCalendar(); //
204 * print out a bunch of interesting things logln("ERA: " +
205 * calendar.get(calendar.ERA)); logln("YEAR: " +
206 * calendar.get(calendar.YEAR)); logln("MONTH: " +
207 * calendar.get(calendar.MONTH)); logln("WEEK_OF_YEAR: " +
208 * calendar.get(calendar.WEEK_OF_YEAR)); logln("WEEK_OF_MONTH: " +
209 * calendar.get(calendar.WEEK_OF_MONTH)); logln("DATE: " +
210 * calendar.get(calendar.DATE)); logln("DAY_OF_MONTH: " +
211 * calendar.get(calendar.DAY_OF_MONTH)); logln("DAY_OF_YEAR: " +
212 * calendar.get(calendar.DAY_OF_YEAR)); logln("DAY_OF_WEEK: " +
213 * calendar.get(calendar.DAY_OF_WEEK)); logln("DAY_OF_WEEK_IN_MONTH: " +
214 * calendar.get(calendar.DAY_OF_WEEK_IN_MONTH)); logln("AM_PM: " +
215 * calendar.get(calendar.AM_PM)); logln("HOUR: " +
216 * calendar.get(calendar.HOUR)); logln("HOUR_OF_DAY: " +
217 * calendar.get(calendar.HOUR_OF_DAY)); logln("MINUTE: " +
218 * calendar.get(calendar.MINUTE)); logln("SECOND: " +
219 * calendar.get(calendar.SECOND)); logln("MILLISECOND: " +
220 * calendar.get(calendar.MILLISECOND)); logln("ZONE_OFFSET: " +
221 * (calendar.get(calendar.ZONE_OFFSET)/(60*60*1000))); logln("DST_OFFSET: " +
222 * (calendar.get(calendar.DST_OFFSET)/(60*60*1000))); calendar = new
223 * GregorianCalendar(1997,3,10); calendar.getTime(); logln("April 10,
224 * 1997"); logln("ERA: " + calendar.get(calendar.ERA)); logln("YEAR: " +
225 * calendar.get(calendar.YEAR)); logln("MONTH: " +
226 * calendar.get(calendar.MONTH)); logln("WEEK_OF_YEAR: " +
227 * calendar.get(calendar.WEEK_OF_YEAR)); logln("WEEK_OF_MONTH: " +
228 * calendar.get(calendar.WEEK_OF_MONTH)); logln("DATE: " +
229 * calendar.get(calendar.DATE)); logln("DAY_OF_MONTH: " +
230 * calendar.get(calendar.DAY_OF_MONTH)); logln("DAY_OF_YEAR: " +
231 * calendar.get(calendar.DAY_OF_YEAR)); logln("DAY_OF_WEEK: " +
232 * calendar.get(calendar.DAY_OF_WEEK)); logln("DAY_OF_WEEK_IN_MONTH: " +
233 * calendar.get(calendar.DAY_OF_WEEK_IN_MONTH)); logln("AM_PM: " +
234 * calendar.get(calendar.AM_PM)); logln("HOUR: " +
235 * calendar.get(calendar.HOUR)); logln("HOUR_OF_DAY: " +
236 * calendar.get(calendar.HOUR_OF_DAY)); logln("MINUTE: " +
237 * calendar.get(calendar.MINUTE)); logln("SECOND: " +
238 * calendar.get(calendar.SECOND)); logln("MILLISECOND: " +
239 * calendar.get(calendar.MILLISECOND)); logln("ZONE_OFFSET: " +
240 * (calendar.get(calendar.ZONE_OFFSET)/(60*60*1000))); // in hours
241 * logln("DST_OFFSET: " + (calendar.get(calendar.DST_OFFSET)/(60*60*1000))); //
245 public void Test4059654() {
247 // work around bug for jdk1.4 on solaris 2.6, which uses funky
249 // jdk1.4.1 will drop support for 2.6 so we should be ok when it
251 java.util.TimeZone javazone = java.util.TimeZone.getTimeZone("GMT");
252 TimeZone icuzone = TimeZone.getTimeZone("GMT");
254 GregorianCalendar gc = new GregorianCalendar(icuzone);
256 gc.set(1997, 3, 1, 15, 16, 17); // April 1, 1997
258 gc.set(Calendar.HOUR, 0);
259 gc.set(Calendar.AM_PM, Calendar.AM);
260 gc.set(Calendar.MINUTE, 0);
261 gc.set(Calendar.SECOND, 0);
262 gc.set(Calendar.MILLISECOND, 0);
264 Date cd = gc.getTime();
265 java.util.Calendar cal = java.util.Calendar.getInstance(javazone);
267 cal.set(1997, 3, 1, 0, 0, 0);
268 Date exp = cal.getTime();
270 errln("Fail: Calendar.set broken. Got " + cd + " Want " + exp);
271 // } catch (RuntimeException e) {
272 // TODO Auto-generated catch block
273 // e.printStackTrace();
277 public void Test4061476() {
278 SimpleDateFormat fmt = new SimpleDateFormat("ddMMMyy", Locale.UK);
279 Calendar cal = GregorianCalendar.getInstance(TimeZone.getTimeZone("GMT"),
281 fmt.setCalendar(cal);
284 Date date = fmt.parse("29MAY97");
287 catch (Exception e) {
288 System.out.print("");
290 cal.set(Calendar.HOUR_OF_DAY, 13);
291 logln("Hour: "+cal.get(Calendar.HOUR_OF_DAY));
292 cal.add(Calendar.HOUR_OF_DAY, 6);
293 logln("Hour: "+cal.get(Calendar.HOUR_OF_DAY));
294 if (cal.get(Calendar.HOUR_OF_DAY) != 19)
295 errln("Fail: Want 19 Got " + cal.get(Calendar.HOUR_OF_DAY));
298 public void Test4070502() {
299 java.util.Calendar tempcal = java.util.Calendar.getInstance();
301 tempcal.set(1998, 0, 30);
302 Date d = getAssociatedDate(tempcal.getTime());
303 Calendar cal = new GregorianCalendar();
305 if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY ||
306 cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
307 errln("Fail: Want weekday Got " + d);
311 * Get the associated date starting from a specified date NOTE: the
312 * unnecessary "getTime()'s" below are a work-around for a bug in jdk 1.1.3
313 * (and probably earlier versions also)
317 * The date to start from
319 public static Date getAssociatedDate(Date d) {
320 GregorianCalendar cal = new GregorianCalendar();
322 //cal.add(field, amount); //<-- PROBLEM SEEN WITH field = DATE,MONTH
323 // cal.getTime(); // <--- REMOVE THIS TO SEE BUG
325 int wd = cal.get(Calendar.DAY_OF_WEEK);
326 if (wd == Calendar.SATURDAY || wd == Calendar.SUNDAY) {
327 cal.add(Calendar.DATE, 1);
333 return cal.getTime();
336 public void Test4071197() {
341 void dowTest(boolean lenient) {
342 GregorianCalendar cal = new GregorianCalendar();
343 cal.set(1997, Calendar.AUGUST, 12); // Wednesday
344 // cal.getTime(); // Force update
345 cal.setLenient(lenient);
346 cal.set(1996, Calendar.DECEMBER, 1); // Set the date to be December 1,
348 int dow = cal.get(Calendar.DAY_OF_WEEK);
349 int min = cal.getMinimum(Calendar.DAY_OF_WEEK);
350 int max = cal.getMaximum(Calendar.DAY_OF_WEEK);
351 logln(cal.getTime().toString());
352 if (min != Calendar.SUNDAY || max != Calendar.SATURDAY)
353 errln("FAIL: Min/max bad");
354 if (dow < min || dow > max)
355 errln("FAIL: Day of week " + dow + " out of range");
356 if (dow != Calendar.SUNDAY)
357 errln("FAIL: Day of week should be SUNDAY Got " + dow);
360 public void Test4071385() {
361 // work around bug for jdk1.4 on solaris 2.6, which uses funky timezone
363 // jdk1.4.1 will drop support for 2.6 so we should be ok when it comes out
364 java.util.TimeZone javazone = java.util.TimeZone.getTimeZone("GMT");
365 TimeZone icuzone = TimeZone.getTimeZone("GMT");
367 Calendar cal = Calendar.getInstance(icuzone);
368 java.util.Calendar tempcal = java.util.Calendar.getInstance(javazone);
370 tempcal.set(1998, Calendar.JUNE, 24);
371 cal.setTime(tempcal.getTime());
372 cal.set(Calendar.MONTH, Calendar.NOVEMBER); // change a field
373 logln(cal.getTime().toString());
374 tempcal.set(1998, Calendar.NOVEMBER, 24);
375 if (!cal.getTime().equals(tempcal.getTime()))
379 public void Test4073929() {
380 GregorianCalendar foo1 = new GregorianCalendar(1997, 8, 27);
381 foo1.add(Calendar.DAY_OF_MONTH, +1);
382 int testyear = foo1.get(Calendar.YEAR);
383 int testmonth = foo1.get(Calendar.MONTH);
384 int testday = foo1.get(Calendar.DAY_OF_MONTH);
385 if (testyear != 1997 ||
388 errln("Fail: Calendar not initialized");
391 public void Test4083167() {
392 TimeZone saveZone = TimeZone.getDefault();
394 TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
395 Date firstDate = new Date();
396 Calendar cal = new GregorianCalendar();
397 cal.setTime(firstDate);
398 long firstMillisInDay = cal.get(Calendar.HOUR_OF_DAY) * 3600000L +
399 cal.get(Calendar.MINUTE) * 60000L +
400 cal.get(Calendar.SECOND) * 1000L +
401 cal.get(Calendar.MILLISECOND);
403 logln("Current time: " + firstDate.toString());
405 for (int validity=0; validity<30; validity++) {
406 Date lastDate = new Date(firstDate.getTime() +
407 (long)validity*1000*24*60*60);
408 cal.setTime(lastDate);
409 long millisInDay = cal.get(Calendar.HOUR_OF_DAY) * 3600000L +
410 cal.get(Calendar.MINUTE) * 60000L +
411 cal.get(Calendar.SECOND) * 1000L +
412 cal.get(Calendar.MILLISECOND);
413 if (firstMillisInDay != millisInDay)
414 errln("Day has shifted " + lastDate);
418 TimeZone.setDefault(saveZone);
422 public void Test4086724() {
423 SimpleDateFormat date;
424 TimeZone saveZone = TimeZone.getDefault();
425 Locale saveLocale = Locale.getDefault();
427 Locale.setDefault(Locale.UK);
428 TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
429 date=new SimpleDateFormat("dd MMM yyy (zzzz) 'is in week' ww");
430 Calendar cal=Calendar.getInstance();
431 cal.set(1997,Calendar.SEPTEMBER,30);
432 Date now=cal.getTime();
433 logln(date.format(now));
434 cal.set(1997,Calendar.JANUARY,1);
436 logln(date.format(now));
437 cal.set(1997,Calendar.JANUARY,8);
439 logln(date.format(now));
440 cal.set(1996,Calendar.DECEMBER,31);
442 logln(date.format(now));
445 Locale.setDefault(saveLocale);
446 TimeZone.setDefault(saveZone);
448 logln("*** THE RESULTS OF THIS TEST MUST BE VERIFIED MANUALLY ***");
451 public void Test4092362() {
452 GregorianCalendar cal1 = new GregorianCalendar(1997, 10, 11, 10, 20, 40);
454 * cal1.set( Calendar.YEAR, 1997 ); cal1.set( Calendar.MONTH, 10 );
455 * cal1.set( Calendar.DATE, 11 ); cal1.set( Calendar.HOUR, 10 );
456 * cal1.set( Calendar.MINUTE, 20 ); cal1.set( Calendar.SECOND, 40 );
459 logln( " Cal1 = " + cal1.getTime().getTime() );
460 logln( " Cal1 time in ms = " + cal1.get(Calendar.MILLISECOND) );
461 for( int k = 0; k < 100 ; k++ ) {
462 System.out.print("");
465 GregorianCalendar cal2 = new GregorianCalendar(1997, 10, 11, 10, 20, 40);
467 * cal2.set( Calendar.YEAR, 1997 ); cal2.set( Calendar.MONTH, 10 );
468 * cal2.set( Calendar.DATE, 11 ); cal2.set( Calendar.HOUR, 10 );
469 * cal2.set( Calendar.MINUTE, 20 ); cal2.set( Calendar.SECOND, 40 );
472 logln( " Cal2 = " + cal2.getTime().getTime() );
473 logln( " Cal2 time in ms = " + cal2.get(Calendar.MILLISECOND) );
474 if( !cal1.equals( cal2 ) )
475 errln("Fail: Milliseconds randomized");
478 public void Test4095407() {
479 GregorianCalendar a = new GregorianCalendar(1997,Calendar.NOVEMBER, 13);
480 int dow = a.get(Calendar.DAY_OF_WEEK);
481 if (dow != Calendar.THURSDAY)
482 errln("Fail: Want THURSDAY Got " + dow);
485 public void Test4096231() {
486 TimeZone GMT = TimeZone.getTimeZone("GMT");
487 TimeZone PST = TimeZone.getTimeZone("PST");
488 int sec = 0, min = 0, hr = 0, day = 1, month = 10, year = 1997;
490 Calendar cal1 = new GregorianCalendar(PST);
491 cal1.setTime(new Date(880698639000L));
493 logln("PST 1 is: " + (p=cal1.get(Calendar.HOUR_OF_DAY)));
494 cal1.setTimeZone(GMT);
495 // Issue 1: Changing the timezone doesn't change the
498 logln("GMT 1 is: " + (h1=cal1.get(Calendar.HOUR_OF_DAY)));
499 cal1.setTime(new Date(880698639000L));
500 logln("GMT 2 is: " + (h2=cal1.get(Calendar.HOUR_OF_DAY)));
501 // Note: This test had a bug in it. It wanted h1!=h2, when
502 // what was meant was h1!=p. Fixed this concurrent with fix
504 if (p == h1 || h1 != h2)
505 errln("Fail: Hour same in different zones");
507 Calendar cal2 = new GregorianCalendar(GMT);
508 Calendar cal3 = new GregorianCalendar(PST);
509 cal2.set(Calendar.MILLISECOND, 0);
510 cal3.set(Calendar.MILLISECOND, 0);
512 cal2.set(cal1.get(Calendar.YEAR),
513 cal1.get(Calendar.MONTH),
514 cal1.get(Calendar.DAY_OF_MONTH),
515 cal1.get(Calendar.HOUR_OF_DAY),
516 cal1.get(Calendar.MINUTE),
517 cal1.get(Calendar.SECOND));
520 logln("RGMT 1 is: " + (t1=cal2.getTime().getTime()));
521 cal3.set(year, month, day, hr, min, sec);
522 logln("RPST 1 is: " + (t2=cal3.getTime().getTime()));
523 cal3.setTimeZone(GMT);
524 logln("RGMT 2 is: " + (t3=cal3.getTime().getTime()));
525 cal3.set(cal1.get(Calendar.YEAR),
526 cal1.get(Calendar.MONTH),
527 cal1.get(Calendar.DAY_OF_MONTH),
528 cal1.get(Calendar.HOUR_OF_DAY),
529 cal1.get(Calendar.MINUTE),
530 cal1.get(Calendar.SECOND));
531 // Issue 2: Calendar continues to use the timezone in its
532 // constructor for set() conversions, regardless
533 // of calls to setTimeZone()
534 logln("RGMT 3 is: " + (t4=cal3.getTime().getTime()));
538 errln("Fail: Calendar zone behavior faulty");
541 public void Test4096539() {
542 int[] y = {31,28,31,30,31,30,31,31,30,31,30,31};
544 for (int x=0;x<12;x++) {
545 GregorianCalendar gc = new
546 GregorianCalendar(1997,x,y[x]);
548 log((m1=gc.get(Calendar.MONTH)+1)+"/"+
549 gc.get(Calendar.DATE)+"/"+gc.get(Calendar.YEAR)+
552 gc.add(Calendar.MONTH, 1);
553 logln((m2=gc.get(Calendar.MONTH)+1)+"/"+
554 gc.get(Calendar.DATE)+"/"+gc.get(Calendar.YEAR)
556 int m = (m1 % 12) + 1;
558 errln("Fail: Want " + m + " Got " + m2);
563 public void Test4100311() {
564 GregorianCalendar cal = (GregorianCalendar)Calendar.getInstance();
565 cal.set(Calendar.YEAR, 1997);
566 cal.set(Calendar.DAY_OF_YEAR, 1);
567 Date d = cal.getTime(); // Should be Jan 1
569 if (cal.get(Calendar.DAY_OF_YEAR) != 1)
570 errln("Fail: DAY_OF_YEAR not set");
573 public void Test4103271() {
574 SimpleDateFormat sdf = new SimpleDateFormat();
575 int numYears=40, startYear=1997, numDays=15;
576 String output, testDesc;
577 GregorianCalendar testCal = (GregorianCalendar)Calendar.getInstance();
579 sdf.setCalendar(testCal);
580 sdf.applyPattern("d MMM yyyy");
581 boolean fail = false;
582 for (int firstDay=1; firstDay<=2; firstDay++) {
583 for (int minDays=1; minDays<=7; minDays++) {
584 testCal.setMinimalDaysInFirstWeek(minDays);
585 testCal.setFirstDayOfWeek(firstDay);
586 testDesc = ("Test" + String.valueOf(firstDay) + String.valueOf(minDays));
587 logln(testDesc + " => 1st day of week=" +
588 String.valueOf(firstDay) +
589 ", minimum days in first week=" +
590 String.valueOf(minDays));
591 for (int j=startYear; j<=startYear+numYears; j++) {
592 testCal.set(j,11,25);
593 for(int i=0; i<numDays; i++) {
594 testCal.add(Calendar.DATE,1);
596 int actWOY = testCal.get(Calendar.WEEK_OF_YEAR);
597 if (actWOY < 1 || actWOY > 53) {
598 Date d = testCal.getTime();
599 calWOY = String.valueOf(actWOY);
600 output = testDesc + " - " + sdf.format(d) + "\t";
601 output = output + "\t" + calWOY;
611 3, 52, 52, 52, 52, 52, 52, 52,
614 4, 52, 52, 52, 52, 52, 52, 52,
615 53, 53, 53, 53, 53, 53, 53,
618 testCal.setFirstDayOfWeek(Calendar.SUNDAY);
619 for (int j=0; j<DATA.length; j+=22) {
620 logln("Minimal days in first week = " + DATA[j] +
621 " Week starts on Sunday");
622 testCal.setMinimalDaysInFirstWeek(DATA[j]);
623 testCal.set(1997, Calendar.DECEMBER, 21);
624 for (int i=0; i<21; ++i) {
625 int woy = testCal.get(Calendar.WEEK_OF_YEAR);
626 log(testCal.getTime() + " " + woy);
627 if (woy != DATA[j + 1 + i]) {
633 // Now compute the time from the fields, and make sure we
634 // get the same answer back. This is a round-trip test.
635 Date save = testCal.getTime();
637 testCal.set(Calendar.YEAR, DATA[j+1+i] < 25 ? 1998 : 1997);
638 testCal.set(Calendar.WEEK_OF_YEAR, DATA[j+1+i]);
639 testCal.set(Calendar.DAY_OF_WEEK, (i%7) + Calendar.SUNDAY);
640 if (!testCal.getTime().equals(save)) {
641 logln(" Parse failed: " + testCal.getTime());
645 testCal.setTime(save);
646 testCal.add(Calendar.DAY_OF_MONTH, 1);
650 Date d[] = new Date[8];
651 java.util.Calendar tempcal = java.util.Calendar.getInstance();
653 tempcal.set(1997, Calendar.DECEMBER, 28);
654 d[0] = tempcal.getTime();
655 tempcal.set(1998, Calendar.JANUARY, 10);
656 d[1] = tempcal.getTime();
657 tempcal.set(1998, Calendar.DECEMBER, 31);
658 d[2] = tempcal.getTime();
659 tempcal.set(1999, Calendar.JANUARY, 1);
660 d[3] = tempcal.getTime();
661 // Test field disambiguation with a few special hard-coded cases.
662 // This shouldn't fail if the above cases aren't failing.
664 new Integer(1998), new Integer(1), new Integer(Calendar.SUNDAY),
666 new Integer(1998), new Integer(2), new Integer(Calendar.SATURDAY),
668 new Integer(1998), new Integer(53), new Integer(Calendar.THURSDAY),
670 new Integer(1998), new Integer(53), new Integer(Calendar.FRIDAY),
673 testCal.setMinimalDaysInFirstWeek(3);
674 testCal.setFirstDayOfWeek(Calendar.SUNDAY);
675 for (int i=0; i<DISAM.length; i+=4) {
676 int y = ((Integer)DISAM[i]).intValue();
677 int woy = ((Integer)DISAM[i+1]).intValue();
678 int dow = ((Integer)DISAM[i+2]).intValue();
679 Date exp = (Date)DISAM[i+3];
681 testCal.set(Calendar.YEAR, y);
682 testCal.set(Calendar.WEEK_OF_YEAR, woy);
683 testCal.set(Calendar.DAY_OF_WEEK, dow);
685 "-DOW" + dow + " expect:" + exp +
686 " got:" + testCal.getTime());
687 if (!testCal.getTime().equals(exp)) {
694 // Now try adding and rolling
695 Object ADD = new Object();
696 Object ROLL = new Object();
697 tempcal.set(1998, Calendar.DECEMBER, 25);
698 d[0] = tempcal.getTime();
699 tempcal.set(1999, Calendar.JANUARY, 1);
700 d[1] = tempcal.getTime();
701 tempcal.set(1997, Calendar.DECEMBER, 28);
702 d[2] = tempcal.getTime();
703 tempcal.set(1998, Calendar.JANUARY, 4);
704 d[3] = tempcal.getTime();
705 tempcal.set(1998, Calendar.DECEMBER, 27);
706 d[4] = tempcal.getTime();
707 tempcal.set(1997, Calendar.DECEMBER, 28);
708 d[5] = tempcal.getTime();
709 tempcal.set(1999, Calendar.JANUARY, 2);
710 d[6] = tempcal.getTime();
711 tempcal.set(1998, Calendar.JANUARY, 3);
712 d[7] = tempcal.getTime();
715 ADD, new Integer(1), d[0], d[1],
716 ADD, new Integer(1), d[2], d[3],
717 ROLL, new Integer(1), d[4], d[5],
718 ROLL, new Integer(1), d[6], d[7],
720 testCal.setMinimalDaysInFirstWeek(3);
721 testCal.setFirstDayOfWeek(Calendar.SUNDAY);
722 for (int i=0; i<ADDROLL.length; i+=4) {
723 int amount = ((Integer)ADDROLL[i+1]).intValue();
724 Date before = (Date)ADDROLL[i+2];
725 Date after = (Date)ADDROLL[i+3];
727 testCal.setTime(before);
728 if (ADDROLL[i] == ADD) testCal.add(Calendar.WEEK_OF_YEAR, amount);
729 else testCal.roll(Calendar.WEEK_OF_YEAR, amount);
730 log((ADDROLL[i]==ADD?"add(WOY,":"roll(WOY,") +
731 amount + ") " + before + " => " +
733 if (!after.equals(testCal.getTime())) {
734 logln(" exp:" + after + " FAIL");
739 testCal.setTime(after);
740 if (ADDROLL[i] == ADD) testCal.add(Calendar.WEEK_OF_YEAR, -amount);
741 else testCal.roll(Calendar.WEEK_OF_YEAR, -amount);
742 log((ADDROLL[i]==ADD?"add(WOY,":"roll(WOY,") +
743 (-amount) + ") " + after + " => " +
745 if (!before.equals(testCal.getTime())) {
746 logln(" exp:" + before + " FAIL");
752 if (fail) errln("Fail: Week of year misbehaving");
755 public void Test4106136() {
756 Locale saveLocale = Locale.getDefault();
757 String[] names = { "Calendar", "DateFormat", "NumberFormat" };
759 Locale[] locales = { Locale.CHINESE, Locale.CHINA };
760 for (int i=0; i<locales.length; ++i) {
761 Locale.setDefault(locales[i]);
763 Calendar.getAvailableLocales().length,
764 DateFormat.getAvailableLocales().length,
765 NumberFormat.getAvailableLocales().length
767 for (int j=0; j<n.length; ++j) {
769 errln("Fail: " + names[j] + " has no locales for " + locales[i]);
774 Locale.setDefault(saveLocale);
778 public void Test4108764() {
779 java.util.Calendar tempcal = java.util.Calendar.getInstance();
781 tempcal.set(1997, Calendar.FEBRUARY, 15, 12, 00, 00);
782 Date d00 = tempcal.getTime();
783 tempcal.set(1997, Calendar.FEBRUARY, 15, 12, 00, 56);
784 Date d01 = tempcal.getTime();
785 tempcal.set(1997, Calendar.FEBRUARY, 15, 12, 34, 00);
786 Date d10 = tempcal.getTime();
787 tempcal.set(1997, Calendar.FEBRUARY, 15, 12, 34, 56);
788 Date d11 = tempcal.getTime();
789 tempcal.set(1997, Calendar.JANUARY, 15, 12, 34, 56);
790 Date dM = tempcal.getTime();
792 tempcal.set(1970, Calendar.JANUARY, 1);
793 Date epoch = tempcal.getTime();
795 Calendar cal = Calendar.getInstance();
798 cal.clear( Calendar.MINUTE );
799 logln(cal.getTime().toString());
800 if (!cal.getTime().equals(d01)) {
801 errln("Fail: " + d11 + " clear(MINUTE) => expect " +
802 d01 + ", got " + cal.getTime());
805 cal.set( Calendar.SECOND, 0 );
806 logln(cal.getTime().toString());
807 if (!cal.getTime().equals(d00))
808 errln("Fail: set(SECOND, 0) broken");
811 cal.set( Calendar.SECOND, 0 );
812 logln(cal.getTime().toString());
813 if (!cal.getTime().equals(d10))
814 errln("Fail: set(SECOND, 0) broken #2");
816 cal.clear( Calendar.MINUTE );
817 logln(cal.getTime().toString());
818 if (!cal.getTime().equals(d00))
819 errln("Fail: clear(MINUTE) broken #2");
822 logln(cal.getTime().toString());
823 if (!cal.getTime().equals(epoch))
824 errln("Fail: after clear() expect " + epoch + ", got " + cal.getTime());
826 //#if defined(FOUNDATION10) || defined(J2SE13)
827 //## // This test case does not work well with JRE1.3 with
828 //## // the timezone update for US 2007 rule. Java 1.3 only
829 //## // supports single DST rule for all years. March 15
830 //## // was not in DST before, but with the new rule, it is
834 cal.clear( Calendar.MONTH );
835 logln(cal.getTime().toString());
836 if (!cal.getTime().equals(dM)) {
837 errln("Fail: " + d11 + " clear(MONTH) => expect " +
838 dM + ", got " + cal.getTime());
843 public void Test4114578() {
844 int ONE_HOUR = 60*60*1000;
845 Calendar cal = Calendar.getInstance();
846 cal.setTimeZone(TimeZone.getTimeZone("PST"));
848 java.util.Calendar tempcal = java.util.Calendar.getInstance();
850 tempcal.set(1998, Calendar.APRIL, 5, 1, 0);
851 long onset = tempcal.getTime().getTime() + ONE_HOUR;
852 tempcal.set(1998, Calendar.OCTOBER, 25, 0, 0);
853 long cease = tempcal.getTime().getTime() + 2*ONE_HOUR;
855 boolean fail = false;
861 // Start Action Amt Expected_change
862 onset - ONE_HOUR, ADD, 1, ONE_HOUR,
863 onset, ADD, -1, -ONE_HOUR,
864 onset - ONE_HOUR, ROLL, 1, ONE_HOUR,
865 onset, ROLL, -1, -ONE_HOUR,
866 cease - ONE_HOUR, ADD, 1, ONE_HOUR,
867 cease, ADD, -1, -ONE_HOUR,
868 cease - ONE_HOUR, ROLL, 1, ONE_HOUR,
869 cease, ROLL, -1, -ONE_HOUR,
872 for (int i=0; i<DATA.length; i+=4) {
873 Date date = new Date(DATA[i]);
874 int amt = (int) DATA[i+2];
875 long expectedChange = DATA[i+3];
877 log(date.toString());
880 switch ((int) DATA[i+1]) {
882 log(" add (HOUR," + (amt<0?"":"+")+amt + ")= ");
883 cal.add(Calendar.HOUR, amt);
886 log(" roll(HOUR," + (amt<0?"":"+")+amt + ")= ");
887 cal.roll(Calendar.HOUR, amt);
891 log(cal.getTime().toString());
893 long change = cal.getTime().getTime() - date.getTime();
894 if (change != expectedChange) {
901 if (fail) errln("Fail: roll/add misbehaves around DST onset/cease");
905 * Make sure maximum for HOUR field is 11, not 12.
907 public void Test4118384() {
908 Calendar cal = Calendar.getInstance();
909 if (cal.getMaximum(Calendar.HOUR) != 11 ||
910 cal.getLeastMaximum(Calendar.HOUR) != 11 ||
911 cal.getActualMaximum(Calendar.HOUR) != 11)
912 errln("Fail: maximum of HOUR field should be 11");
916 * Check isLeapYear for BC years.
918 public void Test4125881() {
919 GregorianCalendar cal = (GregorianCalendar) Calendar.getInstance();
920 DateFormat fmt = new SimpleDateFormat("MMMM d, yyyy G");
922 for (int y=-20; y<=10; ++y) {
923 cal.set(Calendar.ERA, y < 1 ? GregorianCalendar.BC : GregorianCalendar.AD);
924 cal.set(Calendar.YEAR, y < 1 ? 1 - y : y);
925 logln(y + " = " + fmt.format(cal.getTime()) + " " +
927 if (cal.isLeapYear(y) != ((y+40)%4 == 0))
928 errln("Leap years broken");
932 // I am disabling this test -- it is currently failing because of a bug
933 // in Sun's latest change to STZ.getOffset(). I have filed a Sun bug
934 // against this problem.
936 // Re-enabled after 'porting' TZ and STZ from java.util to com.ibm.icu.util.
938 * Prove that GregorianCalendar is proleptic (it used to cut off at 45 BC,
939 * and not have leap years before then).
941 public void Test4125892() {
942 GregorianCalendar cal = (GregorianCalendar) Calendar.getInstance();
943 //DateFormat fmt = new SimpleDateFormat("MMMM d, yyyy G");
946 cal.set(Calendar.ERA, GregorianCalendar.BC);
947 cal.set(Calendar.YEAR, 81); // 81 BC is a leap year (proleptically)
948 cal.set(Calendar.MONTH, Calendar.FEBRUARY);
949 cal.set(Calendar.DATE, 28);
950 cal.add(Calendar.DATE, 1);
951 if (cal.get(Calendar.DATE) != 29 ||
952 !cal.isLeapYear(-80)) // -80 == 81 BC
953 errln("Calendar not proleptic");
957 * Calendar and GregorianCalendar hashCode() methods need improvement.
958 * Calendar needs a good implementation that subclasses can override, and
959 * GregorianCalendar should use that implementation.
961 public void Test4136399() {
963 * Note: This test is actually more strict than it has to be.
964 * Technically, there is no requirement that unequal objects have
965 * unequal hashes. We only require equal objects to have equal hashes.
966 * It is desirable for unequal objects to have distributed hashes, but
967 * there is no hard requirement here.
969 * In this test we make assumptions about certain attributes of calendar
970 * objects getting represented in the hash, which need not always be the
971 * case (although it does work currently with the given test).
973 Calendar a = Calendar.getInstance();
974 Calendar b = (Calendar)a.clone();
975 if (a.hashCode() != b.hashCode()) {
976 errln("Calendar hash code unequal for cloned objects");
978 TimeZone atz1 = a.getTimeZone();
979 TimeZone atz2 = (TimeZone)atz1.clone();
980 if(!atz1.equals(atz2)){
981 errln("The clone timezones are not equal");
983 if(atz1.hashCode()!=atz2.hashCode()){
984 errln("TimeZone hash code unequal for cloned objects");
986 b.setMinimalDaysInFirstWeek(7 - a.getMinimalDaysInFirstWeek());
987 if (a.hashCode() == b.hashCode()) {
988 errln("Calendar hash code ignores minimal days in first week");
990 b.setMinimalDaysInFirstWeek(a.getMinimalDaysInFirstWeek());
992 b.setFirstDayOfWeek((a.getFirstDayOfWeek() % 7) + 1); // Next day
993 if (a.hashCode() == b.hashCode()) {
994 errln("Calendar hash code ignores first day of week");
996 b.setFirstDayOfWeek(a.getFirstDayOfWeek());
998 b.setLenient(!a.isLenient());
999 if (a.hashCode() == b.hashCode()) {
1000 errln("Calendar hash code ignores lenient setting");
1002 b.setLenient(a.isLenient());
1004 // Assume getTimeZone() returns a reference, not a clone
1005 // of a reference -- this is true as of this writing
1006 TimeZone atz = a.getTimeZone();
1007 TimeZone btz = b.getTimeZone();
1009 btz.setRawOffset(atz.getRawOffset() + 60*60*1000);
1010 if(atz.hashCode()== btz.hashCode()){
1011 errln(atz.hashCode()+"=="+btz.hashCode());
1013 if (a.getTimeZone()!= b.getTimeZone() && a.hashCode() == b.hashCode()) {
1014 errln("Calendar hash code ignores zone");
1016 b.getTimeZone().setRawOffset(a.getTimeZone().getRawOffset());
1018 GregorianCalendar c = new GregorianCalendar();
1019 GregorianCalendar d = (GregorianCalendar)c.clone();
1020 if (c.hashCode() != d.hashCode()) {
1021 errln("GregorianCalendar hash code unequal for clones objects");
1023 Date cutover = c.getGregorianChange();
1024 d.setGregorianChange(new Date(cutover.getTime() + 24*60*60*1000));
1025 if (c.hashCode() == d.hashCode()) {
1026 errln("GregorianCalendar hash code ignores cutover");
1031 * GregorianCalendar.equals() ignores cutover date
1033 public void Test4141665() {
1034 GregorianCalendar cal = new GregorianCalendar();
1035 GregorianCalendar cal2 = (GregorianCalendar)cal.clone();
1036 Date cut = cal.getGregorianChange();
1037 Date cut2 = new Date(cut.getTime() + 100*24*60*60*1000L); // 100 days
1039 if (!cal.equals(cal2)) {
1040 errln("Cloned GregorianCalendars not equal");
1042 cal2.setGregorianChange(cut2);
1043 if (cal.equals(cal2)) {
1044 errln("GregorianCalendar.equals() ignores cutover");
1049 * Bug states that ArrayIndexOutOfBoundsException is thrown by
1050 * GregorianCalendar.roll() when IllegalArgumentException should be.
1052 public void Test4142933() {
1053 GregorianCalendar calendar = new GregorianCalendar();
1055 calendar.roll(-1, true);
1056 errln("Test failed, no exception trown");
1058 catch (IllegalArgumentException e) {
1060 // logln("Test passed");
1061 System.out.print("");
1063 catch (Exception e) {
1064 errln("Test failed. Unexpected exception is thrown: " + e);
1065 e.printStackTrace();
1070 * GregorianCalendar handling of Dates Long.MIN_VALUE and Long.MAX_VALUE is
1071 * confusing; unless the time zone has a raw offset of zero, one or the
1072 * other of these will wrap. We've modified the test given in the bug report
1073 * to therefore only check the behavior of a calendar with a zero raw offset
1076 public void Test4145158() {
1077 GregorianCalendar calendar = new GregorianCalendar();
1079 calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
1081 calendar.setTime(new Date(Long.MIN_VALUE));
1082 int year1 = calendar.get(Calendar.YEAR);
1083 int era1 = calendar.get(Calendar.ERA);
1085 calendar.setTime(new Date(Long.MAX_VALUE));
1086 int year2 = calendar.get(Calendar.YEAR);
1087 int era2 = calendar.get(Calendar.ERA);
1089 if (year1 == year2 && era1 == era2) {
1090 errln("Fail: Long.MIN_VALUE or Long.MAX_VALUE wrapping around");
1095 * Maximum value for YEAR field wrong.
1097 public void Test4145983() {
1098 GregorianCalendar calendar = new GregorianCalendar();
1099 calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
1100 Date[] DATES = { new Date(Long.MAX_VALUE), new Date(Long.MIN_VALUE) };
1101 for (int i=0; i<DATES.length; ++i) {
1102 calendar.setTime(DATES[i]);
1103 int year = calendar.get(Calendar.YEAR);
1104 int maxYear = calendar.getMaximum(Calendar.YEAR);
1105 if (year > maxYear) {
1106 errln("Failed for "+DATES[i].getTime()+" ms: year=" +
1107 year + ", maxYear=" + maxYear);
1113 * This is a bug in the validation code of GregorianCalendar. As reported,
1114 * the bug seems worse than it really is, due to a bug in the way the bug
1115 * report test was written. In reality the bug is restricted to the
1116 * DAY_OF_YEAR field. - liu 6/29/98
1118 public void Test4147269() {
1119 GregorianCalendar calendar = new GregorianCalendar();
1120 calendar.setLenient(false);
1121 java.util.Calendar tempcal = java.util.Calendar.getInstance();
1123 tempcal.set(1996, Calendar.JANUARY, 3); // Arbitrary date
1124 Date date = tempcal.getTime();
1125 for (int field = 0; field < calendar.getFieldCount(); field++) {
1126 calendar.setTime(date);
1127 // Note: In the bug report, getActualMaximum() was called instead
1128 // of getMaximum() -- this was an error. The validation code doesn't
1129 // use getActualMaximum(), since that's too costly.
1130 int max = calendar.getMaximum(field);
1132 calendar.set(field, value);
1134 calendar.getTime(); // Force time computation
1135 // We expect an exception to be thrown. If we fall through
1136 // to the next line, then we have a bug.
1137 errln("Test failed with field " + FIELD_NAME[field] +
1138 ", date before: " + date +
1139 ", date after: " + calendar.getTime() +
1140 ", value: " + value + " (max = " + max +")");
1141 } catch (IllegalArgumentException e) {
1142 System.out.print("");
1148 * Reported bug is that a GregorianCalendar with a cutover of
1149 * Date(Long.MAX_VALUE) doesn't behave as a pure Julian calendar. CANNOT
1150 * REPRODUCE THIS BUG
1152 public void Test4149677() {
1153 TimeZone[] zones = { TimeZone.getTimeZone("GMT"),
1154 TimeZone.getTimeZone("PST"),
1155 TimeZone.getTimeZone("EAT") };
1156 for (int i=0; i<zones.length; ++i) {
1157 GregorianCalendar calendar = new GregorianCalendar(zones[i]);
1159 // Make sure extreme values don't wrap around
1160 calendar.setTime(new Date(Long.MIN_VALUE));
1161 if (calendar.get(Calendar.ERA) != GregorianCalendar.BC) {
1162 errln("Fail: Long.MIN_VALUE ms has an AD year");
1164 calendar.setTime(new Date(Long.MAX_VALUE));
1165 if (calendar.get(Calendar.ERA) != GregorianCalendar.AD) {
1166 errln("Fail: Long.MAX_VALUE ms has a BC year");
1169 calendar.setGregorianChange(new Date(Long.MAX_VALUE));
1170 // to obtain a pure Julian calendar
1172 boolean is100Leap = calendar.isLeapYear(100);
1174 errln("test failed with zone " + zones[i].getID());
1175 errln(" cutover date is Calendar.MAX_DATE");
1176 errln(" isLeapYear(100) returns: " + is100Leap);
1182 * Calendar and Date HOUR broken. If HOUR is out-of-range, Calendar and Date
1183 * classes will misbehave.
1185 public void Test4162587() {
1186 TimeZone tz = TimeZone.getTimeZone("PST");
1187 TimeZone.setDefault(tz);
1188 GregorianCalendar cal = new GregorianCalendar(tz);
1191 for (int i=0; i<5; ++i) {
1192 if (i>0) logln("---");
1195 cal.set(1998, Calendar.APRIL, 5, i, 0);
1197 String s0 = d.toString();
1198 logln("0 " + i + ": " + s0);
1201 cal.set(1998, Calendar.APRIL, 4, i+24, 0);
1203 String sPlus = d.toString();
1204 logln("+ " + i + ": " + sPlus);
1207 cal.set(1998, Calendar.APRIL, 6, i-24, 0);
1209 String sMinus = d.toString();
1210 logln("- " + i + ": " + sMinus);
1212 if (!s0.equals(sPlus) || !s0.equals(sMinus)) {
1213 errln("Fail: All three lines must match");
1219 * Adding 12 months behaves differently from adding 1 year
1221 public void Test4165343() {
1222 GregorianCalendar calendar = new GregorianCalendar(1996, Calendar.FEBRUARY, 29);
1223 Date start = calendar.getTime();
1224 logln("init date: " + start);
1225 calendar.add(Calendar.MONTH, 12);
1226 Date date1 = calendar.getTime();
1227 logln("after adding 12 months: " + date1);
1228 calendar.setTime(start);
1229 calendar.add(Calendar.YEAR, 1);
1230 Date date2 = calendar.getTime();
1231 logln("after adding one year : " + date2);
1232 if (date1.equals(date2)) {
1233 logln("Test passed");
1235 errln("Test failed");
1240 * GregorianCalendar.getActualMaximum() does not account for first day of
1243 public void Test4166109() {
1247 * March 1998 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
1248 * 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
1250 boolean passed = true;
1251 int field = Calendar.WEEK_OF_MONTH;
1253 GregorianCalendar calendar = new GregorianCalendar(Locale.US);
1254 calendar.set(1998, Calendar.MARCH, 1);
1255 calendar.setMinimalDaysInFirstWeek(1);
1256 logln("Date: " + calendar.getTime());
1258 int firstInMonth = calendar.get(Calendar.DAY_OF_MONTH);
1260 for (int firstInWeek = Calendar.SUNDAY; firstInWeek <= Calendar.SATURDAY; firstInWeek++) {
1261 calendar.setFirstDayOfWeek(firstInWeek);
1262 int returned = calendar.getActualMaximum(field);
1263 int expected = (31 + ((firstInMonth - firstInWeek + 7)% 7) + 6) / 7;
1265 logln("First day of week = " + firstInWeek +
1266 " getActualMaximum(WEEK_OF_MONTH) = " + returned +
1267 " expected = " + expected +
1268 ((returned == expected) ? " ok" : " FAIL"));
1270 if (returned != expected) {
1275 errln("Test failed");
1280 * Calendar.getActualMaximum(YEAR) works wrong.
1282 public void Test4167060() {
1283 int field = Calendar.YEAR;
1284 DateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy G",
1287 GregorianCalendar calendars[] = {
1288 new GregorianCalendar(100, Calendar.NOVEMBER, 1),
1289 new GregorianCalendar(-99 /* 100BC */, Calendar.JANUARY, 1),
1290 new GregorianCalendar(1996, Calendar.FEBRUARY, 29),
1293 String[] id = { "Hybrid", "Gregorian", "Julian" };
1295 for (int k=0; k<3; ++k) {
1296 logln("--- " + id[k] + " ---");
1298 for (int j=0; j<calendars.length; ++j) {
1299 GregorianCalendar calendar = calendars[j];
1301 calendar.setGregorianChange(new Date(Long.MIN_VALUE));
1302 } else if (k == 2) {
1303 calendar.setGregorianChange(new Date(Long.MAX_VALUE));
1306 format.setCalendar((Calendar)calendar.clone());
1308 Date dateBefore = calendar.getTime();
1310 int maxYear = calendar.getActualMaximum(field);
1311 logln("maxYear: " + maxYear + " for " + format.format(calendar.getTime()));
1312 logln("date before: " + format.format(dateBefore));
1314 int years[] = {2000, maxYear-1, maxYear, maxYear+1};
1316 for (int i = 0; i < years.length; i++) {
1317 boolean valid = years[i] <= maxYear;
1318 calendar.set(field, years[i]);
1319 Date dateAfter = calendar.getTime();
1320 int newYear = calendar.get(field);
1321 calendar.setTime(dateBefore); // restore calendar for next
1324 logln(" Year " + years[i] + (valid? " ok " : " bad") +
1325 " => " + format.format(dateAfter));
1326 if (valid && newYear != years[i]) {
1327 errln(" FAIL: " + newYear + " should be valid; date, month and time shouldn't change");
1328 } else if (!valid && newYear == years[i]) {
1329 // We no longer require strict year maxima. That is, the
1331 // algorithm may work for values > the stated maximum.
1332 //errln(" FAIL: " + newYear + " should be invalid");
1333 logln(" Note: " + newYear + " > maximum, but still valid");
1341 * Calendar.roll broken This bug relies on the TimeZone bug 4173604 to also
1344 public void Test4173516() {
1345 int fieldsList[][] = {
1346 { 1997, Calendar.FEBRUARY, 1, 10, 45, 15, 900 },
1347 { 1999, Calendar.DECEMBER, 22, 23, 59, 59, 999 }
1350 GregorianCalendar cal = new GregorianCalendar();
1352 cal.setTime(new Date(0));
1353 cal.roll(Calendar.HOUR, 0x7F000000);
1354 cal.roll(Calendar.HOUR, -0x7F000000);
1355 if (cal.getTime().getTime() != 0) {
1356 errln("Hour rolling broken");
1359 for (int op=0; op<2; ++op) {
1360 logln("Testing GregorianCalendar " +
1361 (op==0 ? "add" : "roll"));
1362 for (int field=0; field < cal.getFieldCount(); ++field) {
1363 if (field != Calendar.ZONE_OFFSET &&
1364 field != Calendar.DST_OFFSET) {
1365 for (int j=0; j<fieldsList.length; ++j) {
1366 int fields[] = fieldsList[j];
1368 cal.set(fields[0], fields[1], fields[2],
1369 fields[3], fields[4], fields[5]);
1370 cal.set(Calendar.MILLISECOND, fields[6]);
1371 cal.setMinimalDaysInFirstWeek(1);
1372 for (int i = 0; i < 2*limit; i++) {
1374 cal.add(field, i < limit ? 1 : -1);
1376 cal.roll(field, i < limit ? 1 : -1);
1379 if (cal.get(Calendar.YEAR) != fields[0] ||
1380 cal.get(Calendar.MONTH) != fields[1] ||
1381 cal.get(Calendar.DATE) != fields[2] ||
1382 cal.get(Calendar.HOUR_OF_DAY) != fields[3] ||
1383 cal.get(Calendar.MINUTE) != fields[4] ||
1384 cal.get(Calendar.SECOND) != fields[5] ||
1385 cal.get(Calendar.MILLISECOND) != fields[6]) {
1386 errln("Field " + field +
1387 " (" + FIELD_NAME[field] +
1388 ") FAIL, expected " +
1390 "/" + (fields[1] + 1) +
1396 ", got " + cal.get(Calendar.YEAR) +
1397 "/" + (cal.get(Calendar.MONTH) + 1) +
1398 "/" + cal.get(Calendar.DATE) +
1399 " " + cal.get(Calendar.HOUR_OF_DAY) +
1400 ":" + cal.get(Calendar.MINUTE) +
1401 ":" + cal.get(Calendar.SECOND) +
1402 "." + cal.get(Calendar.MILLISECOND));
1404 cal.set(fields[0], fields[1], fields[2],
1405 fields[3], fields[4], fields[5]);
1406 cal.set(Calendar.MILLISECOND, fields[6]);
1407 logln("Start date: " + cal.get(Calendar.YEAR) +
1408 "/" + (cal.get(Calendar.MONTH) + 1) +
1409 "/" + cal.get(Calendar.DATE) +
1410 " " + cal.get(Calendar.HOUR_OF_DAY) +
1411 ":" + cal.get(Calendar.MINUTE) +
1412 ":" + cal.get(Calendar.SECOND) +
1413 "." + cal.get(Calendar.MILLISECOND));
1414 long prev = cal.getTime().getTime();
1415 for (int i = 0; i < 2*limit; i++) {
1417 cal.add(field, i < limit ? 1 : -1);
1419 cal.roll(field, i < limit ? 1 : -1);
1421 long t = cal.getTime().getTime();
1422 long delta = t - prev;
1424 logln((op == 0 ? "add(" : "roll(") + FIELD_NAME[field] +
1425 (i < limit ? ", +1) => " : ", -1) => ") +
1426 cal.get(Calendar.YEAR) +
1427 "/" + (cal.get(Calendar.MONTH) + 1) +
1428 "/" + cal.get(Calendar.DATE) +
1429 " " + cal.get(Calendar.HOUR_OF_DAY) +
1430 ":" + cal.get(Calendar.MINUTE) +
1431 ":" + cal.get(Calendar.SECOND) +
1432 "." + cal.get(Calendar.MILLISECOND) +
1433 " delta=" + delta + " ms");
1442 public void Test4174361() {
1443 GregorianCalendar calendar = new GregorianCalendar(1996, 1, 29);
1445 calendar.add(Calendar.MONTH, 10);
1446 //Date date1 = calendar.getTime();
1448 int d1 = calendar.get(Calendar.DAY_OF_MONTH);
1450 calendar = new GregorianCalendar(1996, 1, 29);
1451 calendar.add(Calendar.MONTH, 11);
1452 //Date date2 = calendar.getTime();
1454 int d2 = calendar.get(Calendar.DAY_OF_MONTH);
1457 errln("adding months to Feb 29 broken");
1462 * Calendar does not update field values when setTimeZone is called.
1464 public void Test4177484() {
1465 TimeZone PST = TimeZone.getTimeZone("PST");
1466 TimeZone EST = TimeZone.getTimeZone("EST");
1468 Calendar cal = Calendar.getInstance(PST, Locale.US);
1470 cal.set(1999, 3, 21, 15, 5, 0); // Arbitrary
1471 int h1 = cal.get(Calendar.HOUR_OF_DAY);
1472 cal.setTimeZone(EST);
1473 int h2 = cal.get(Calendar.HOUR_OF_DAY);
1475 errln("FAIL: Fields not updated after setTimeZone");
1478 // getTime() must NOT change when time zone is changed.
1479 // getTime() returns zone-independent time in ms.
1481 cal.setTimeZone(PST);
1482 cal.set(Calendar.HOUR_OF_DAY, 10);
1483 Date pst10 = cal.getTime();
1484 cal.setTimeZone(EST);
1485 Date est10 = cal.getTime();
1486 if (!pst10.equals(est10)) {
1487 errln("FAIL: setTimeZone changed time");
1492 * Week of year is wrong at the start and end of the year.
1494 public void Test4197699() {
1495 GregorianCalendar cal = new GregorianCalendar();
1496 cal.setFirstDayOfWeek(Calendar.MONDAY);
1497 cal.setMinimalDaysInFirstWeek(4);
1498 DateFormat fmt = new SimpleDateFormat("E dd MMM yyyy 'DOY='D 'WOY='w");
1499 fmt.setCalendar(cal);
1502 2000, Calendar.JANUARY, 1, 52,
1503 2001, Calendar.DECEMBER, 31, 1,
1506 for (int i=0; i<DATA.length; ) {
1507 cal.set(DATA[i++], DATA[i++], DATA[i++]);
1508 int expWOY = DATA[i++];
1509 int actWOY = cal.get(Calendar.WEEK_OF_YEAR);
1510 if (expWOY == actWOY) {
1511 logln("Ok: " + fmt.format(cal.getTime()));
1513 errln("FAIL: " + fmt.format(cal.getTime())
1514 + ", expected WOY=" + expWOY);
1515 cal.add(Calendar.DATE, -8);
1516 for (int j=0; j<14; ++j) {
1517 cal.add(Calendar.DATE, 1);
1518 logln(fmt.format(cal.getTime()));
1525 * Calendar DAY_OF_WEEK_IN_MONTH fields->time broken. The problem is in the
1526 * field disambiguation code in GregorianCalendar. This code is supposed to
1527 * choose the most recent set of fields among the following:
1529 * MONTH + DAY_OF_MONTH MONTH + WEEK_OF_MONTH + DAY_OF_WEEK MONTH +
1530 * DAY_OF_WEEK_IN_MONTH + DAY_OF_WEEK DAY_OF_YEAR WEEK_OF_YEAR + DAY_OF_WEEK
1532 public void Test4209071() {
1533 Calendar cal = Calendar.getInstance(Locale.US);
1535 // General field setting test
1538 Date d[] = new Date[13];
1539 java.util.Calendar tempcal = java.util.Calendar.getInstance();
1541 tempcal.set(Y, Calendar.JANUARY, 1);
1542 d[0] = tempcal.getTime();
1543 tempcal.set(Y, Calendar.MARCH, 1);
1544 d[1] = tempcal.getTime();
1545 tempcal.set(Y, Calendar.JANUARY, 4);
1546 d[2] = tempcal.getTime();
1547 tempcal.set(Y, Calendar.JANUARY, 18);
1548 d[3] = tempcal.getTime();
1549 tempcal.set(Y, Calendar.JANUARY, 18);
1550 d[4] = tempcal.getTime();
1551 tempcal.set(Y-1, Calendar.DECEMBER, 22);
1552 d[5] = tempcal.getTime();
1553 tempcal.set(Y, Calendar.JANUARY, 26);
1554 d[6] = tempcal.getTime();
1555 tempcal.set(Y, Calendar.JANUARY, 26);
1556 d[7] = tempcal.getTime();
1557 tempcal.set(Y, Calendar.MARCH, 1);
1558 d[8] = tempcal.getTime();
1559 tempcal.set(Y, Calendar.OCTOBER, 6);
1560 d[9] = tempcal.getTime();
1561 tempcal.set(Y, Calendar.OCTOBER, 13);
1562 d[10] = tempcal.getTime();
1563 tempcal.set(Y, Calendar.AUGUST, 10);
1564 d[11] = tempcal.getTime();
1565 tempcal.set(Y, Calendar.DECEMBER, 7);
1566 d[12] = tempcal.getTime();
1568 Object[] FIELD_DATA = {
1569 // Add new test cases as needed.
1574 new int[] { Calendar.MONTH, Calendar.MARCH }, d[1],
1576 new int[] { Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY }, d[2],
1578 new int[] { Calendar.DAY_OF_WEEK, Calendar.THURSDAY,
1579 Calendar.DAY_OF_MONTH, 18, }, d[3],
1581 new int[] { Calendar.DAY_OF_MONTH, 18,
1582 Calendar.DAY_OF_WEEK, Calendar.THURSDAY, }, d[4],
1583 // 5 (WOM -1 is in previous month)
1584 new int[] { Calendar.DAY_OF_MONTH, 18,
1585 Calendar.WEEK_OF_MONTH, -1,
1586 Calendar.DAY_OF_WEEK, Calendar.THURSDAY, }, d[5],
1588 new int[] { Calendar.DAY_OF_MONTH, 18,
1589 Calendar.WEEK_OF_MONTH, 4,
1590 Calendar.DAY_OF_WEEK, Calendar.THURSDAY, }, d[6],
1591 // 7 (DIM -1 is in same month)
1592 new int[] { Calendar.DAY_OF_MONTH, 18,
1593 Calendar.DAY_OF_WEEK_IN_MONTH, -1,
1594 Calendar.DAY_OF_WEEK, Calendar.THURSDAY, }, d[7],
1596 new int[] { Calendar.WEEK_OF_YEAR, 9,
1597 Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY, }, d[8],
1599 new int[] { Calendar.MONTH, Calendar.OCTOBER,
1600 Calendar.DAY_OF_WEEK_IN_MONTH, 1,
1601 Calendar.DAY_OF_WEEK, Calendar.FRIDAY, }, d[9],
1603 new int[] { Calendar.MONTH, Calendar.OCTOBER,
1604 Calendar.WEEK_OF_MONTH, 2,
1605 Calendar.DAY_OF_WEEK, Calendar.FRIDAY, }, d[10],
1607 new int[] { Calendar.MONTH, Calendar.OCTOBER,
1608 Calendar.DAY_OF_MONTH, 15,
1609 Calendar.DAY_OF_YEAR, 222, }, d[11],
1611 new int[] { Calendar.DAY_OF_WEEK, Calendar.THURSDAY,
1612 Calendar.MONTH, Calendar.DECEMBER, }, d[12],
1615 for (int i=0; i<FIELD_DATA.length; i+=2) {
1616 int[] fields = (int[]) FIELD_DATA[i];
1617 Date exp = (Date) FIELD_DATA[i+1];
1620 cal.set(Calendar.YEAR, Y);
1621 for (int j=0; j<fields.length; j+=2) {
1622 cal.set(fields[j], fields[j+1]);
1625 Date act = cal.getTime();
1626 if (!act.equals(exp)) {
1627 errln("FAIL: Test " + (i/2) + " got " + act +
1629 " (see test/java/util/Calendar/CalendarRegression.java");
1633 tempcal.set(1997, Calendar.JANUARY, 5);
1634 d[0] = tempcal.getTime();
1635 tempcal.set(1997, Calendar.JANUARY, 26);
1636 d[1] = tempcal.getTime();
1637 tempcal.set(1997, Calendar.FEBRUARY, 23);
1638 d[2] = tempcal.getTime();
1639 tempcal.set(1997, Calendar.JANUARY, 26);
1640 d[3] = tempcal.getTime();
1641 tempcal.set(1997, Calendar.JANUARY, 5);
1642 d[4] = tempcal.getTime();
1643 tempcal.set(1996, Calendar.DECEMBER, 8);
1644 d[5] = tempcal.getTime();
1645 // Test specific failure reported in bug
1647 new Integer(1), d[0], new Integer(4), d[1],
1648 new Integer(8), d[2], new Integer(-1), d[3],
1649 new Integer(-4), d[4], new Integer(-8), d[5],
1651 for (int i=0; i<DATA.length; i+=2) {
1653 cal.set(Calendar.DAY_OF_WEEK_IN_MONTH,
1654 ((Number) DATA[i]).intValue());
1655 cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
1656 cal.set(Calendar.MONTH, Calendar.JANUARY);
1657 cal.set(Calendar.YEAR, 1997);
1658 Date actual = cal.getTime();
1659 if (!actual.equals(DATA[i+1])) {
1660 errln("FAIL: Sunday " + DATA[i] +
1661 " of Jan 1997 -> " + actual +
1662 ", want " + DATA[i+1]);
1668 * WEEK_OF_YEAR computed incorrectly. A failure of this test can indicate a
1669 * problem in several different places in the
1671 public void Test4288792() throws Exception
1673 TimeZone savedTZ = TimeZone.getDefault();
1674 TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
1675 GregorianCalendar cal = new GregorianCalendar();
1677 for (int i = 1900; i < 2100; i++) {
1678 for (int j1 = 1; j1 <= 7; j1++) {
1679 // Loop for MinimalDaysInFirstWeek: 1..7
1680 for (int j = Calendar.SUNDAY; j <= Calendar.SATURDAY; j++) {
1681 // Loop for FirstDayOfWeek: SUNDAY..SATURDAY
1683 cal.setMinimalDaysInFirstWeek(j1);
1684 cal.setFirstDayOfWeek(j);
1685 // Set the calendar to the first day of the last week
1686 // of the year. This may overlap some of the start of
1687 // the next year; that is, the last week of 1999 may
1688 // include some of January 2000. Use the add() method
1689 // to advance through the week. For each day, call
1690 // get(WEEK_OF_YEAR). The result should be the same
1691 // for the whole week. Note that a bug in
1692 // getActualMaximum() will break this test.
1693 cal.set(Calendar.YEAR, i);
1694 int maxWeek = cal.getActualMaximum(Calendar.WEEK_OF_YEAR);
1695 cal.set(Calendar.WEEK_OF_YEAR, maxWeek);
1696 cal.set(Calendar.DAY_OF_WEEK, j);
1697 for (int k = 1; k < 7; k++) {
1698 cal.add(Calendar.DATE, 1);
1699 int WOY = cal.get(Calendar.WEEK_OF_YEAR);
1700 if (WOY != maxWeek) {
1701 errln(cal.getTime() + ",got=" + WOY
1702 + ",expected=" + maxWeek
1703 + ",min=" + j1 + ",first=" + j);
1706 // Now advance the calendar one more day. This should
1707 // put it at the first day of week 1 of the next year.
1708 cal.add(Calendar.DATE, 1);
1709 int WOY = cal.get(Calendar.WEEK_OF_YEAR);
1711 errln(cal.getTime() + ",got=" + WOY
1712 + ",expected=1,min=" + j1 + ",first" + j);
1717 TimeZone.setDefault(savedTZ);
1721 * Test fieldDifference().
1723 public void TestJ438() throws Exception {
1725 2000, Calendar.JANUARY, 20, 2010, Calendar.JUNE, 15,
1726 2010, Calendar.JUNE, 15, 2000, Calendar.JANUARY, 20,
1727 1964, Calendar.SEPTEMBER, 7, 1999, Calendar.JUNE, 4,
1728 1999, Calendar.JUNE, 4, 1964, Calendar.SEPTEMBER, 7,
1730 Calendar cal = Calendar.getInstance(Locale.US);
1731 for (int i=0; i<DATA.length; i+=6) {
1740 cal.set(y1, m1, d1);
1741 Date date1 = cal.getTime();
1742 cal.set(y2, m2, d2);
1743 Date date2 = cal.getTime();
1746 int dy = cal.fieldDifference(date2, Calendar.YEAR);
1747 int dm = cal.fieldDifference(date2, Calendar.MONTH);
1748 int dd = cal.fieldDifference(date2, Calendar.DATE);
1750 logln("" + date2 + " - " + date1 + " = " +
1751 dy + "y " + dm + "m " + dd + "d");
1754 cal.add(Calendar.YEAR, dy);
1755 cal.add(Calendar.MONTH, dm);
1756 cal.add(Calendar.DATE, dd);
1757 Date date22 = cal.getTime();
1758 if (!date2.equals(date22)) {
1759 errln("FAIL: " + date1 + " + " +
1760 dy + "y " + dm + "m " + dd + "d = " +
1761 date22 + ", exp " + date2);
1763 logln("Ok: " + date1 + " + " +
1764 dy + "y " + dm + "m " + dd + "d = " +
1770 public void TestT5555() throws Exception
1772 Calendar cal = Calendar.getInstance();
1774 // Set date to Wednesday, February 21, 2007
1775 cal.set(2007, Calendar.FEBRUARY, 21);
1778 // Advance month by three years
1779 cal.add(Calendar.MONTH, 36);
1781 // Move to last Wednesday of month.
1782 cal.set(Calendar.DAY_OF_WEEK_IN_MONTH, -1);
1785 } catch (Exception e) {
1786 errln("Got an exception calling getTime().");
1791 yy = cal.get(Calendar.YEAR);
1792 mm = cal.get(Calendar.MONTH);
1793 dd = cal.get(Calendar.DATE);
1794 ee = cal.get(Calendar.DAY_OF_WEEK_IN_MONTH);
1796 if (yy != 2010 || mm != Calendar.FEBRUARY || dd != 24 || ee != Calendar.WEDNESDAY) {
1797 errln("Got date " + yy + "/" + (mm + 1) + "/" + dd + ", expected 2010/2/24");
1802 * Set behavior of DST_OFFSET field. ICU4J Jitterbug 9.
1804 public void TestJ9() {
1805 int HOURS = 60*60*1000;
1806 Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("PST"),
1809 final int END_FIELDS = 0x1234;
1812 // With no explicit ZONE/DST expect 12:00 am
1813 Calendar.MONTH, Calendar.JUNE,
1815 0, 0, // expected hour, min
1817 // Normal ZONE/DST for June 1 Pacific is 8:00/1:00
1818 Calendar.MONTH, Calendar.JUNE,
1819 Calendar.ZONE_OFFSET, -8*HOURS,
1820 Calendar.DST_OFFSET, HOURS,
1822 0, 0, // expected hour, min
1824 // With ZONE/DST of 8:00/0:30 expect time of 12:30 am
1825 Calendar.MONTH, Calendar.JUNE,
1826 Calendar.ZONE_OFFSET, -8*HOURS,
1827 Calendar.DST_OFFSET, HOURS/2,
1829 0, 30, // expected hour, min
1831 // With ZONE/DST of 8:00/UNSET expect time of 1:00 am
1832 Calendar.MONTH, Calendar.JUNE,
1833 Calendar.ZONE_OFFSET, -8*HOURS,
1835 1, 0, // expected hour, min
1837 // With ZONE/DST of UNSET/0:30 expect 4:30 pm (day before)
1838 Calendar.MONTH, Calendar.JUNE,
1839 Calendar.DST_OFFSET, HOURS/2,
1841 16, 30, // expected hour, min
1844 for (int i=0; i<DATA.length; ) {
1849 while (DATA[i] != END_FIELDS) {
1850 cal.set(DATA[i++], DATA[i++]);
1852 ++i; // skip over END_FIELDS
1855 int h = cal.get(Calendar.HOUR_OF_DAY);
1856 int m = cal.get(Calendar.MINUTE);
1859 if (h != DATA[i] || m != DATA[i+1]) {
1860 errln("Fail: expected " + DATA[i] + ":" + DATA[i+1] +
1861 ", got " + h + ":" + m + " after:");
1862 while (DATA[start] != END_FIELDS) {
1863 logln("set(" + FIELD_NAME[DATA[start++]] +
1864 ", " + DATA[start++] + ");");
1868 i += 2; // skip over expected hour, min
1873 * DateFormat class mistakes date style and time style as follows: -
1874 * DateFormat.getDateTimeInstance takes date style as time style, and time
1875 * style as date style - If a Calendar is passed to
1876 * DateFormat.getDateInstance, it returns time instance - If a Calendar is
1877 * passed to DateFormat.getTimeInstance, it returns date instance
1879 public void TestDateFormatFactoryJ26() {
1880 TimeZone zone = TimeZone.getDefault();
1882 Locale loc = Locale.US;
1883 TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
1884 java.util.Calendar tempcal = java.util.Calendar.getInstance();
1885 tempcal.set(2001, Calendar.APRIL, 5, 17, 43, 53);
1886 Date date = tempcal.getTime();
1887 Calendar cal = Calendar.getInstance(loc);
1889 DateFormat.getDateInstance(DateFormat.SHORT, loc),
1890 "DateFormat.getDateInstance(DateFormat.SHORT, loc)",
1893 DateFormat.getTimeInstance(DateFormat.SHORT, loc),
1894 "DateFormat.getTimeInstance(DateFormat.SHORT, loc)",
1897 DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.SHORT, loc),
1898 "DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.SHORT, loc)",
1899 "Thursday, April 5, 2001 5:43 PM",
1901 DateFormat.getDateInstance(cal, DateFormat.SHORT, loc),
1902 "DateFormat.getDateInstance(cal, DateFormat.SHORT, loc)",
1905 DateFormat.getTimeInstance(cal, DateFormat.SHORT, loc),
1906 "DateFormat.getTimeInstance(cal, DateFormat.SHORT, loc)",
1909 DateFormat.getDateTimeInstance(cal, DateFormat.FULL, DateFormat.SHORT, loc),
1910 "DateFormat.getDateTimeInstance(cal, DateFormat.FULL, DateFormat.SHORT, loc)",
1911 "Thursday, April 5, 2001 5:43 PM",
1913 cal.getDateTimeFormat(DateFormat.SHORT, DateFormat.FULL, loc),
1914 "cal.getDateTimeFormat(DateFormat.SHORT, DateFormat.FULL, loc)",
1915 "4/5/01 5:43:53 PM Pacific Daylight Time",
1917 cal.getDateTimeFormat(DateFormat.FULL, DateFormat.SHORT, loc),
1918 "cal.getDateTimeFormat(DateFormat.FULL, DateFormat.SHORT, loc)",
1919 "Thursday, April 5, 2001 5:43 PM",
1921 for (int i=0; i<DATA.length; i+=3) {
1922 DateFormat df = (DateFormat) DATA[i];
1923 String desc = (String) DATA[i+1];
1924 String exp = (String) DATA[i+2];
1925 String got = df.format(date);
1926 if (got.equals(exp)) {
1927 logln("Ok: " + desc + " => " + got);
1929 errln("FAIL: " + desc + " => " + got + ", expected " + exp);
1933 TimeZone.setDefault(zone);
1937 public void TestRegistration() {
1939 * Set names = Calendar.getCalendarFactoryNames();
1941 * TimeZone tz = TimeZone.getDefault(); Locale loc =
1942 * Locale.getDefault(); Iterator iter = names.iterator(); while
1943 * (iter.hasNext()) { String name = (String)iter.next(); logln("Testing
1944 * factory: " + name);
1946 * Calendar cal = Calendar.getInstance(tz, loc, name); logln("Calendar
1947 * class: " + cal.getClass());
1949 * DateFormat fmt = cal.getDateTimeFormat(DateFormat.LONG,
1950 * DateFormat.LONG, loc);
1952 * logln("Date: " + fmt.format(cal.getTime())); }
1953 * // register new default for our locale logln("\nTesting
1954 * registration"); loc = new Locale("en", "US"); Object key =
1955 * Calendar.register(JapaneseCalendar.factory(), loc, true);
1957 * loc = new Locale("en", "US", "TEST"); Calendar cal =
1958 * Calendar.getInstance(loc); logln("Calendar class: " +
1959 * cal.getClass()); DateFormat fmt =
1960 * cal.getDateTimeFormat(DateFormat.LONG, DateFormat.LONG, loc);
1961 * logln("Date: " + fmt.format(cal.getTime()));
1962 * // force to use other default anyway logln("\nOverride
1963 * registration"); cal = Calendar.getInstance(tz, loc, "Gregorian"); fmt =
1964 * cal.getDateTimeFormat(DateFormat.LONG, DateFormat.LONG, loc);
1965 * logln("Date: " + fmt.format(cal.getTime()));
1966 * // unregister default logln("\nUnregistration"); logln("Unregister
1967 * returned: " + Calendar.unregister(key)); cal =
1968 * Calendar.getInstance(tz, loc, "Gregorian"); fmt =
1969 * cal.getDateTimeFormat(DateFormat.LONG, DateFormat.LONG, loc);
1970 * logln("Date: " + fmt.format(cal.getTime()));
1975 * test serialize-and-modify.
1976 * @throws ClassNotFoundException
1978 public void TestSerialization3474() {
1980 ByteArrayOutputStream icuStream = new ByteArrayOutputStream();
1982 logln("icu Calendar");
1984 com.ibm.icu.util.GregorianCalendar icuCalendar =
1985 new com.ibm.icu.util.GregorianCalendar();
1987 icuCalendar.setTimeInMillis(1187912555931L);
1988 long expectMillis = 1187912520931L; // with seconds (not ms) cleared.
1990 logln("instantiated: "+icuCalendar);
1991 logln("getMillis: "+icuCalendar.getTimeInMillis());
1992 icuCalendar.set(com.ibm.icu.util.GregorianCalendar.SECOND, 0);
1993 logln("setSecond=0: "+icuCalendar);
1995 long gotMillis = icuCalendar.getTimeInMillis();
1996 if(gotMillis != expectMillis) {
1997 errln("expect millis "+expectMillis+" but got "+gotMillis);
1999 logln("getMillis: "+gotMillis);
2002 ObjectOutputStream icuOut =
2003 new ObjectOutputStream(icuStream);
2004 icuOut.writeObject(icuCalendar);
2008 ObjectInputStream icuIn =
2009 new ObjectInputStream(new ByteArrayInputStream(icuStream.toByteArray()));
2011 icuCalendar = (com.ibm.icu.util.GregorianCalendar)icuIn.readObject();
2013 logln("serialized back in: "+icuCalendar);
2015 long gotMillis = icuCalendar.getTimeInMillis();
2016 if(gotMillis != expectMillis) {
2017 errln("expect millis "+expectMillis+" but got "+gotMillis);
2019 logln("getMillis: "+gotMillis);
2023 icuCalendar.set(com.ibm.icu.util.GregorianCalendar.SECOND, 0);
2025 logln("setSecond=0: "+icuCalendar);
2027 long gotMillis = icuCalendar.getTimeInMillis();
2028 if(gotMillis != expectMillis) {
2029 errln("expect millis "+expectMillis+" after stream and setSecond but got "+gotMillis);
2031 logln("getMillis after stream and setSecond: "+gotMillis);
2034 } catch(IOException e) {
2035 errln(e.toString());
2036 e.printStackTrace();
2037 } catch(ClassNotFoundException cnf) {
2038 errln(cnf.toString());
2039 cnf.printStackTrace();
2042 // JDK works correctly, etc etc.
2043 // ByteArrayOutputStream jdkStream = new ByteArrayOutputStream();
2045 // logln("\nSUN Calendar");
2047 // java.util.GregorianCalendar sunCalendar =
2048 // new java.util.GregorianCalendar();
2050 // logln("instanzieren: "+sunCalendar);
2051 // logln("getMillis: "+sunCalendar.getTimeInMillis());
2052 // sunCalendar.set(java.util.GregorianCalendar.SECOND, 0);
2053 // logln("setSecond=0: "+sunCalendar);
2054 // logln("getMillis: "+sunCalendar.getTimeInMillis());
2056 // ObjectOutputStream sunOut =
2057 // new ObjectOutputStream(jdkStream);
2058 // sunOut.writeObject(sunCalendar);
2062 // ObjectInputStream sunIn =
2063 // new ObjectInputStream(new ByteArrayInputStream(jdkStream.toByteArray()));
2064 // sunCalendar = null;
2065 // sunCalendar = (java.util.GregorianCalendar)sunIn.readObject();
2067 // logln("serialized: "+sunCalendar);
2068 // logln("getMillis: "+sunCalendar.getTimeInMillis());
2070 // sunCalendar.set(java.util.GregorianCalendar.SECOND, 0);
2071 // logln("setSecond=0: "+sunCalendar);
2072 // logln("getMillis: "+sunCalendar.getTimeInMillis());
2076 public void TestYearJump3279() {
2077 final long time = 1041148800000L;
2078 Calendar c = new GregorianCalendar();
2079 DateFormat fmt = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.US);
2081 c.setTimeInMillis(time);
2082 int year1 = c.get(Calendar.YEAR);
2084 logln("time: " + fmt.format(new Date(c.getTimeInMillis())));
2086 logln("setting DOW to " + c.getFirstDayOfWeek());
2087 c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
2088 logln("week: " + c.getTime());
2089 logln("week adjust: " + fmt.format(new Date(c.getTimeInMillis())));
2090 int year2 = c.get(Calendar.YEAR);
2092 if(year1 != year2) {
2093 errln("Error: adjusted day of week, and year jumped from " + year1 + " to " + year2);
2095 logln("Year remained " + year2 + " - PASS.");
2099 public void TestGetKeywordValuesForLocale(){
2101 final String[][] PREFERRED = {
2102 {"root", "gregorian"},
2103 {"und", "gregorian"},
2104 {"en_US", "gregorian"},
2105 {"en_029", "gregorian"},
2106 {"th_TH", "buddhist", "gregorian"},
2107 {"und_TH", "buddhist", "gregorian"},
2108 {"en_TH", "buddhist", "gregorian"},
2109 {"he_IL", "gregorian", "hebrew", "islamic", "islamic-civil"},
2110 {"ar_EG", "gregorian", "coptic", "islamic", "islamic-civil"},
2111 {"ja", "gregorian", "japanese"},
2112 {"ps_Guru_IN", "gregorian", "indian"},
2113 {"th@calendar=gregorian", "buddhist", "gregorian"},
2114 {"en@calendar=islamic", "gregorian"},
2115 {"zh_TW", "gregorian", "roc", "chinese"},
2116 {"ar_IR", "gregorian", "persian", "islamic", "islamic-civil"},
2119 String[] ALL = Calendar.getKeywordValuesForLocale("calendar", ULocale.getDefault(), false);
2120 HashSet ALLSET = new HashSet();
2121 for (int i = 0; i < ALL.length; i++) {
2125 for (int i = 0; i < PREFERRED.length; i++) {
2126 ULocale loc = new ULocale(PREFERRED[i][0]);
2127 String[] expected = new String[PREFERRED[i].length - 1];
2128 System.arraycopy(PREFERRED[i], 1, expected, 0, expected.length);
2130 String[] pref = Calendar.getKeywordValuesForLocale("calendar", loc, true);
2131 boolean matchPref = false;
2132 if (pref.length == expected.length) {
2134 for (int j = 0; j < pref.length; j++) {
2135 if (!pref[j].equals(expected[j])) {
2141 errln("FAIL: Preferred values for locale " + loc
2142 + " got:" + Utility.arrayToString(pref) + " expected:" + Utility.arrayToString(expected));
2145 String[] all = Calendar.getKeywordValuesForLocale("calendar", loc, false);
2146 boolean matchAll = false;
2147 if (all.length == ALLSET.size()) {
2149 for (int j = 0; j < all.length; j++) {
2150 if (!ALLSET.contains(all[j])) {
2157 errln("FAIL: All values for locale " + loc
2158 + " got:" + Utility.arrayToString(all));