24 import java.text.*;
25 import java.util.*;
26 import java.io.*;
27
28 /**
29 * @test
30 * @bug 4029195 4052408 4056591 4059917 4060212 4061287 4065240 4071441 4073003
31 * 4089106 4100302 4101483 4103340 4103341 4104136 4104522 4106807 4108407
32 * 4134203 4138203 4148168 4151631 4151706 4153860 4162071 4182066 4209272 4210209
33 * 4213086 4250359 4253490 4266432 4406615 4413980 8008577
34 * @library /java/text/testlib
35 * @run main/othervm -Djava.locale.providers=COMPAT,SPI DateFormatRegression
36 */
37 public class DateFormatRegression extends IntlTest {
38
39 public static void main(String[] args) throws Exception {
40 new DateFormatRegression().run(args);
41 }
42
43 public void Test4029195() {
44
45 Date today = new Date();
46
47 logln("today: " + today);
48
49 SimpleDateFormat sdf = (SimpleDateFormat)SimpleDateFormat.getDateInstance();
50 logln("pattern: " + sdf.toPattern());
51 logln("today: " + sdf.format(today));
52
53 sdf.applyPattern("G yyyy DDD");
54 String todayS = sdf.format(today);
55 logln("today: " + todayS);
56 try {
57 today = sdf.parse(todayS);
58 logln("today date: " + today);
59 } catch(Exception e) {
60 logln("Error reparsing date: " + e.getMessage());
61 }
62
63 try {
64 String rt = sdf.format(sdf.parse(todayS));
65 logln("round trip: " + rt);
66 if (!rt.equals(todayS)) errln("Fail: Want " + todayS + " Got " + rt);
67 }
68 catch (ParseException e) {
69 errln("Fail: " + e);
70 e.printStackTrace();
71 }
72 }
73
74 public void Test4052408() {
75 DateFormat fmt = DateFormat.getDateTimeInstance(DateFormat.SHORT,
76 DateFormat.SHORT, Locale.US);
77 Date date = new Date(97, Calendar.MAY, 3, 8, 55);
78 String str;
79 logln(str = fmt.format(date));
80
81 if (!str.equals("5/3/97 8:55 AM"))
82 errln("Fail: Test broken; Want 5/3/97 8:55 AM Got " + str);
83 Hashtable expected = new Hashtable();
84 expected.put(new Integer(DateFormat.MONTH_FIELD), "5");
85 expected.put(new Integer(DateFormat.DATE_FIELD), "3");
86 expected.put(new Integer(DateFormat.YEAR_FIELD), "97");
87 expected.put(new Integer(DateFormat.HOUR1_FIELD), "8");
88 expected.put(new Integer(DateFormat.MINUTE_FIELD), "55");
89 expected.put(new Integer(DateFormat.AM_PM_FIELD), "AM");
90
91 StringBuffer buf = new StringBuffer();
92 String fieldNames[] = {
93 "ERA_FIELD",
94 "YEAR_FIELD",
95 "MONTH_FIELD",
96 "DATE_FIELD",
97 "HOUR_OF_DAY1_FIELD",
98 "HOUR_OF_DAY0_FIELD",
99 "MINUTE_FIELD",
100 "SECOND_FIELD",
101 "MILLISECOND_FIELD",
102 "DAY_OF_WEEK_FIELD",
103 "DAY_OF_YEAR_FIELD",
104 "DAY_OF_WEEK_IN_MONTH_FIELD",
105 "WEEK_OF_YEAR_FIELD",
106 "WEEK_OF_MONTH_FIELD",
107 "AM_PM_FIELD",
108 "HOUR1_FIELD",
109 "HOUR0_FIELD",
110 "TIMEZONE_FIELD",
111 };
112 boolean pass = true;
113 for (int i=0; i<=17; ++i) {
114 FieldPosition pos = new FieldPosition(i);
115 fmt.format(date, buf, pos);
116 char[] dst = new char[pos.getEndIndex() - pos.getBeginIndex()];
117 buf.getChars(pos.getBeginIndex(), pos.getEndIndex(), dst, 0);
118 str = new String(dst);
119 log(i + ": " + fieldNames[i] +
120 ", \"" + str + "\", " +
121 pos.getBeginIndex() + ", " +
122 pos.getEndIndex());
123 String exp = (String) expected.get(new Integer(i));
124 if ((exp == null && str.length() == 0) ||
125 str.equals(exp))
126 logln(" ok");
127 else {
128 logln(" expected " + exp);
129 pass = false;
130 }
131 }
132 if (!pass) errln("Fail: FieldPosition not set right by DateFormat");
133 }
134
135 /**
136 * Verify the function of the [s|g]et2DigitYearStart() API.
137 */
138 public void Test4056591() {
139 try {
140 SimpleDateFormat fmt = new SimpleDateFormat("yyMMdd", Locale.US);
141 Date start = new Date(1809-1900, Calendar.DECEMBER, 25);
142 fmt.set2DigitYearStart(start);
143 if (!fmt.get2DigitYearStart().equals(start))
144 errln("get2DigitYearStart broken");
145 Object[] DATA = {
146 "091225", new Date(1809-1900, Calendar.DECEMBER, 25),
147 "091224", new Date(1909-1900, Calendar.DECEMBER, 24),
148 "091226", new Date(1809-1900, Calendar.DECEMBER, 26),
149 "611225", new Date(1861-1900, Calendar.DECEMBER, 25),
150 };
151 for (int i=0; i<DATA.length; i+=2) {
152 String s = (String) DATA[i];
153 Date exp = (Date) DATA[i+1];
154 Date got = fmt.parse(s);
155 logln(s + " -> " + got + "; exp " + exp);
156 if (!got.equals(exp)) errln("set2DigitYearStart broken");
157 }
238 }
239 }
240
241 public void Test4061287() {
242 SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
243 try {
244 logln(df.parse("35/01/1971").toString());
245 }
246 catch (ParseException e) {
247 errln("Fail: " + e);
248 e.printStackTrace();
249 }
250 df.setLenient(false);
251 boolean ok = false;
252 try {
253 logln(df.parse("35/01/1971").toString());
254 } catch (ParseException e) {ok=true;}
255 if (!ok) errln("Fail: Lenient not working");
256 }
257
258 public void Test4065240() {
259 Date curDate;
260 DateFormat shortdate, fulldate;
261 String strShortDate, strFullDate;
262 Locale saveLocale = Locale.getDefault();
263 TimeZone saveZone = TimeZone.getDefault();
264 try {
265 Locale curLocale = new Locale("de","DE");
266 Locale.setDefault(curLocale);
267 TimeZone.setDefault(TimeZone.getTimeZone("EST"));
268 curDate = new Date(98, 0, 1);
269 shortdate = DateFormat.getDateInstance(DateFormat.SHORT);
270 fulldate = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG
271 );
272 strShortDate = new String("The current date (short form) is " + shortdate.
273 format(curDate));
274 strFullDate = new String("The current date (long form) is " + fulldate.format(curDate));
275
276 logln(strShortDate);
277 logln(strFullDate);
280 if (!strFullDate.endsWith("EST")
281 && !strFullDate.endsWith("GMT-05:00")) {
282 errln("Fail: Want GMT-05:00");
283 }
284 }
285 finally {
286 Locale.setDefault(saveLocale);
287 TimeZone.setDefault(saveZone);
288 }
289 }
290
291 /*
292 DateFormat.equals is too narrowly defined. As a result, MessageFormat
293 does not work correctly. DateFormat.equals needs to be written so
294 that the Calendar sub-object is not compared using Calendar.equals,
295 but rather compared for equivalency. This may necessitate adding a
296 (package private) method to Calendar to test for equivalency.
297
298 Currently this bug breaks MessageFormat.toPattern
299 */
300 public void Test4071441() {
301 DateFormat fmtA = DateFormat.getInstance();
302 DateFormat fmtB = DateFormat.getInstance();
303 Calendar calA = fmtA.getCalendar();
304 Calendar calB = fmtB.getCalendar();
305 Date epoch = new Date(0);
306 Date xmas = new Date(61, Calendar.DECEMBER, 25);
307 calA.setTime(epoch);
308 calB.setTime(epoch);
309 if (!calA.equals(calB))
310 errln("Fail: Can't complete test; Calendar instances unequal");
311 if (!fmtA.equals(fmtB))
312 errln("Fail: DateFormat unequal when Calendars equal");
313 calB.setTime(xmas);
314 if (calA.equals(calB))
315 errln("Fail: Can't complete test; Calendar instances equal");
316 if (!fmtA.equals(fmtB))
317 errln("Fail: DateFormat unequal when Calendars equivalent");
318 logln("DateFormat.equals ok");
319 }
471 if (!pass) {
472 errln("Fail: DateFormat serialization/equality bug");
473 }
474 }
475 catch (IOException e) {
476 errln("Fail: " + e);
477 e.printStackTrace();
478 }
479 catch (ClassNotFoundException e) {
480 errln("Fail: " + e);
481 e.printStackTrace();
482 }
483 finally {
484 TimeZone.setDefault(savedTimeZone);
485 }
486 }
487
488 public void Test4101483() {
489 SimpleDateFormat sdf = new SimpleDateFormat("z", Locale.US);
490 FieldPosition fp = new FieldPosition(DateFormat.TIMEZONE_FIELD);
491 Date d= new Date(9234567890L);
492 StringBuffer buf = new StringBuffer("");
493 logln(sdf.format(d, buf, fp).toString());
494 logln(d + " => " + buf);
495 logln("beginIndex = " + fp.getBeginIndex());
496 logln("endIndex = " + fp.getEndIndex());
497 if (fp.getBeginIndex() == fp.getEndIndex()) errln("Fail: Empty field");
498 }
499
500 /**
501 * Bug 4103340
502 * Bug 4138203
503 * This bug really only works in Locale.US, since that's what the locale
504 * used for Date.toString() is. Bug 4138203 reports that it fails on Korean
505 * NT; it would actually have failed on any non-US locale. Now it should
506 * work on all locales.
507 */
508 public void Test4103340() {
509 // choose a date that is the FIRST of some month
510 // and some arbitrary time
511 Date d=new Date(97, 3, 1, 1, 1, 1);
512 SimpleDateFormat df=new SimpleDateFormat("MMMM", Locale.US);
513
514 String s = d.toString();
515 String s2 = df.format(d);
516 logln("Date="+s);
517 logln("DF="+s2);
518 if (s.indexOf(s2.substring(0,2)) == -1)
519 errln("Months should match");
520 }
521
522 public void Test4103341() {
523 TimeZone saveZone =TimeZone.getDefault();
524 try {
525 TimeZone.setDefault(TimeZone.getTimeZone("CST"));
526 SimpleDateFormat simple = new SimpleDateFormat("MM/dd/yyyy HH:mm");
527 if (!simple.getTimeZone().equals(TimeZone.getDefault()))
528 errln("Fail: SimpleDateFormat not using default zone");
529 }
530 finally {
531 TimeZone.setDefault(saveZone);
532 }
533 }
534
535 public void Test4104136() {
536 SimpleDateFormat sdf = new SimpleDateFormat();
537 String pattern = "'time' hh:mm";
538 sdf.applyPattern(pattern);
539 logln("pattern: \"" + pattern + "\"");
540
541 Object[] DATA = {
542 "time 10:30", new ParsePosition(10), new Date(70, Calendar.JANUARY, 1, 10, 30),
543 "time 10:x", new ParsePosition(0), null,
544 "time 10x", new ParsePosition(0), null,
545 };
546 for (int i=0; i<DATA.length; i+=3) {
547 String text = (String) DATA[i];
548 ParsePosition finish = (ParsePosition) DATA[i+1];
549 Date exp = (Date) DATA[i+2];
550
551 ParsePosition pos = new ParsePosition(0);
552 Date d = sdf.parse(text, pos);
553 logln(" text: \"" + text + "\"");
554 logln(" index: " + pos.getIndex());
555 logln(" result: " + d);
556 if (pos.getIndex() != finish.getIndex())
557 errln("Fail: Expected pos " + finish.getIndex());
558 if (!((d == null && exp == null) ||
559 d.equals(exp)))
560 errln("Fail: Expected result " + exp);
681 "1.4567", "1.45678" };
682 int[] MS = { 4, 4, 4, 45, 456, 567, 678 };
683 for (int i=0; i<IN.length; ++i) {
684 d = fmt.parse(IN[i]);
685 cal.setTime(d);
686 ms = cal.get(Calendar.MILLISECOND);
687 if (ms != MS[i]) {
688 errln("FAIL: parse(\"" + IN[i] + "\" x \"s.S\") -> " +
689 ms + " ms, exp " + MS[i] + " ms");
690 }
691 }
692 }
693
694 /**
695 * SimpleDateFormat incorrect handling of 2 single quotes in format()
696 */
697 public void Test4151631() {
698 String pattern = "'TO_DATE('''dd'-'MM'-'yyyy HH:mm:ss''' , ''DD-MM-YYYY HH:MI:SS'')'";
699 logln("pattern=" + pattern);
700 SimpleDateFormat format = new SimpleDateFormat(pattern, Locale.US);
701 String result = format.format(new Date(1998-1900, Calendar.JUNE, 30, 13, 30, 0));
702 if (!result.equals("TO_DATE('30-06-1998 13:30:00' , 'DD-MM-YYYY HH:MI:SS')")) {
703 errln("Fail: result=" + result);
704 }
705 else {
706 logln("Pass: result=" + result);
707 }
708 }
709
710 /**
711 * 'z' at end of date format throws index exception in SimpleDateFormat
712 * CANNOT REPRODUCE THIS BUG ON 1.2FCS
713 */
714 public void Test4151706() {
715 SimpleDateFormat fmt =
716 new SimpleDateFormat("EEEE, dd-MMM-yy HH:mm:ss z", Locale.US);
717 try {
718 Date d = fmt.parse("Thursday, 31-Dec-98 23:00:00 GMT");
719 if (d.getTime() != Date.UTC(1998-1900, Calendar.DECEMBER, 31, 23, 0, 0))
720 errln("Incorrect value: " + d);
721 } catch (Exception e) {
722 errln("Fail: " + e);
723 }
724 }
725
726 /**
727 * SimpleDateFormat fails to parse redundant data.
728 * This is actually a bug down in GregorianCalendar, but it was reported
729 * as follows...
730 */
731 public void Test4153860() throws ParseException {
732 Locale savedLocale = Locale.getDefault();
733 Locale.setDefault(Locale.US);
738 // Try to create a Date for February 4th
739 Date d1 = sf.parse("1998.02-04");
740 // Set the pattern, this time to use the W value
741 sf.applyPattern("yyyy.MM-dd W");
742 // Try to create a Date for February 4th
743 Date d2 = sf.parse("1998.02-04 1");
744 if (!d1.equals(d2)) {
745 errln("Parse failed, got " + d2 +
746 ", expected " + d1);
747 }
748 }
749 finally {
750 Locale.setDefault(savedLocale);
751 }
752 }
753
754 /**
755 * Confirm that "EST"(GMT-5:00) and "CST"(GMT-6:00) are used in US
756 * as "EST" or "CST", not Australian "EST" and "CST".
757 */
758 public void Test4406615() {
759 Locale savedLocale = Locale.getDefault();
760 TimeZone savedTimeZone = TimeZone.getDefault();
761 Locale.setDefault(Locale.US);
762 TimeZone.setDefault(TimeZone.getTimeZone("PST"));
763
764 Date d1, d2;
765 String dt = "Mon, 1 Jan 2001 00:00:00";
766 SimpleDateFormat sdf =
767 new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
768
769 try {
770 d1 = sdf.parse(dt+" EST");
771 d2 = sdf.parse(dt+" CST");
772
773 if (d1.getYear() != (2000-1900) || d1.getMonth() != 11 ||
774 d1.getDate() != 31 || d1.getHours() != 21 || d1.getMinutes() != 0 ||
775 d2.getYear() != (2000-1900) || d2.getMonth() != 11 ||
776 d2.getDate() != 31 || d2.getHours() != 22 || d2.getMinutes() != 0) {
777 errln("Parse failed, d1 = " + d1 + ", d2 = " + d2);
806 errln("Parse format \"" + format + "\" failed.");
807 }
808 }
809
810 /**
811 * DateFormat shouldn't parse year "-1" as a two-digit year (e.g., "-1" -> 1999).
812 */
813 public void Test4182066() {
814 Locale savedLocale = Locale.getDefault();
815 Locale.setDefault(Locale.US);
816 try {
817 SimpleDateFormat fmt = new SimpleDateFormat("MM/dd/yy",
818 DateFormatSymbols.getInstance(Locale.US));
819 SimpleDateFormat dispFmt = new SimpleDateFormat("MMM dd yyyy GG",
820 DateFormatSymbols.getInstance(Locale.US));
821 /* We expect 2-digit year formats to put 2-digit years in the right
822 * window. Out of range years, that is, anything less than "00" or
823 * greater than "99", are treated as literal years. So "1/2/3456"
824 * becomes 3456 AD. Likewise, "1/2/-3" becomes -3 AD == 2 BC.
825 */
826 Object[] DATA = {
827 "02/29/00", new Date(2000-1900, Calendar.FEBRUARY, 29),
828 "01/23/01", new Date(2001-1900, Calendar.JANUARY, 23),
829 "04/05/-1", new Date( -1-1900, Calendar.APRIL, 5),
830 "01/23/-9", new Date( -9-1900, Calendar.JANUARY, 23),
831 "11/12/1314", new Date(1314-1900, Calendar.NOVEMBER, 12),
832 "10/31/1", new Date( 1-1900, Calendar.OCTOBER, 31),
833 "09/12/+1", null, // "+1" isn't recognized by US NumberFormat
834 "09/12/001", new Date( 1-1900, Calendar.SEPTEMBER,12),
835 };
836 StringBuffer out = new StringBuffer();
837 boolean pass = true;
838 for (int i=0; i<DATA.length; i+=2) {
839 String str = (String) DATA[i];
840 Date expected = (Date) DATA[i+1];
841 Date actual;
842 try {
843 actual = fmt.parse(str);
844 } catch (ParseException e) {
845 actual = null;
861 log(out.toString());
862 } else {
863 err(out.toString());
864 }
865 }
866 finally {
867 Locale.setDefault(savedLocale);
868 }
869 }
870
871 /**
872 * Bug 4210209
873 * Bug 4209272
874 * DateFormat cannot parse Feb 29 2000 when setLenient(false)
875 */
876 public void Test4210209() {
877 String pattern = "MMM d, yyyy";
878 DateFormat fmt = new SimpleDateFormat(pattern,
879 DateFormatSymbols.getInstance(Locale.US));
880 fmt.getCalendar().setLenient(false);
881 Date d = new Date(2000-1900, Calendar.FEBRUARY, 29);
882 String s = fmt.format(d);
883 logln(d + " x " + pattern + " => " + s);
884 ParsePosition pos = new ParsePosition(0);
885 d = fmt.parse(s, pos);
886 logln(d + " <= " + pattern + " x " + s);
887 logln("Parse pos = " + pos);
888 if (pos.getErrorIndex() != -1) {
889 errln("FAIL");
890 }
891
892 // The underlying bug is in GregorianCalendar. If the following lines
893 // succeed, the bug is fixed. If the bug isn't fixed, they will throw
894 // an exception.
895 GregorianCalendar cal = new GregorianCalendar();
896 cal.clear();
897 cal.setLenient(false);
898 cal.set(2000, Calendar.FEBRUARY, 29); // This should work!
899 logln(cal.getTime().toString());
900 }
940 df = DateFormat.getDateInstance(DATA[i]);
941 break;
942 case 3:
943 df = DateFormat.getTimeInstance(DATA[i]);
944 break;
945 }
946 got = 1;
947 } catch (IllegalArgumentException iae) {
948 got = 0;
949 } catch (Exception ex) {
950 e = ex;
951 }
952 if (got != DATA[i+1] || e != null) {
953 errln("FAIL: DateFormat." + DESC[j] + " style " + DATA[i] + " " +
954 (e != null ? e.toString() : GOT[got]));
955 }
956 }
957 }
958 }
959
960 public void Test4253490() throws ParseException {
961 SimpleDateFormat fmt = new SimpleDateFormat("S", Locale.US);
962
963 GregorianCalendar cal = new GregorianCalendar();
964
965 int FORMAT_MS = 12;
966 String[] FORMAT_PAT = { "S", "SS", "SSS", "SSSS" };
967 String[] FORMAT_TO = { "12", "12", "012", "0012" };
968
969 String PARSE_PAT = "S";
970 String[] PARSE_STR = { "1", "12", "125", "1250" };
971 int[] PARSE_TO = { 1, 12, 125, 250 };
972 String PARSE_LPAT = "SSSSS";
973
974 // Test formatting. We want to make sure all digits are output
975 // and that they are zero-padded on the left if necessary.
976 cal.setTime(new Date(0L));
977 cal.set(Calendar.MILLISECOND, FORMAT_MS);
978 Date d = cal.getTime();
979 for (int i=0; i<FORMAT_PAT.length; ++i) {
1009 for (int i=0; i<PARSE_STR.length; ++i) {
1010 cal.setTime(fmt.parse(PARSE_STR[i]));
1011 int ms = cal.get(Calendar.MILLISECOND);
1012 if (ms == PARSE_TO[i]) {
1013 logln("\"" + PARSE_STR[i] + "\" p* \"" +
1014 PARSE_LPAT + "\" -> " + ms + " ms");
1015 } else {
1016 errln("FAIL: \"" + PARSE_STR[i] + "\" p* \"" +
1017 PARSE_LPAT + "\" -> " + ms + " ms, expect " +
1018 PARSE_TO[i] + " ms");
1019 }
1020 }
1021 }
1022
1023 /**
1024 * Bug in handling of time instance; introduces in fix for 4213086.
1025 */
1026 public void Test4250359() {
1027 DateFormat df = DateFormat.getTimeInstance(DateFormat.SHORT,
1028 Locale.US);
1029 Date d = new Date(1999-1900, Calendar.DECEMBER, 25,
1030 1, 2, 3);
1031 String s = df.format(d);
1032 // If the bug is present, we see "1:02 AM 1:02 AM".
1033 // Look for more than one instance of "AM".
1034 int i = s.indexOf("AM");
1035 int j = s.indexOf("AM", i+1);
1036 if (i < 0 || j >= 0) {
1037 errln("FAIL: getTimeInstance().format(d) => \"" +
1038 s + "\"");
1039 }
1040 }
1041
1042 /**
1043 * Test whether SimpleDataFormat (DateFormatSymbols) can format/parse
1044 * non-localized time zones.
1045 */
1046 public void Test4261506() {
1047 Locale savedLocale = Locale.getDefault();
1048 TimeZone savedTimeZone = TimeZone.getDefault();
1049 Locale.setDefault(Locale.JAPAN);
1050
1051 // XXX: Test assumes "PST" is not TimeZoneNames_ja. Need to
1052 // pick up another time zone when L10N is done to that file.
1053 TimeZone.setDefault(TimeZone.getTimeZone("PST"));
1054 SimpleDateFormat fmt = new SimpleDateFormat("yy/MM/dd hh:ss zzz", Locale.JAPAN);
1055 String result = fmt.format(new Date(1999, 0, 1));
1056 logln("format()=>" + result);
1057 if (!result.endsWith("PST")) {
1058 errln("FAIL: SimpleDataFormat.format() did not retrun PST");
1059 }
1060
1061 Date d = null;
1062 try {
1063 d = fmt.parse("99/1/1 10:10 PST");
1064 } catch (ParseException e) {
1065 errln("FAIL: SimpleDataFormat.parse() could not parse PST");
1066 }
1067
1068 result = fmt.format(d);
1069 logln("roundtrip:" + result);
1070 if (!result.equals("99/01/01 10:10 PST")) {
1071 errln("FAIL: SimpleDataFomat timezone roundtrip failed");
1072 }
1073
1074 Locale.setDefault(savedLocale);
|
24 import java.text.*;
25 import java.util.*;
26 import java.io.*;
27
28 /**
29 * @test
30 * @bug 4029195 4052408 4056591 4059917 4060212 4061287 4065240 4071441 4073003
31 * 4089106 4100302 4101483 4103340 4103341 4104136 4104522 4106807 4108407
32 * 4134203 4138203 4148168 4151631 4151706 4153860 4162071 4182066 4209272 4210209
33 * 4213086 4250359 4253490 4266432 4406615 4413980 8008577
34 * @library /java/text/testlib
35 * @run main/othervm -Djava.locale.providers=COMPAT,SPI DateFormatRegression
36 */
37 public class DateFormatRegression extends IntlTest {
38
39 public static void main(String[] args) throws Exception {
40 new DateFormatRegression().run(args);
41 }
42
43 public void Test4029195() {
44 @SuppressWarnings("deprecation")
45 Date today = new Date();
46
47 logln("today: " + today);
48
49 SimpleDateFormat sdf = (SimpleDateFormat)SimpleDateFormat.getDateInstance();
50 logln("pattern: " + sdf.toPattern());
51 logln("today: " + sdf.format(today));
52
53 sdf.applyPattern("G yyyy DDD");
54 String todayS = sdf.format(today);
55 logln("today: " + todayS);
56 try {
57 today = sdf.parse(todayS);
58 logln("today date: " + today);
59 } catch(Exception e) {
60 logln("Error reparsing date: " + e.getMessage());
61 }
62
63 try {
64 String rt = sdf.format(sdf.parse(todayS));
65 logln("round trip: " + rt);
66 if (!rt.equals(todayS)) errln("Fail: Want " + todayS + " Got " + rt);
67 }
68 catch (ParseException e) {
69 errln("Fail: " + e);
70 e.printStackTrace();
71 }
72 }
73
74 public void Test4052408() {
75 DateFormat fmt = DateFormat.getDateTimeInstance(DateFormat.SHORT,
76 DateFormat.SHORT, Locale.US);
77 @SuppressWarnings("deprecation")
78 Date date = new Date(97, Calendar.MAY, 3, 8, 55);
79 String str;
80 logln(str = fmt.format(date));
81
82 if (!str.equals("5/3/97 8:55 AM"))
83 errln("Fail: Test broken; Want 5/3/97 8:55 AM Got " + str);
84 Map<Integer,String> expected = new HashMap<>();
85 expected.put(DateFormat.MONTH_FIELD, "5");
86 expected.put(DateFormat.DATE_FIELD, "3");
87 expected.put(DateFormat.YEAR_FIELD, "97");
88 expected.put(DateFormat.HOUR1_FIELD, "8");
89 expected.put(DateFormat.MINUTE_FIELD, "55");
90 expected.put(DateFormat.AM_PM_FIELD, "AM");
91
92 StringBuffer buf = new StringBuffer();
93 String fieldNames[] = {
94 "ERA_FIELD",
95 "YEAR_FIELD",
96 "MONTH_FIELD",
97 "DATE_FIELD",
98 "HOUR_OF_DAY1_FIELD",
99 "HOUR_OF_DAY0_FIELD",
100 "MINUTE_FIELD",
101 "SECOND_FIELD",
102 "MILLISECOND_FIELD",
103 "DAY_OF_WEEK_FIELD",
104 "DAY_OF_YEAR_FIELD",
105 "DAY_OF_WEEK_IN_MONTH_FIELD",
106 "WEEK_OF_YEAR_FIELD",
107 "WEEK_OF_MONTH_FIELD",
108 "AM_PM_FIELD",
109 "HOUR1_FIELD",
110 "HOUR0_FIELD",
111 "TIMEZONE_FIELD",
112 };
113 boolean pass = true;
114 for (int i=0; i<=17; ++i) {
115 FieldPosition pos = new FieldPosition(i);
116 fmt.format(date, buf, pos);
117 char[] dst = new char[pos.getEndIndex() - pos.getBeginIndex()];
118 buf.getChars(pos.getBeginIndex(), pos.getEndIndex(), dst, 0);
119 str = new String(dst);
120 log(i + ": " + fieldNames[i] +
121 ", \"" + str + "\", " +
122 pos.getBeginIndex() + ", " +
123 pos.getEndIndex());
124 String exp = expected.get(i);
125 if ((exp == null && str.length() == 0) ||
126 str.equals(exp))
127 logln(" ok");
128 else {
129 logln(" expected " + exp);
130 pass = false;
131 }
132 }
133 if (!pass) errln("Fail: FieldPosition not set right by DateFormat");
134 }
135
136 /**
137 * Verify the function of the [s|g]et2DigitYearStart() API.
138 */
139 @SuppressWarnings("deprecation")
140 public void Test4056591() {
141 try {
142 SimpleDateFormat fmt = new SimpleDateFormat("yyMMdd", Locale.US);
143 Date start = new Date(1809-1900, Calendar.DECEMBER, 25);
144 fmt.set2DigitYearStart(start);
145 if (!fmt.get2DigitYearStart().equals(start))
146 errln("get2DigitYearStart broken");
147 Object[] DATA = {
148 "091225", new Date(1809-1900, Calendar.DECEMBER, 25),
149 "091224", new Date(1909-1900, Calendar.DECEMBER, 24),
150 "091226", new Date(1809-1900, Calendar.DECEMBER, 26),
151 "611225", new Date(1861-1900, Calendar.DECEMBER, 25),
152 };
153 for (int i=0; i<DATA.length; i+=2) {
154 String s = (String) DATA[i];
155 Date exp = (Date) DATA[i+1];
156 Date got = fmt.parse(s);
157 logln(s + " -> " + got + "; exp " + exp);
158 if (!got.equals(exp)) errln("set2DigitYearStart broken");
159 }
240 }
241 }
242
243 public void Test4061287() {
244 SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
245 try {
246 logln(df.parse("35/01/1971").toString());
247 }
248 catch (ParseException e) {
249 errln("Fail: " + e);
250 e.printStackTrace();
251 }
252 df.setLenient(false);
253 boolean ok = false;
254 try {
255 logln(df.parse("35/01/1971").toString());
256 } catch (ParseException e) {ok=true;}
257 if (!ok) errln("Fail: Lenient not working");
258 }
259
260 @SuppressWarnings("deprecation")
261 public void Test4065240() {
262 Date curDate;
263 DateFormat shortdate, fulldate;
264 String strShortDate, strFullDate;
265 Locale saveLocale = Locale.getDefault();
266 TimeZone saveZone = TimeZone.getDefault();
267 try {
268 Locale curLocale = new Locale("de","DE");
269 Locale.setDefault(curLocale);
270 TimeZone.setDefault(TimeZone.getTimeZone("EST"));
271 curDate = new Date(98, 0, 1);
272 shortdate = DateFormat.getDateInstance(DateFormat.SHORT);
273 fulldate = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG
274 );
275 strShortDate = new String("The current date (short form) is " + shortdate.
276 format(curDate));
277 strFullDate = new String("The current date (long form) is " + fulldate.format(curDate));
278
279 logln(strShortDate);
280 logln(strFullDate);
283 if (!strFullDate.endsWith("EST")
284 && !strFullDate.endsWith("GMT-05:00")) {
285 errln("Fail: Want GMT-05:00");
286 }
287 }
288 finally {
289 Locale.setDefault(saveLocale);
290 TimeZone.setDefault(saveZone);
291 }
292 }
293
294 /*
295 DateFormat.equals is too narrowly defined. As a result, MessageFormat
296 does not work correctly. DateFormat.equals needs to be written so
297 that the Calendar sub-object is not compared using Calendar.equals,
298 but rather compared for equivalency. This may necessitate adding a
299 (package private) method to Calendar to test for equivalency.
300
301 Currently this bug breaks MessageFormat.toPattern
302 */
303 @SuppressWarnings("deprecation")
304 public void Test4071441() {
305 DateFormat fmtA = DateFormat.getInstance();
306 DateFormat fmtB = DateFormat.getInstance();
307 Calendar calA = fmtA.getCalendar();
308 Calendar calB = fmtB.getCalendar();
309 Date epoch = new Date(0);
310 Date xmas = new Date(61, Calendar.DECEMBER, 25);
311 calA.setTime(epoch);
312 calB.setTime(epoch);
313 if (!calA.equals(calB))
314 errln("Fail: Can't complete test; Calendar instances unequal");
315 if (!fmtA.equals(fmtB))
316 errln("Fail: DateFormat unequal when Calendars equal");
317 calB.setTime(xmas);
318 if (calA.equals(calB))
319 errln("Fail: Can't complete test; Calendar instances equal");
320 if (!fmtA.equals(fmtB))
321 errln("Fail: DateFormat unequal when Calendars equivalent");
322 logln("DateFormat.equals ok");
323 }
475 if (!pass) {
476 errln("Fail: DateFormat serialization/equality bug");
477 }
478 }
479 catch (IOException e) {
480 errln("Fail: " + e);
481 e.printStackTrace();
482 }
483 catch (ClassNotFoundException e) {
484 errln("Fail: " + e);
485 e.printStackTrace();
486 }
487 finally {
488 TimeZone.setDefault(savedTimeZone);
489 }
490 }
491
492 public void Test4101483() {
493 SimpleDateFormat sdf = new SimpleDateFormat("z", Locale.US);
494 FieldPosition fp = new FieldPosition(DateFormat.TIMEZONE_FIELD);
495 @SuppressWarnings("deprecation")
496 Date d= new Date(9234567890L);
497 StringBuffer buf = new StringBuffer("");
498 logln(sdf.format(d, buf, fp).toString());
499 logln(d + " => " + buf);
500 logln("beginIndex = " + fp.getBeginIndex());
501 logln("endIndex = " + fp.getEndIndex());
502 if (fp.getBeginIndex() == fp.getEndIndex()) errln("Fail: Empty field");
503 }
504
505 /**
506 * Bug 4103340
507 * Bug 4138203
508 * This bug really only works in Locale.US, since that's what the locale
509 * used for Date.toString() is. Bug 4138203 reports that it fails on Korean
510 * NT; it would actually have failed on any non-US locale. Now it should
511 * work on all locales.
512 */
513 public void Test4103340() {
514 // choose a date that is the FIRST of some month
515 // and some arbitrary time
516 @SuppressWarnings("deprecation")
517 Date d=new Date(97, 3, 1, 1, 1, 1);
518 SimpleDateFormat df=new SimpleDateFormat("MMMM", Locale.US);
519
520 String s = d.toString();
521 String s2 = df.format(d);
522 logln("Date="+s);
523 logln("DF="+s2);
524 if (s.indexOf(s2.substring(0,2)) == -1)
525 errln("Months should match");
526 }
527
528 public void Test4103341() {
529 TimeZone saveZone =TimeZone.getDefault();
530 try {
531 TimeZone.setDefault(TimeZone.getTimeZone("CST"));
532 SimpleDateFormat simple = new SimpleDateFormat("MM/dd/yyyy HH:mm");
533 if (!simple.getTimeZone().equals(TimeZone.getDefault()))
534 errln("Fail: SimpleDateFormat not using default zone");
535 }
536 finally {
537 TimeZone.setDefault(saveZone);
538 }
539 }
540
541 public void Test4104136() {
542 SimpleDateFormat sdf = new SimpleDateFormat();
543 String pattern = "'time' hh:mm";
544 sdf.applyPattern(pattern);
545 logln("pattern: \"" + pattern + "\"");
546
547 @SuppressWarnings("deprecation")
548 Object[] DATA = {
549 "time 10:30", new ParsePosition(10), new Date(70, Calendar.JANUARY, 1, 10, 30),
550 "time 10:x", new ParsePosition(0), null,
551 "time 10x", new ParsePosition(0), null,
552 };
553 for (int i=0; i<DATA.length; i+=3) {
554 String text = (String) DATA[i];
555 ParsePosition finish = (ParsePosition) DATA[i+1];
556 Date exp = (Date) DATA[i+2];
557
558 ParsePosition pos = new ParsePosition(0);
559 Date d = sdf.parse(text, pos);
560 logln(" text: \"" + text + "\"");
561 logln(" index: " + pos.getIndex());
562 logln(" result: " + d);
563 if (pos.getIndex() != finish.getIndex())
564 errln("Fail: Expected pos " + finish.getIndex());
565 if (!((d == null && exp == null) ||
566 d.equals(exp)))
567 errln("Fail: Expected result " + exp);
688 "1.4567", "1.45678" };
689 int[] MS = { 4, 4, 4, 45, 456, 567, 678 };
690 for (int i=0; i<IN.length; ++i) {
691 d = fmt.parse(IN[i]);
692 cal.setTime(d);
693 ms = cal.get(Calendar.MILLISECOND);
694 if (ms != MS[i]) {
695 errln("FAIL: parse(\"" + IN[i] + "\" x \"s.S\") -> " +
696 ms + " ms, exp " + MS[i] + " ms");
697 }
698 }
699 }
700
701 /**
702 * SimpleDateFormat incorrect handling of 2 single quotes in format()
703 */
704 public void Test4151631() {
705 String pattern = "'TO_DATE('''dd'-'MM'-'yyyy HH:mm:ss''' , ''DD-MM-YYYY HH:MI:SS'')'";
706 logln("pattern=" + pattern);
707 SimpleDateFormat format = new SimpleDateFormat(pattern, Locale.US);
708 @SuppressWarnings("deprecation")
709 String result = format.format(new Date(1998-1900, Calendar.JUNE, 30, 13, 30, 0));
710 if (!result.equals("TO_DATE('30-06-1998 13:30:00' , 'DD-MM-YYYY HH:MI:SS')")) {
711 errln("Fail: result=" + result);
712 }
713 else {
714 logln("Pass: result=" + result);
715 }
716 }
717
718 /**
719 * 'z' at end of date format throws index exception in SimpleDateFormat
720 * CANNOT REPRODUCE THIS BUG ON 1.2FCS
721 */
722 @SuppressWarnings("deprecation")
723 public void Test4151706() {
724 SimpleDateFormat fmt =
725 new SimpleDateFormat("EEEE, dd-MMM-yy HH:mm:ss z", Locale.US);
726 try {
727 Date d = fmt.parse("Thursday, 31-Dec-98 23:00:00 GMT");
728 if (d.getTime() != Date.UTC(1998-1900, Calendar.DECEMBER, 31, 23, 0, 0))
729 errln("Incorrect value: " + d);
730 } catch (Exception e) {
731 errln("Fail: " + e);
732 }
733 }
734
735 /**
736 * SimpleDateFormat fails to parse redundant data.
737 * This is actually a bug down in GregorianCalendar, but it was reported
738 * as follows...
739 */
740 public void Test4153860() throws ParseException {
741 Locale savedLocale = Locale.getDefault();
742 Locale.setDefault(Locale.US);
747 // Try to create a Date for February 4th
748 Date d1 = sf.parse("1998.02-04");
749 // Set the pattern, this time to use the W value
750 sf.applyPattern("yyyy.MM-dd W");
751 // Try to create a Date for February 4th
752 Date d2 = sf.parse("1998.02-04 1");
753 if (!d1.equals(d2)) {
754 errln("Parse failed, got " + d2 +
755 ", expected " + d1);
756 }
757 }
758 finally {
759 Locale.setDefault(savedLocale);
760 }
761 }
762
763 /**
764 * Confirm that "EST"(GMT-5:00) and "CST"(GMT-6:00) are used in US
765 * as "EST" or "CST", not Australian "EST" and "CST".
766 */
767 @SuppressWarnings("deprecation")
768 public void Test4406615() {
769 Locale savedLocale = Locale.getDefault();
770 TimeZone savedTimeZone = TimeZone.getDefault();
771 Locale.setDefault(Locale.US);
772 TimeZone.setDefault(TimeZone.getTimeZone("PST"));
773
774 Date d1, d2;
775 String dt = "Mon, 1 Jan 2001 00:00:00";
776 SimpleDateFormat sdf =
777 new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
778
779 try {
780 d1 = sdf.parse(dt+" EST");
781 d2 = sdf.parse(dt+" CST");
782
783 if (d1.getYear() != (2000-1900) || d1.getMonth() != 11 ||
784 d1.getDate() != 31 || d1.getHours() != 21 || d1.getMinutes() != 0 ||
785 d2.getYear() != (2000-1900) || d2.getMonth() != 11 ||
786 d2.getDate() != 31 || d2.getHours() != 22 || d2.getMinutes() != 0) {
787 errln("Parse failed, d1 = " + d1 + ", d2 = " + d2);
816 errln("Parse format \"" + format + "\" failed.");
817 }
818 }
819
820 /**
821 * DateFormat shouldn't parse year "-1" as a two-digit year (e.g., "-1" -> 1999).
822 */
823 public void Test4182066() {
824 Locale savedLocale = Locale.getDefault();
825 Locale.setDefault(Locale.US);
826 try {
827 SimpleDateFormat fmt = new SimpleDateFormat("MM/dd/yy",
828 DateFormatSymbols.getInstance(Locale.US));
829 SimpleDateFormat dispFmt = new SimpleDateFormat("MMM dd yyyy GG",
830 DateFormatSymbols.getInstance(Locale.US));
831 /* We expect 2-digit year formats to put 2-digit years in the right
832 * window. Out of range years, that is, anything less than "00" or
833 * greater than "99", are treated as literal years. So "1/2/3456"
834 * becomes 3456 AD. Likewise, "1/2/-3" becomes -3 AD == 2 BC.
835 */
836 @SuppressWarnings("deprecation")
837 Object[] DATA = {
838 "02/29/00", new Date(2000-1900, Calendar.FEBRUARY, 29),
839 "01/23/01", new Date(2001-1900, Calendar.JANUARY, 23),
840 "04/05/-1", new Date( -1-1900, Calendar.APRIL, 5),
841 "01/23/-9", new Date( -9-1900, Calendar.JANUARY, 23),
842 "11/12/1314", new Date(1314-1900, Calendar.NOVEMBER, 12),
843 "10/31/1", new Date( 1-1900, Calendar.OCTOBER, 31),
844 "09/12/+1", null, // "+1" isn't recognized by US NumberFormat
845 "09/12/001", new Date( 1-1900, Calendar.SEPTEMBER,12),
846 };
847 StringBuffer out = new StringBuffer();
848 boolean pass = true;
849 for (int i=0; i<DATA.length; i+=2) {
850 String str = (String) DATA[i];
851 Date expected = (Date) DATA[i+1];
852 Date actual;
853 try {
854 actual = fmt.parse(str);
855 } catch (ParseException e) {
856 actual = null;
872 log(out.toString());
873 } else {
874 err(out.toString());
875 }
876 }
877 finally {
878 Locale.setDefault(savedLocale);
879 }
880 }
881
882 /**
883 * Bug 4210209
884 * Bug 4209272
885 * DateFormat cannot parse Feb 29 2000 when setLenient(false)
886 */
887 public void Test4210209() {
888 String pattern = "MMM d, yyyy";
889 DateFormat fmt = new SimpleDateFormat(pattern,
890 DateFormatSymbols.getInstance(Locale.US));
891 fmt.getCalendar().setLenient(false);
892 @SuppressWarnings("deprecation")
893 Date d = new Date(2000-1900, Calendar.FEBRUARY, 29);
894 String s = fmt.format(d);
895 logln(d + " x " + pattern + " => " + s);
896 ParsePosition pos = new ParsePosition(0);
897 d = fmt.parse(s, pos);
898 logln(d + " <= " + pattern + " x " + s);
899 logln("Parse pos = " + pos);
900 if (pos.getErrorIndex() != -1) {
901 errln("FAIL");
902 }
903
904 // The underlying bug is in GregorianCalendar. If the following lines
905 // succeed, the bug is fixed. If the bug isn't fixed, they will throw
906 // an exception.
907 GregorianCalendar cal = new GregorianCalendar();
908 cal.clear();
909 cal.setLenient(false);
910 cal.set(2000, Calendar.FEBRUARY, 29); // This should work!
911 logln(cal.getTime().toString());
912 }
952 df = DateFormat.getDateInstance(DATA[i]);
953 break;
954 case 3:
955 df = DateFormat.getTimeInstance(DATA[i]);
956 break;
957 }
958 got = 1;
959 } catch (IllegalArgumentException iae) {
960 got = 0;
961 } catch (Exception ex) {
962 e = ex;
963 }
964 if (got != DATA[i+1] || e != null) {
965 errln("FAIL: DateFormat." + DESC[j] + " style " + DATA[i] + " " +
966 (e != null ? e.toString() : GOT[got]));
967 }
968 }
969 }
970 }
971
972 @SuppressWarnings("deprecation")
973 public void Test4253490() throws ParseException {
974 SimpleDateFormat fmt = new SimpleDateFormat("S", Locale.US);
975
976 GregorianCalendar cal = new GregorianCalendar();
977
978 int FORMAT_MS = 12;
979 String[] FORMAT_PAT = { "S", "SS", "SSS", "SSSS" };
980 String[] FORMAT_TO = { "12", "12", "012", "0012" };
981
982 String PARSE_PAT = "S";
983 String[] PARSE_STR = { "1", "12", "125", "1250" };
984 int[] PARSE_TO = { 1, 12, 125, 250 };
985 String PARSE_LPAT = "SSSSS";
986
987 // Test formatting. We want to make sure all digits are output
988 // and that they are zero-padded on the left if necessary.
989 cal.setTime(new Date(0L));
990 cal.set(Calendar.MILLISECOND, FORMAT_MS);
991 Date d = cal.getTime();
992 for (int i=0; i<FORMAT_PAT.length; ++i) {
1022 for (int i=0; i<PARSE_STR.length; ++i) {
1023 cal.setTime(fmt.parse(PARSE_STR[i]));
1024 int ms = cal.get(Calendar.MILLISECOND);
1025 if (ms == PARSE_TO[i]) {
1026 logln("\"" + PARSE_STR[i] + "\" p* \"" +
1027 PARSE_LPAT + "\" -> " + ms + " ms");
1028 } else {
1029 errln("FAIL: \"" + PARSE_STR[i] + "\" p* \"" +
1030 PARSE_LPAT + "\" -> " + ms + " ms, expect " +
1031 PARSE_TO[i] + " ms");
1032 }
1033 }
1034 }
1035
1036 /**
1037 * Bug in handling of time instance; introduces in fix for 4213086.
1038 */
1039 public void Test4250359() {
1040 DateFormat df = DateFormat.getTimeInstance(DateFormat.SHORT,
1041 Locale.US);
1042 @SuppressWarnings("deprecation")
1043 Date d = new Date(1999-1900, Calendar.DECEMBER, 25,
1044 1, 2, 3);
1045 String s = df.format(d);
1046 // If the bug is present, we see "1:02 AM 1:02 AM".
1047 // Look for more than one instance of "AM".
1048 int i = s.indexOf("AM");
1049 int j = s.indexOf("AM", i+1);
1050 if (i < 0 || j >= 0) {
1051 errln("FAIL: getTimeInstance().format(d) => \"" +
1052 s + "\"");
1053 }
1054 }
1055
1056 /**
1057 * Test whether SimpleDataFormat (DateFormatSymbols) can format/parse
1058 * non-localized time zones.
1059 */
1060 public void Test4261506() {
1061 Locale savedLocale = Locale.getDefault();
1062 TimeZone savedTimeZone = TimeZone.getDefault();
1063 Locale.setDefault(Locale.JAPAN);
1064
1065 // XXX: Test assumes "PST" is not TimeZoneNames_ja. Need to
1066 // pick up another time zone when L10N is done to that file.
1067 TimeZone.setDefault(TimeZone.getTimeZone("PST"));
1068 SimpleDateFormat fmt = new SimpleDateFormat("yy/MM/dd hh:ss zzz", Locale.JAPAN);
1069 @SuppressWarnings("deprecation")
1070 String result = fmt.format(new Date(1999, 0, 1));
1071 logln("format()=>" + result);
1072 if (!result.endsWith("PST")) {
1073 errln("FAIL: SimpleDataFormat.format() did not retrun PST");
1074 }
1075
1076 Date d = null;
1077 try {
1078 d = fmt.parse("99/1/1 10:10 PST");
1079 } catch (ParseException e) {
1080 errln("FAIL: SimpleDataFormat.parse() could not parse PST");
1081 }
1082
1083 result = fmt.format(d);
1084 logln("roundtrip:" + result);
1085 if (!result.equals("99/01/01 10:10 PST")) {
1086 errln("FAIL: SimpleDataFomat timezone roundtrip failed");
1087 }
1088
1089 Locale.setDefault(savedLocale);
|