• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 package org.apache.harmony.tests.java.text;
18 
19 import libcore.test.annotation.NonCts;
20 import libcore.test.annotation.NonMts;
21 import libcore.test.reasons.NonCtsReasons;
22 import libcore.test.reasons.NonMtsReasons;
23 
24 import java.text.DateFormat;
25 import java.text.DateFormatSymbols;
26 import java.text.FieldPosition;
27 import java.text.ParseException;
28 import java.text.ParsePosition;
29 import java.text.SimpleDateFormat;
30 import java.util.Calendar;
31 import java.util.Date;
32 import java.util.GregorianCalendar;
33 import java.util.Locale;
34 import java.util.SimpleTimeZone;
35 import java.util.TimeZone;
36 
37 
38 public class SimpleDateFormatTest extends junit.framework.TestCase {
39 
40     private TimeZone previousDefaultTimeZone;
41 
setUp()42     @Override public void setUp() {
43         previousDefaultTimeZone = TimeZone.getDefault();
44         TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
45     }
46 
tearDown()47     @Override public void tearDown() {
48         TimeZone.setDefault(previousDefaultTimeZone);
49     }
50 
test_Constructor()51     public void test_Constructor() {
52         // Test for method java.text.SimpleDateFormat()
53         SimpleDateFormat f2 = new SimpleDateFormat();
54         assertTrue("Wrong class", f2.getClass() == SimpleDateFormat.class);
55         assertTrue("Wrong default", f2.equals(DateFormat.getDateTimeInstance(
56                 DateFormat.SHORT, DateFormat.SHORT, Locale.getDefault())));
57         assertTrue("Wrong symbols", f2.getDateFormatSymbols().equals(new DateFormatSymbols()));
58         assertTrue("Doesn't work", f2.format(new Date()).getClass() == String.class);
59     }
60 
test_ConstructorLjava_lang_String()61     public void test_ConstructorLjava_lang_String() {
62         // Test for method java.text.SimpleDateFormat(java.lang.String)
63         SimpleDateFormat f2 = new SimpleDateFormat("yyyy");
64         assertTrue("Wrong class", f2.getClass() == SimpleDateFormat.class);
65         assertEquals("Wrong pattern", "yyyy", f2.toPattern());
66         assertTrue("Wrong locale", f2.equals(new SimpleDateFormat("yyyy", Locale.getDefault())));
67         assertTrue("Wrong symbols", f2.getDateFormatSymbols().equals(new DateFormatSymbols()));
68         assertTrue("Doesn't work", f2.format(new Date()).getClass() == String.class);
69 
70         // Invalid constructor value.
71         try {
72             new SimpleDateFormat("this is an invalid simple date format");
73             fail("Expected test_ConstructorLjava_lang_String to throw IAE.");
74         } catch (IllegalArgumentException ex) {
75             // expected
76         }
77 
78         // Null string value
79         try {
80             new SimpleDateFormat(null);
81             fail("Expected test_ConstructorLjava_lang_String to throw NPE.");
82         } catch (NullPointerException ex) {
83             // expected
84         }
85     }
86 
test_ConstructorLjava_lang_StringLjava_text_DateFormatSymbols()87     public void test_ConstructorLjava_lang_StringLjava_text_DateFormatSymbols() {
88         // Test for method java.text.SimpleDateFormat(java.lang.String,
89         // java.text.DateFormatSymbols)
90         DateFormatSymbols symbols = new DateFormatSymbols(Locale.ENGLISH);
91         symbols.setEras(new String[] { "Before", "After" });
92         SimpleDateFormat f2 = new SimpleDateFormat("y'y'yy", symbols);
93         assertTrue("Wrong class", f2.getClass() == SimpleDateFormat.class);
94         assertEquals("Wrong pattern", "y'y'yy", f2.toPattern());
95         assertTrue("Wrong symbols", f2.getDateFormatSymbols().equals(symbols));
96         assertTrue("Doesn't work", f2.format(new Date()).getClass() == String.class);
97 
98         try {
99             new SimpleDateFormat(null, symbols);
100             fail();
101         } catch (NullPointerException expected) {
102         }
103 
104         try {
105             new SimpleDateFormat("eee", symbols);
106             fail();
107         } catch (IllegalArgumentException expected) {
108         }
109     }
110 
test_ConstructorLjava_lang_StringLjava_util_Locale()111     public void test_ConstructorLjava_lang_StringLjava_util_Locale() {
112         // Test for method java.text.SimpleDateFormat(java.lang.String,
113         // java.util.Locale)
114         SimpleDateFormat f2 = new SimpleDateFormat("'yyyy' MM yy", Locale.GERMAN);
115         assertTrue("Wrong class", f2.getClass() == SimpleDateFormat.class);
116         assertEquals("Wrong pattern", "'yyyy' MM yy", f2.toPattern());
117         assertTrue("Wrong symbols", f2.getDateFormatSymbols().equals(
118                 new DateFormatSymbols(Locale.GERMAN)));
119         assertTrue("Doesn't work", f2.format(new Date()).getClass() == String.class);
120 
121         try {
122             new SimpleDateFormat(null, Locale.GERMAN);
123             fail();
124         } catch (NullPointerException expected) {
125         }
126         try {
127             new SimpleDateFormat("eee", Locale.GERMAN);
128             fail();
129         } catch (IllegalArgumentException expected) {
130         }
131     }
132 
test_applyLocalizedPatternLjava_lang_String()133     public void test_applyLocalizedPatternLjava_lang_String() {
134         SimpleDateFormat f2 = new SimpleDateFormat("y", new Locale("de", "CH"));
135         String pattern = "GyMdkHmsSEDFwWahKzZLc";
136         f2.applyLocalizedPattern(pattern);
137         assertEquals(pattern, f2.toPattern());
138         assertEquals(pattern, f2.toLocalizedPattern());
139 
140         // test invalid patterns
141         try {
142             f2.applyLocalizedPattern("j");
143             fail();
144         } catch (IllegalArgumentException expected) {
145         }
146 
147         try {
148             f2.applyLocalizedPattern("a '"); // Unterminated quote.
149             fail();
150         } catch (IllegalArgumentException expected) {
151         }
152 
153         try {
154             f2.applyLocalizedPattern(null);
155             fail();
156         } catch (NullPointerException expected) {
157         }
158     }
159 
test_applyPatternLjava_lang_String()160     public void test_applyPatternLjava_lang_String() {
161         // Test for method void
162         // java.text.SimpleDateFormat.applyPattern(java.lang.String)
163         SimpleDateFormat f2 = new SimpleDateFormat("y", new Locale("de", "CH"));
164         f2.applyPattern("GyMdkHmsSEDFwWahKz");
165         assertEquals("Wrong pattern", "GyMdkHmsSEDFwWahKz", f2.toPattern());
166 
167         // test invalid patterns
168         try {
169             f2.applyPattern("j");
170             fail("Expected IllegalArgumentException for pattern with invalid patter letter: b");
171         } catch (IllegalArgumentException e) {
172         }
173 
174 //        try {
175 //            f2.applyPattern("u");
176 //            fail("Expected IllegalArgumentException for pattern with invalid patter letter: u");
177 //        } catch (IllegalArgumentException e) {
178 //        }
179 
180         try {
181             f2.applyPattern("a '");
182             fail("Expected IllegalArgumentException for pattern with unterminated quote: a '");
183         } catch (IllegalArgumentException e) {
184         }
185 
186         try {
187             f2.applyPattern(null);
188             fail("Expected NullPointerException for null pattern");
189         } catch (NullPointerException e) {
190         }
191     }
192 
test_clone()193     public void test_clone() {
194         // Test for method java.lang.Object java.text.SimpleDateFormat.clone()
195         SimpleDateFormat f2 = new SimpleDateFormat();
196         SimpleDateFormat clone = (SimpleDateFormat) f2.clone();
197         assertTrue("Invalid clone", f2.equals(clone));
198         clone.applyPattern("y");
199         assertTrue("Format modified", !f2.equals(clone));
200         clone = (SimpleDateFormat) f2.clone();
201         // Date date = clone.get2DigitYearStart();
202         // date.setTime(0);
203         // assertTrue("Equal after date change: " +
204         // f2.get2DigitYearStart().getTime() + " " +
205         // clone.get2DigitYearStart().getTime(), !f2.equals(clone));
206     }
207 
test_equalsLjava_lang_Object()208     public void test_equalsLjava_lang_Object() {
209         // Test for method boolean
210         // java.text.SimpleDateFormat.equals(java.lang.Object)
211         SimpleDateFormat format = (SimpleDateFormat) DateFormat.getInstance();
212         SimpleDateFormat clone = (SimpleDateFormat) format.clone();
213         assertTrue("clone not equal", format.equals(clone));
214         format.format(new Date());
215         assertTrue("not equal after format", format.equals(clone));
216     }
217 
test_equals_afterFormat()218     public void test_equals_afterFormat() {
219         // Regression test for HARMONY-209
220         SimpleDateFormat df = new SimpleDateFormat();
221         df.format(new Date());
222         assertEquals(df, new SimpleDateFormat());
223       }
224 
test_hashCode()225     public void test_hashCode() {
226         SimpleDateFormat format = (SimpleDateFormat) DateFormat.getInstance();
227         SimpleDateFormat clone = (SimpleDateFormat) format.clone();
228         assertTrue("clone has not equal hash code", clone.hashCode() == format.hashCode());
229         format.format(new Date());
230         assertTrue("clone has not equal hash code after format",
231                 clone.hashCode() == format.hashCode());
232         DateFormatSymbols symbols = new DateFormatSymbols(Locale.ENGLISH);
233         symbols.setEras(new String[] { "Before", "After" });
234         SimpleDateFormat format2 = new SimpleDateFormat("y'y'yy", symbols);
235         assertFalse("objects has equal hash code", format2.hashCode() == format.hashCode());
236     }
237 
238     @NonCts(bug = 383977133, reason = NonCtsReasons.NON_BREAKING_BEHAVIOR_FIX)
239     @NonMts(bug = 383977133, reason = NonMtsReasons.TZDATA_VERSION_DEPENDENCY)
test_formatToCharacterIteratorLjava_lang_Object()240     public void test_formatToCharacterIteratorLjava_lang_Object() {
241         try {
242             // Regression for HARMONY-466
243             new SimpleDateFormat().formatToCharacterIterator(null);
244             fail();
245         } catch (NullPointerException expected) {
246         }
247 
248         // Test for method formatToCharacterIterator(java.lang.Object)
249         new Support_SimpleDateFormat(
250                 "test_formatToCharacterIteratorLjava_lang_Object")
251                 .t_formatToCharacterIterator();
252     }
253 
254     @NonCts(bug = 383977133, reason = NonCtsReasons.NON_BREAKING_BEHAVIOR_FIX)
255     @NonMts(bug = 383977133, reason = NonMtsReasons.TZDATA_VERSION_DEPENDENCY)
test_formatLjava_util_DateLjava_lang_StringBufferLjava_text_FieldPosition()256     public void test_formatLjava_util_DateLjava_lang_StringBufferLjava_text_FieldPosition() {
257         // Test for method java.lang.StringBuffer
258         // java.text.SimpleDateFormat.format(java.util.Date,
259         // java.lang.StringBuffer, java.text.FieldPosition)
260 
261         new Support_SimpleDateFormat(
262                 "test_formatLjava_util_DateLjava_lang_StringBufferLjava_text_FieldPosition")
263                 .t_format_with_FieldPosition();
264 
265         SimpleDateFormat format = new SimpleDateFormat("", Locale.ENGLISH);
266         Calendar cal = new GregorianCalendar(1999, Calendar.JUNE, 2, 15, 3, 6);
267         assertFormat(format, " G", cal, " AD", DateFormat.ERA_FIELD);
268         assertFormat(format, " GG", cal, " AD", DateFormat.ERA_FIELD);
269         assertFormat(format, " GGG", cal, " AD", DateFormat.ERA_FIELD);
270         assertFormat(format, " G", new GregorianCalendar(-1999, Calendar.JUNE, 2), " BC",
271                 DateFormat.ERA_FIELD);
272 
273         // This assumes Unicode behavior where 'y' and 'yyy' don't truncate,
274         // which means that it will fail on the RI.
275         assertFormat(format, " y", cal, " 1999", DateFormat.YEAR_FIELD);
276         assertFormat(format, " yy", cal, " 99", DateFormat.YEAR_FIELD);
277         assertFormat(format, " yy", new GregorianCalendar(2001, Calendar.JUNE, 2), " 01",
278                 DateFormat.YEAR_FIELD);
279         assertFormat(format, " yy", new GregorianCalendar(2000, Calendar.JUNE, 2), " 00",
280                 DateFormat.YEAR_FIELD);
281         assertFormat(format, " yyy", new GregorianCalendar(2000, Calendar.JUNE, 2), " 2000",
282                 DateFormat.YEAR_FIELD);
283         assertFormat(format, " yyy", cal, " 1999", DateFormat.YEAR_FIELD);
284         assertFormat(format, " yyyy", cal, " 1999", DateFormat.YEAR_FIELD);
285         assertFormat(format, " yyyyy", cal, " 01999", DateFormat.YEAR_FIELD);
286 
287         assertFormat(format, " M", cal, " 6", DateFormat.MONTH_FIELD);
288         assertFormat(format, " M", new GregorianCalendar(1999, Calendar.NOVEMBER, 2), " 11",
289                 DateFormat.MONTH_FIELD);
290         assertFormat(format, " MM", cal, " 06", DateFormat.MONTH_FIELD);
291         assertFormat(format, " MMM", cal, " Jun", DateFormat.MONTH_FIELD);
292         assertFormat(format, " MMMM", cal, " June", DateFormat.MONTH_FIELD);
293         assertFormat(format, " MMMMM", cal, " J", DateFormat.MONTH_FIELD);
294 
295         assertFormat(format, " d", cal, " 2", DateFormat.DATE_FIELD);
296         assertFormat(format, " d", new GregorianCalendar(1999, Calendar.NOVEMBER, 12), " 12",
297                 DateFormat.DATE_FIELD);
298         assertFormat(format, " dd", cal, " 02", DateFormat.DATE_FIELD);
299         assertFormat(format, " dddd", cal, " 0002", DateFormat.DATE_FIELD);
300 
301         assertFormat(format, " h", cal, " 3", DateFormat.HOUR1_FIELD);
302         assertFormat(format, " h", new GregorianCalendar(1999, Calendar.NOVEMBER, 12), " 12",
303                 DateFormat.HOUR1_FIELD);
304         assertFormat(format, " hh", cal, " 03", DateFormat.HOUR1_FIELD);
305         assertFormat(format, " hhhh", cal, " 0003", DateFormat.HOUR1_FIELD);
306 
307         assertFormat(format, " H", cal, " 15", DateFormat.HOUR_OF_DAY0_FIELD);
308         assertFormat(format, " H", new GregorianCalendar(1999, Calendar.NOVEMBER, 12, 4, 0), " 4",
309                 DateFormat.HOUR_OF_DAY0_FIELD);
310         assertFormat(format, " H", new GregorianCalendar(1999, Calendar.NOVEMBER, 12, 12, 0), " 12",
311                 DateFormat.HOUR_OF_DAY0_FIELD);
312         assertFormat(format, " H", new GregorianCalendar(1999, Calendar.NOVEMBER, 12), " 0",
313                 DateFormat.HOUR_OF_DAY0_FIELD);
314         assertFormat(format, " HH", cal, " 15", DateFormat.HOUR_OF_DAY0_FIELD);
315         assertFormat(format, " HHHH", cal, " 0015", DateFormat.HOUR_OF_DAY0_FIELD);
316 
317         assertFormat(format, " m", cal, " 3", DateFormat.MINUTE_FIELD);
318         assertFormat(format, " m", new GregorianCalendar(1999, Calendar.NOVEMBER, 12, 4, 47), " 47",
319                 DateFormat.MINUTE_FIELD);
320         assertFormat(format, " mm", cal, " 03", DateFormat.MINUTE_FIELD);
321         assertFormat(format, " mmmm", cal, " 0003", DateFormat.MINUTE_FIELD);
322 
323         assertFormat(format, " s", cal, " 6", DateFormat.SECOND_FIELD);
324         assertFormat(format, " s", new GregorianCalendar(1999, Calendar.NOVEMBER, 12, 4, 47, 13), " 13",
325                 DateFormat.SECOND_FIELD);
326         assertFormat(format, " ss", cal, " 06", DateFormat.SECOND_FIELD);
327         assertFormat(format, " ssss", cal, " 0006", DateFormat.SECOND_FIELD);
328 
329         assertFormat(format, " S", cal, " 0", DateFormat.MILLISECOND_FIELD);
330         Calendar temp = new GregorianCalendar();
331         temp.set(Calendar.MILLISECOND, 961);
332 
333         assertFormat(format, " SS", temp, " 96", DateFormat.MILLISECOND_FIELD);
334         assertFormat(format, " SSSS", cal, " 0000", DateFormat.MILLISECOND_FIELD);
335 
336         assertFormat(format, " SS", cal, " 00", DateFormat.MILLISECOND_FIELD);
337 
338         assertFormat(format, " E", cal, " Wed", DateFormat.DAY_OF_WEEK_FIELD);
339         assertFormat(format, " EE", cal, " Wed", DateFormat.DAY_OF_WEEK_FIELD);
340         assertFormat(format, " EEE", cal, " Wed", DateFormat.DAY_OF_WEEK_FIELD);
341         assertFormat(format, " EEEE", cal, " Wednesday", DateFormat.DAY_OF_WEEK_FIELD);
342         assertFormat(format, " EEEEE", cal, " W", DateFormat.DAY_OF_WEEK_FIELD);
343 
344         assertFormat(format, " D", cal, " 153", DateFormat.DAY_OF_YEAR_FIELD);
345         assertFormat(format, " DD", cal, " 153", DateFormat.DAY_OF_YEAR_FIELD);
346         assertFormat(format, " DDDD", cal, " 0153", DateFormat.DAY_OF_YEAR_FIELD);
347 
348         assertFormat(format, " F", cal, " 1", DateFormat.DAY_OF_WEEK_IN_MONTH_FIELD);
349         assertFormat(format, " F", new GregorianCalendar(1999, Calendar.NOVEMBER, 14), " 2",
350                 DateFormat.DAY_OF_WEEK_IN_MONTH_FIELD);
351         assertFormat(format, " FF", cal, " 01", DateFormat.DAY_OF_WEEK_IN_MONTH_FIELD);
352         assertFormat(format, " FFFF", cal, " 0001", DateFormat.DAY_OF_WEEK_IN_MONTH_FIELD);
353 
354         cal.setMinimalDaysInFirstWeek(1);
355         cal.setFirstDayOfWeek(1);
356 
357         assertFormat(format, " w", cal, " 23", DateFormat.WEEK_OF_YEAR_FIELD);
358         assertFormat(format, " ww", cal, " 23", DateFormat.WEEK_OF_YEAR_FIELD);
359         assertFormat(format, " wwww", cal, " 0023", DateFormat.WEEK_OF_YEAR_FIELD);
360 
361         assertFormat(format, " W", cal, " 1", DateFormat.WEEK_OF_MONTH_FIELD);
362         assertFormat(format, " WW", cal, " 01", DateFormat.WEEK_OF_MONTH_FIELD);
363         assertFormat(format, " WWWW", cal, " 0001", DateFormat.WEEK_OF_MONTH_FIELD);
364 
365         assertFormat(format, " a", cal, " PM", DateFormat.AM_PM_FIELD);
366         assertFormat(format, " a", new GregorianCalendar(1999, Calendar.NOVEMBER, 14), " AM",
367                 DateFormat.AM_PM_FIELD);
368         assertFormat(format, " a", new GregorianCalendar(1999, Calendar.NOVEMBER, 14, 12, 0), " PM",
369                 DateFormat.AM_PM_FIELD);
370         assertFormat(format, " aa", cal, " PM", DateFormat.AM_PM_FIELD);
371         assertFormat(format, " aaa", cal, " PM", DateFormat.AM_PM_FIELD);
372         assertFormat(format, " aaaa", cal, " PM", DateFormat.AM_PM_FIELD);
373         assertFormat(format, " aaaaa", cal, " PM", DateFormat.AM_PM_FIELD);
374 
375         assertFormat(format, " k", cal, " 15", DateFormat.HOUR_OF_DAY1_FIELD);
376         assertFormat(format, " k", new GregorianCalendar(1999, Calendar.NOVEMBER, 12, 4, 0), " 4",
377                 DateFormat.HOUR_OF_DAY1_FIELD);
378         assertFormat(format, " k", new GregorianCalendar(1999, Calendar.NOVEMBER, 12, 12, 0), " 12",
379                 DateFormat.HOUR_OF_DAY1_FIELD);
380         assertFormat(format, " k", new GregorianCalendar(1999, Calendar.NOVEMBER, 12), " 24",
381                 DateFormat.HOUR_OF_DAY1_FIELD);
382         assertFormat(format, " kk", cal, " 15", DateFormat.HOUR_OF_DAY1_FIELD);
383         assertFormat(format, " kkkk", cal, " 0015", DateFormat.HOUR_OF_DAY1_FIELD);
384 
385         assertFormat(format, " K", cal, " 3", DateFormat.HOUR0_FIELD);
386         assertFormat(format, " K", new GregorianCalendar(1999, Calendar.NOVEMBER, 12), " 0",
387                 DateFormat.HOUR0_FIELD);
388         assertFormat(format, " KK", cal, " 03", DateFormat.HOUR0_FIELD);
389         assertFormat(format, " KKKK", cal, " 0003", DateFormat.HOUR0_FIELD);
390 
391         format.applyPattern("'Mkz''':.@5");
392         assertEquals("Wrong output", "Mkz':.@5", format.format(new Date()));
393 
394         // Test invalid args to format.
395         SimpleDateFormat dateFormat = new SimpleDateFormat();
396         try {
397             dateFormat.format(null, new StringBuffer(), new FieldPosition(1));
398             fail();
399         } catch (NullPointerException expected) {
400         }
401     }
402 
assertFormat(SimpleDateFormat format, String pattern, Calendar cal, String expected, int field)403     private void assertFormat(SimpleDateFormat format, String pattern, Calendar cal,
404             String expected, int field) {
405         StringBuffer buffer = new StringBuffer();
406         FieldPosition position = new FieldPosition(field);
407         format.applyPattern(pattern);
408         format.format(cal.getTime(), buffer, position);
409         String result = buffer.toString();
410         assertTrue("Wrong format: \"" + pattern + "\" expected: " + expected + " result: " + result,
411                 result.equals(expected));
412         assertEquals("Wrong begin position: " + pattern + "\n" + "expected: " + expected + "\n" +
413                 "field: " + field, 1, position.getBeginIndex());
414         assertTrue("Wrong end position: " + pattern + " expected: " + expected + " field: " + field,
415                 position.getEndIndex() == result.length());
416     }
417 
418     @NonCts(bug = 383977133, reason = NonCtsReasons.NON_BREAKING_BEHAVIOR_FIX)
419     @NonMts(bug = 383977133, reason = NonMtsReasons.TZDATA_VERSION_DEPENDENCY)
test_format_time_zones()420     public void test_format_time_zones() throws Exception {
421         Calendar cal = new GregorianCalendar(1999, Calendar.JUNE, 2, 15, 3, 6);
422 
423         SimpleDateFormat format = new SimpleDateFormat("", Locale.ENGLISH);
424         format.setTimeZone(TimeZone.getTimeZone("EST"));
425         assertFormat(format, " z", cal, " EST", DateFormat.TIMEZONE_FIELD);
426         Calendar temp2 = new GregorianCalendar(1999, Calendar.JANUARY, 12);
427         assertFormat(format, " z", temp2, " EST", DateFormat.TIMEZONE_FIELD);
428         assertFormat(format, " zz", cal, " EST", DateFormat.TIMEZONE_FIELD);
429         assertFormat(format, " zzz", cal, " EST", DateFormat.TIMEZONE_FIELD);
430         assertFormat(format, " zzzz", cal, " Eastern Standard Time", DateFormat.TIMEZONE_FIELD);
431         assertFormat(format, " zzzz", temp2, " Eastern Standard Time", DateFormat.TIMEZONE_FIELD);
432         assertFormat(format, " zzzzz", cal, " Eastern Standard Time", DateFormat.TIMEZONE_FIELD);
433 
434         format.setTimeZone(TimeZone.getTimeZone("America/New_York"));
435         assertFormat(format, " z", cal, " EDT", DateFormat.TIMEZONE_FIELD);
436         assertFormat(format, " z", temp2, " EST", DateFormat.TIMEZONE_FIELD);
437         assertFormat(format, " zz", cal, " EDT", DateFormat.TIMEZONE_FIELD);
438         assertFormat(format, " zzz", cal, " EDT", DateFormat.TIMEZONE_FIELD);
439         assertFormat(format, " zzzz", cal, " Eastern Daylight Time", DateFormat.TIMEZONE_FIELD);
440         assertFormat(format, " zzzz", temp2, " Eastern Standard Time", DateFormat.TIMEZONE_FIELD);
441         assertFormat(format, " zzzzz", cal, " Eastern Daylight Time", DateFormat.TIMEZONE_FIELD);
442 
443         TimeZone tz0001 = new SimpleTimeZone(60000, "ONE MINUTE");
444         TimeZone tz0130 = new SimpleTimeZone(5400000, "ONE HOUR, THIRTY");
445         TimeZone tzMinus0130 = new SimpleTimeZone(-5400000, "NEG ONE HOUR, THIRTY");
446 
447         format.setTimeZone(tz0001);
448 //        test(" Z", cal, " +0001", DateFormat.TIMEZONE_FIELD);
449 //        test(" ZZZZ", cal, " GMT+00:01", DateFormat.TIMEZONE_FIELD);
450 //        test(" ZZZZZ", cal, " +00:01", DateFormat.TIMEZONE_FIELD);
451         format.setTimeZone(tz0130);
452 //        test(" Z", cal, " +0130", DateFormat.TIMEZONE_FIELD);
453         format.setTimeZone(tzMinus0130);
454 //        test(" Z", cal, " -0130", DateFormat.TIMEZONE_FIELD);
455 
456         format.setTimeZone(tz0001);
457         assertFormat(format, " z", cal, " GMT+00:01", DateFormat.TIMEZONE_FIELD);
458         assertFormat(format, " zzzz", cal, " GMT+00:01", DateFormat.TIMEZONE_FIELD);
459         format.setTimeZone(tz0130);
460         assertFormat(format, " z", cal, " GMT+01:30", DateFormat.TIMEZONE_FIELD);
461         format.setTimeZone(tzMinus0130);
462         assertFormat(format, " z", cal, " GMT-01:30", DateFormat.TIMEZONE_FIELD);
463     }
464 
465     @NonCts(bug = 383977133, reason = NonCtsReasons.NON_BREAKING_BEHAVIOR_FIX)
466     @NonMts(bug = 383977133, reason = NonMtsReasons.TZDATA_VERSION_DEPENDENCY)
test_timeZoneFormatting()467     public void test_timeZoneFormatting() {
468         // tests specific to formatting of timezones
469         Date summerDate = new GregorianCalendar(1999, Calendar.JUNE, 2, 15, 3, 6).getTime();
470         Date winterDate = new GregorianCalendar(1999, Calendar.JANUARY, 12).getTime();
471 
472         verifyFormatTimezone(
473                 "America/Los_Angeles", "PDT, Pacific Daylight Time", "-0700, GMT-07:00",
474                 summerDate);
475         verifyFormatTimezone(
476                 "America/Los_Angeles", "PST, Pacific Standard Time", "-0800, GMT-08:00",
477                 winterDate);
478 
479         verifyFormatTimezone("GMT-7", "GMT-07:00, GMT-07:00", "-0700, GMT-07:00", summerDate);
480         verifyFormatTimezone("GMT-7", "GMT-07:00, GMT-07:00", "-0700, GMT-07:00", winterDate);
481 
482         verifyFormatTimezone("GMT+14", "GMT+14:00, GMT+14:00", "+1400, GMT+14:00", summerDate);
483         verifyFormatTimezone("GMT+14", "GMT+14:00, GMT+14:00", "+1400, GMT+14:00", winterDate);
484 
485         // this fails on the RI!
486         verifyFormatTimezone("America/Detroit", "EDT, Eastern Daylight Time", "-0400, GMT-04:00",
487                 summerDate);
488         verifyFormatTimezone("America/Detroit", "EST, Eastern Standard Time", "-0500, GMT-05:00",
489                 winterDate);
490 
491         // Pacific/Kiritimati is one of the timezones supported only in mJava
492         verifyFormatTimezone(
493                 "Pacific/Kiritimati", "GMT+14:00, Line Islands Time", "+1400, GMT+14:00",
494                 summerDate);
495         verifyFormatTimezone(
496                 "Pacific/Kiritimati", "GMT+14:00, Line Islands Time", "+1400, GMT+14:00",
497                 winterDate);
498 
499         verifyFormatTimezone("EST", "EST, Eastern Standard Time", "-0500, GMT-05:00", summerDate);
500         verifyFormatTimezone("EST", "EST, Eastern Standard Time", "-0500, GMT-05:00", winterDate);
501 
502         verifyFormatTimezone("GMT+14", "GMT+14:00, GMT+14:00", "+1400, GMT+14:00", summerDate);
503         verifyFormatTimezone("GMT+14", "GMT+14:00, GMT+14:00", "+1400, GMT+14:00", winterDate);
504     }
505 
verifyFormatTimezone(String timeZoneId, String expected1, String expected2, Date date)506     private void verifyFormatTimezone(String timeZoneId, String expected1, String expected2,
507             Date date) {
508         SimpleDateFormat format = new SimpleDateFormat("", Locale.ENGLISH);
509         format.setTimeZone(SimpleTimeZone.getTimeZone(timeZoneId));
510         format.applyPattern("z, zzzz");
511         assertEquals("Test z for TimeZone : " + timeZoneId, expected1, format.format(date));
512 
513         format.applyPattern("Z, ZZZZ");
514         assertEquals("Test Z for TimeZone : " + timeZoneId, expected2, format.format(date));
515     }
516 
test_get2DigitYearStart()517     public void test_get2DigitYearStart() {
518         // Test for method java.util.Date
519         // java.text.SimpleDateFormat.get2DigitYearStart()
520         SimpleDateFormat f1 = new SimpleDateFormat("y");
521         Date date = f1.get2DigitYearStart();
522         Calendar cal = new GregorianCalendar();
523         int year = cal.get(Calendar.YEAR);
524         cal.setTime(date);
525         assertTrue("Wrong default year start", cal.get(Calendar.YEAR) == (year - 80));
526     }
527 
test_getDateFormatSymbols()528     public void test_getDateFormatSymbols() {
529         // Test for method java.text.DateFormatSymbols
530         // java.text.SimpleDateFormat.getDateFormatSymbols()
531         SimpleDateFormat df = (SimpleDateFormat) DateFormat.getInstance();
532         DateFormatSymbols dfs = df.getDateFormatSymbols();
533         assertTrue("Symbols identical", dfs != df.getDateFormatSymbols());
534     }
535 
test_parseLjava_lang_StringLjava_text_ParsePosition()536     public void test_parseLjava_lang_StringLjava_text_ParsePosition() throws Exception {
537         // Test for method java.util.Date
538         // java.text.SimpleDateFormat.parse(java.lang.String,
539         // java.text.ParsePosition)
540         Calendar cal = new GregorianCalendar(1970, Calendar.JANUARY, 1);
541         Date time = cal.getTime();
542         assertParse("h", " 12", time, 1, 3);
543         assertParse("H", " 0", time, 1, 2);
544         assertParse("k", " 24", time, 1, 3);
545         assertParse("K", " 0", time, 1, 2);
546 
547         cal = new GregorianCalendar(1970, Calendar.JANUARY, 1, 1, 0);
548         time = cal.getTime();
549         assertParse("h", "1", time, 0, 1);
550         assertParse("H", "1 ", time, 0, 1);
551         assertParse("k", "1", time, 0, 1);
552         assertParse("K", "1", time, 0, 1);
553 
554         cal = new GregorianCalendar(1970, Calendar.JANUARY, 1, 11, 0);
555         time = cal.getTime();
556         assertParse("h", "0011 ", time, 0, 4);
557         assertParse("K", "11", time, 0, 2);
558         cal = new GregorianCalendar(1970, Calendar.JANUARY, 1, 23, 0);
559         time = cal.getTime();
560         assertParse("H", "23", time, 0, 2);
561         assertParse("k", "23", time, 0, 2);
562 
563         assertParse("h a", " 3 AM", new GregorianCalendar(1970,
564                 Calendar.JANUARY, 1, 3, 0).getTime(), 1, 5);
565         assertParse("K a", " 3 pm ", new GregorianCalendar(1970,
566                 Calendar.JANUARY, 1, 15, 0).getTime(), 1, 5);
567         assertParse("m:s", "0:59 ", new GregorianCalendar(1970,
568                 Calendar.JANUARY, 1, 0, 0, 59).getTime(), 0, 4);
569         assertParse("m:s", "59:0", new GregorianCalendar(1970, Calendar.JANUARY,
570                 1, 0, 59, 0).getTime(), 0, 4);
571         assertParse("ms", "059", new GregorianCalendar(1970, Calendar.JANUARY,
572                 1, 0, 0, 59).getTime(), 0, 3);
573 
574         cal = new GregorianCalendar(1970, Calendar.JANUARY, 1);
575         assertParse("S", "0", cal.getTime(), 0, 1);
576         cal.setTimeZone(TimeZone.getTimeZone("HST"));
577         cal.set(Calendar.MILLISECOND, 999);
578         assertParse("S z", "999 HST", cal.getTime(), 0, 7);
579 
580         cal = new GregorianCalendar(1970, Calendar.JANUARY, 1);
581         cal.set(Calendar.ERA, GregorianCalendar.BC);
582         assertParse("G", "Bc ", cal.getTime(), 0, 2);
583     }
584 
test_parse_y()585     public void test_parse_y() throws Exception {
586         assertParse("y", "00", new GregorianCalendar(2000, Calendar.JANUARY, 1).getTime(), 0, 2);
587         assertParse("y", "99", new GregorianCalendar(1999, Calendar.JANUARY, 1).getTime(), 0, 2);
588         assertParse("y", "1", new GregorianCalendar(1, Calendar.JANUARY, 1).getTime(), 0, 1);
589         assertParse("y", "-1", new GregorianCalendar(-1, Calendar.JANUARY, 1).getTime(), 0, 2);
590         assertParse("y", "001", new GregorianCalendar(1, Calendar.JANUARY, 1).getTime(), 0, 3);
591         assertParse("y", "2005", new GregorianCalendar(2005, Calendar.JANUARY, 1).getTime(), 0, 4);
592     }
593 
test_parse_yy()594     public void test_parse_yy() throws Exception {
595         assertParse("yy", "00", new GregorianCalendar(2000, Calendar.JANUARY, 1).getTime(), 0, 2);
596         assertParse("yy", "99", new GregorianCalendar(1999, Calendar.JANUARY, 1).getTime(), 0, 2);
597         assertParse("yy", "1", new GregorianCalendar(1, Calendar.JANUARY, 1).getTime(), 0, 1);
598         assertParse("yy", "-1", new GregorianCalendar(-1, Calendar.JANUARY, 1).getTime(), 0, 2);
599         assertParse("yy", "001", new GregorianCalendar(1, Calendar.JANUARY, 1).getTime(), 0, 3);
600         assertParse("yy", "2005", new GregorianCalendar(2005, Calendar.JANUARY, 1).getTime(), 0, 4);
601     }
602 
test_parse_yyy()603     public void test_parse_yyy() throws Exception {
604         assertParse("yyy", "99", new GregorianCalendar(99, Calendar.JANUARY, 1).getTime(), 0, 2);
605         assertParse("yyy", "1", new GregorianCalendar(1, Calendar.JANUARY, 1).getTime(), 0, 1);
606         assertParse("yyy", "-1", new GregorianCalendar(-1, Calendar.JANUARY, 1).getTime(), 0, 2);
607         assertParse("yyy", "001", new GregorianCalendar(1, Calendar.JANUARY, 1).getTime(), 0, 3);
608         assertParse("yyy", "2005", new GregorianCalendar(2005, Calendar.JANUARY, 1).getTime(),
609                 0, 4);
610     }
611 
test_parse_yyyy()612     public void test_parse_yyyy() throws Exception {
613         assertParse("yyyy", "99", new GregorianCalendar(99, Calendar.JANUARY, 1).getTime(), 0, 2);
614         assertParse("yyyy", "  1999", new GregorianCalendar(1999, Calendar.JANUARY, 1).getTime(),
615                 2, 6);
616     }
617 
test_parse_monthPatterns()618     public void test_parse_monthPatterns() throws Exception {
619         assertParse("MM'M'", "4M", new GregorianCalendar(1970, Calendar.APRIL, 1).getTime(), 0, 2);
620         assertParse("MMM", "Feb", new GregorianCalendar(1970, Calendar.FEBRUARY, 1).getTime(),
621                 0, 3);
622         assertParse("MMMM d", "April 14 ",
623                 new GregorianCalendar(1970, Calendar.APRIL, 14).getTime(), 0, 8);
624         assertParse("MMMMd", "April14 ", new GregorianCalendar(1970, Calendar.APRIL, 14).getTime(),
625                 0, 7);
626         assertParse("E w", "Mon 12", new GregorianCalendar(1970, Calendar.MARCH, 16).getTime(),
627                 0, 6);
628         assertParse("Ew", "Mon12", new GregorianCalendar(1970, Calendar.MARCH, 16).getTime(), 0, 5);
629         assertParse("M EE ''W", "5 Tue '2", new GregorianCalendar(1970, Calendar.MAY, 5).getTime(),
630                 0, 8);
631         assertParse("MEE''W", "5Tue'2", new GregorianCalendar(1970, Calendar.MAY, 5).getTime(),
632                 0, 6);
633         assertParse("MMM EEE F", " JUL Sunday 3",
634                 new GregorianCalendar(1970, Calendar.JULY, 19).getTime(), 1, 13);
635         assertParse("MMMEEEF", " JULSunday3",
636                 new GregorianCalendar(1970, Calendar.JULY, 19).getTime(), 1, 11);
637     }
638 
test_parse_dayOfYearPatterns()639     public void test_parse_dayOfYearPatterns() throws Exception {
640         GregorianCalendar cal = new GregorianCalendar(1970, Calendar.JANUARY, 1);
641         cal.setTimeZone(TimeZone.getTimeZone("GMT+0:1"));
642         cal.set(Calendar.DAY_OF_YEAR, 243);
643         assertParse("D z", "243 GMT+00:00", cal.getTime(), 0, 13);
644     }
645 
test_parse_h_m_z()646     public void test_parse_h_m_z() throws Exception {
647         GregorianCalendar cal = new GregorianCalendar(1970, Calendar.JANUARY, 1);
648         cal.setTimeZone(TimeZone.getTimeZone("EST"));
649         cal.set(1970, Calendar.JANUARY, 1, 4, 30);
650         assertParse("h:m z", "4:30 GMT-5:00", cal.getTime(), 0, 13);
651     }
652 
test_parse_h_z_2DigitOffsetFromGMT_doesNotParse()653     public void test_parse_h_z_2DigitOffsetFromGMT_doesNotParse() throws Exception {
654         SimpleDateFormat pFormat = new SimpleDateFormat("h z", Locale.ENGLISH);
655         try {
656             pFormat.parse("14 GMT-23");
657             fail();
658         } catch (ParseException expected) {
659         }
660 
661         try {
662             pFormat.parse("14 GMT+23");
663             fail();
664         } catch (ParseException expected) {
665         }
666     }
667 
test_parse_h_z_4DigitOffsetFromGMT()668     public void test_parse_h_z_4DigitOffsetFromGMT() throws Exception {
669         assertParse("h z", "14 GMT-0100 ", new Date(54000000), 0, 11);
670         assertParse("h z", "14 GMT+0100 ", new Date(46800000), 0, 11);
671 
672         SimpleDateFormat pFormat = new SimpleDateFormat("h z", Locale.ENGLISH);
673         // Note that OpenJDK only allows zone offsets in the range [-23:59,+23:59]. These offsets
674         // are confined to a narrower range in practice.
675         try {
676             pFormat.parse("14 GMT+24:00");
677             fail();
678         } catch (ParseException expected) {
679         }
680         try {
681             pFormat.parse("14 GMT-24:00");
682             fail();
683         } catch (ParseException expected) {
684         }
685     }
686 
test_parse_h_z_4DigitOffsetNoGMT()687     public void test_parse_h_z_4DigitOffsetNoGMT() throws Exception {
688         assertParse("h z", "14 +0100 ", new Date(46800000), 0, 8);
689         assertParse("h z", "14 -0100 ", new Date(54000000), 0, 8);
690     }
691 
test_parse_yyyyMMddHHmmss()692     public void test_parse_yyyyMMddHHmmss() throws Exception {
693         assertParse("yyyyMMddHHmmss", "19990913171901",
694                 new GregorianCalendar(1999, Calendar.SEPTEMBER, 13, 17, 19, 1).getTime(), 0, 14);
695     }
696 
test_parse_dd_MMMM_yyyy_EEEE()697     public void test_parse_dd_MMMM_yyyy_EEEE() throws Exception {
698         Date d = new Date(1015822800000L);
699         String pattern = "dd MMMM yyyy EEEE";
700         String dateString = "11 March 2002 Monday";
701         assertFormat(d, pattern, dateString);
702         assertParse(dateString, pattern, d);
703     }
704 
test_parse_dd_MMMM_yyyy_F()705     public void test_parse_dd_MMMM_yyyy_F() throws Exception {
706         Date d = new Date(1015822800000L);
707         String pattern = "dd MMMM yyyy F";
708         String dateString = "11 March 2002 2";
709         assertFormat(d, pattern, dateString);
710         assertParse(dateString, pattern, d);
711     }
712 
test_parse_dd_MMMM_yyyy_w()713     public void test_parse_dd_MMMM_yyyy_w() throws Exception {
714         Date d = new Date(1015822800000L);
715         String pattern = "dd MMMM yyyy w";
716         String dateString = "11 March 2002 11";
717         assertFormat(d, pattern, dateString);
718         assertParse(dateString, pattern, d);
719     }
720 
test_parse_dd_MMMM_yyyy_W()721     public void test_parse_dd_MMMM_yyyy_W() throws Exception {
722         Date d = new Date(1015822800000L);
723         String pattern = "dd MMMM yyyy W";
724         String dateString = "11 March 2002 3";
725         assertFormat(d, pattern, dateString);
726         assertParse(dateString, pattern, d);
727     }
728 
test_parse_dd_MMMM_yyyy_D()729     public void test_parse_dd_MMMM_yyyy_D() throws Exception {
730         // The day of the year overrides the day of the month.
731         Date d = new Date(1015822800000L);
732         String pattern = "dd MMMM yyyy D";
733         assertFormat(d, pattern, "11 March 2002 70");
734         assertParse("5 January 2002 70", pattern, d);
735     }
736 
test_parse_W_w_dd_MMMM_yyyy_EEEE()737     public void test_parse_W_w_dd_MMMM_yyyy_EEEE() throws Exception {
738         Date d = new Date(1015822800000L);
739         String pattern = "W w dd MMMM yyyy EEEE";
740         assertFormat(d, pattern, "3 11 11 March 2002 Monday");
741 
742         // http://b/27158550 - this behavior changed when Android switched to using OpenJDK code.
743         // The pattern provides the same information in different ways (e.g. W / w / EEEE
744         // all directly affect the day of the week). The result is entirely dependent on how the
745         // Calendar implementation resolves field order.
746         // assertParse("3 12 5 March 2002 Monday", pattern, d);
747     }
748 
test_parse_w_W_dd_MMMM_yyyy_EEEE()749     public void test_parse_w_W_dd_MMMM_yyyy_EEEE() throws Exception {
750         Date d = new Date(1015822800000L);
751         String pattern = "w W dd MMMM yyyy EEEE";
752         assertFormat(d, pattern, "11 3 11 March 2002 Monday");
753         assertParse("12 3 5 March 2002 Monday", pattern, d);
754     }
755 
test_parse_F_dd_MMMM_yyyy_EEEE()756     public void test_parse_F_dd_MMMM_yyyy_EEEE() throws Exception {
757         Date d = new Date(1015822800000L);
758         String pattern = "F dd MMMM yyyy EEEE";
759         assertFormat(d, pattern, "2 11 March 2002 Monday");
760         assertParse("2 5 March 2002 Monday", pattern, d);
761     }
762 
test_parse_w_dd_MMMM_yyyy_EEEE()763     public void test_parse_w_dd_MMMM_yyyy_EEEE() throws Exception {
764         Date d = new Date(1015822800000L);
765         String pattern = "w dd MMMM yyyy EEEE";
766         assertFormat(d, pattern, "11 11 March 2002 Monday");
767         assertParse("11 5 January 2002 Monday", pattern, d);
768     }
769 
test_parse_w_dd_yyyy_EEEE_MMMM()770     public void test_parse_w_dd_yyyy_EEEE_MMMM() throws Exception {
771         Date d = new Date(1015822800000L);
772         String pattern = "w dd yyyy EEEE MMMM";
773         assertFormat(d, pattern, "11 11 2002 Monday March");
774         assertParse("11 5 2002 Monday January", pattern, d);
775     }
776 
test_parse_w_yyyy_EEEE_MMMM_dd()777     public void test_parse_w_yyyy_EEEE_MMMM_dd() throws Exception {
778         Date d = new Date(1015822800000L);
779         String pattern = "w yyyy EEEE MMMM dd";
780         assertFormat(d, pattern, "11 2002 Monday March 11");
781         assertParse("17 2002 Monday March 11", pattern, d);
782     }
783 
test_parse_dd_D_yyyy_MMMM()784     public void test_parse_dd_D_yyyy_MMMM() throws Exception {
785         Date d = new Date(1015822800000L);
786         String pattern = "dd D yyyy MMMM";
787         assertFormat(d, pattern, "11 70 2002 March");
788         assertParse("5 70 2002 January", pattern, d);
789     }
790 
test_parse_D_dd_yyyy_MMMM()791     public void test_parse_D_dd_yyyy_MMMM() throws Exception {
792         Date d = new Date(1015822800000L);
793         String pattern = "D dd yyyy MMMM";
794         assertFormat(d, pattern, "70 11 2002 March");
795         assertParse("240 11 2002 March", pattern, d);
796     }
797 
assertParse(String input, String pattern, Date expectedDate)798     private static void assertParse(String input, String pattern, Date expectedDate)
799             throws Exception {
800         SimpleDateFormat df = new SimpleDateFormat(pattern, new Locale("en", "US"));
801         df.setTimeZone(TimeZone.getTimeZone("EST"));
802         Date date = df.parse(input);
803         assertEquals("Invalid result '" + pattern + "'", expectedDate, date);
804     }
805 
assertFormat(Date date, String pattern, String expectedOutput)806     private static void assertFormat(Date date, String pattern, String expectedOutput) {
807         SimpleDateFormat df = new SimpleDateFormat(pattern, new Locale("en", "US"));
808         df.setTimeZone(TimeZone.getTimeZone("EST"));
809         String output = df.format(date);
810         assertEquals("Invalid output '" + pattern + "'", expectedOutput, output);
811     }
812 
test_parse_nullParsePosition()813     public void test_parse_nullParsePosition() {
814         SimpleDateFormat format = new SimpleDateFormat("", Locale.ENGLISH);
815         try {
816             format.parse("240 11 2002 March", null);
817             fail();
818         } catch (NullPointerException expected) {
819         }
820     }
821 
test_parse_nullInput()822     public void test_parse_nullInput() {
823         SimpleDateFormat format = new SimpleDateFormat("", Locale.ENGLISH);
824         try {
825             format.parse(null, new ParsePosition(0));
826             fail();
827         } catch (NullPointerException expected) {
828         }
829     }
830 
assertParse(String pattern, String input, Date expected, int start, int end)831     private void assertParse(String pattern, String input, Date expected, int start, int end) {
832         SimpleDateFormat pFormat = new SimpleDateFormat(pattern, Locale.ENGLISH);
833         ParsePosition position = new ParsePosition(start);
834         Date result = pFormat.parse(input, position);
835         assertEquals("Wrong result: " + pattern + " input: " + input +
836                 " resultTime: " + ((result == null) ? "null" : result.getTime()),
837                 expected, result);
838         assertEquals("Wrong end position: " + pattern + " input: " + input,
839                 end, position.getIndex());
840     }
841 
test_set2DigitYearStartLjava_util_Date()842     public void test_set2DigitYearStartLjava_util_Date() {
843         // Test for method void
844         // java.text.SimpleDateFormat.set2DigitYearStart(java.util.Date)
845         SimpleDateFormat f1 = new SimpleDateFormat("yy");
846         f1.set2DigitYearStart(new GregorianCalendar(1950, Calendar.JANUARY, 1).getTime());
847         Calendar cal = new GregorianCalendar();
848         try {
849             cal.setTime(f1.parse("49"));
850             assertEquals("Incorrect year 2049", 2049, cal.get(Calendar.YEAR));
851             cal.setTime(f1.parse("50"));
852             int year = cal.get(Calendar.YEAR);
853             assertTrue("Incorrect year 1950: " + year, year == 1950);
854             f1.applyPattern("y");
855             cal.setTime(f1.parse("00"));
856             assertEquals("Incorrect year 2000", 2000, cal.get(Calendar.YEAR));
857             f1.applyPattern("yyy");
858             cal.setTime(f1.parse("50"));
859             assertEquals("Incorrect year 50", 50, cal.get(Calendar.YEAR));
860         } catch (ParseException e) {
861             fail("ParseException");
862         }
863     }
864 
test_setDateFormatSymbolsLjava_text_DateFormatSymbols()865     public void test_setDateFormatSymbolsLjava_text_DateFormatSymbols() {
866         // Test for method void
867         // java.text.SimpleDateFormat.setDateFormatSymbols(java.text.DateFormatSymbols)
868         SimpleDateFormat f1 = new SimpleDateFormat("a");
869         DateFormatSymbols symbols = new DateFormatSymbols();
870         symbols.setAmPmStrings(new String[] { "morning", "night" });
871         f1.setDateFormatSymbols(symbols);
872         DateFormatSymbols newSym = f1.getDateFormatSymbols();
873         assertTrue("Set incorrectly", newSym.equals(symbols));
874         assertTrue("Not a clone", f1.getDateFormatSymbols() != symbols);
875         String result = f1.format(new GregorianCalendar(1999, Calendar.JUNE, 12, 3, 0).getTime());
876         assertEquals("Incorrect symbols used", "morning", result);
877         symbols.setEras(new String[] { "before", "after" });
878         assertTrue("Identical symbols", !f1.getDateFormatSymbols().equals(symbols));
879 
880         try {
881             f1.setDateFormatSymbols(null);
882             fail();
883         } catch (NullPointerException expected) {
884         }
885     }
886 
test_toPattern()887     public void test_toPattern() {
888         String pattern = "yyyy mm dd";
889         SimpleDateFormat f = new SimpleDateFormat(pattern);
890         assertEquals("Wrong pattern: " + pattern, pattern, f.toPattern());
891 
892         pattern = "GyMdkHmsSEDFwWahKz";
893         f = new SimpleDateFormat("GyMdkHmsSEDFwWahKz", new Locale("de", "CH"));
894         assertTrue("Wrong pattern: " + pattern, f.toPattern().equals(pattern));
895 
896         pattern = "G y M d Z";
897         f = new SimpleDateFormat(pattern, new Locale("de", "CH"));
898         pattern = f.toPattern();
899         assertTrue("Wrong pattern: " + pattern, f.toPattern().equals(pattern));
900     }
901 
test_toLocalizedPattern()902     public void test_toLocalizedPattern() {
903         SimpleDateFormat f2 = new SimpleDateFormat("GyMdkHmsSEDFwWahKzZLc", new Locale("de", "CH"));
904         assertEquals(f2.toPattern(), f2.toLocalizedPattern());
905     }
906 
907     // Regression for HARMONY-502
test_parse_whitespace_within_date()908     public void test_parse_whitespace_within_date() {
909         Date date = new GregorianCalendar(2003, Calendar.APRIL, 5, 9, 7, 6).getTime();
910         parse_whitespace_variants(
911             new SimpleDateFormat("HH:mm:ss dd/MM/yy"),
912             date,
913             new String[] {
914                 "%c9:07:06 05/04/03",
915                 "%c09:07:06 05/04/03",
916                 "09:%c7:06 05/04/03",
917                 "09:%c07:06 05/04/03",
918                 "09:07:%c6 05/04/03",
919                 "09:07:%c06 05/04/03",
920                 "09:07:06 %c05/04/03",
921                 "09:07:06 %c5/04/03",
922                 "09:07:06 05/%c4/03",
923                 "09:07:06 05/%c04/03",
924                 "09:07:06 05/04/%c03",
925             });
926 
927         parse_whitespace_variants(
928             new SimpleDateFormat("HH:mm:ss dd/MM/yyyy"),
929             date,
930             new String[] {
931                 "09:07:06 05/04/%c2003",
932             });
933     }
934 
935     // Tests valid and invalid whitespace characters are parsed correctly within
936     // a date string. dateFormat and expected are the SimpleDateFormat and expected date.
937     // variants is a list of input variations where %c will be substituted with
938     // the whitespace characters to test.
parse_whitespace_variants(SimpleDateFormat dateFormat, Date expected, String[] variants)939     private void parse_whitespace_variants(SimpleDateFormat dateFormat, Date expected,
940         String[] variants) {
941         char validWhitespace[] = { 0x9, 0x20 };
942         char invalidWhitespace[] = {
943                 // Whitespace
944                 0x1c, 0x1d, 0x1e, 0x1f, 0xa, 0xb, 0xc, 0xd, 0x2001, 0x2002,
945                 0x2003, 0x2004, 0x2005, 0x2006, 0x2008, 0x2009, 0x200a, 0x200b,
946                 0x2028, 0x2029, 0x3000,
947                 // Non-breaking space
948                 0xA0, 0x2007, 0x202F };
949 
950         dateFormat.setLenient(false);
951         for (String variant: variants) {
952             for (char c : validWhitespace) {
953                 String info = String.format("Parsing variant='%s', c=0x%x:", variant, (int) c);
954                 String input = String.format(variant, c);
955                 Date date = dateFormat.parse(input, new ParsePosition(0));
956                 assertEquals(info, expected, date);
957                 try {
958                     date = dateFormat.parse(input);
959                 } catch (ParseException e) {
960                     fail(info);
961                 }
962                 assertEquals(info, expected, date);
963 
964             }
965             for (char c : invalidWhitespace) {
966                 String info = String.format("Parsing variant='%s', c=0x%x:", variant, (int) c);
967                 String input = String.format(variant, c);
968                 Date date = dateFormat.parse(input, new ParsePosition(0));
969                 assertNull(info, date);
970                 try {
971                     dateFormat.parse(input);
972                     fail(info);
973                 } catch (ParseException e) {
974                     // Expected
975                 }
976             }
977         }
978     }
979 }
980