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