1 16 package org.joda.time.format; 17 18 import java.util.Locale ; 19 import java.util.TimeZone ; 20 21 import junit.framework.TestCase; 22 import junit.framework.TestSuite; 23 24 import org.joda.time.Chronology; 25 import org.joda.time.DateTime; 26 import org.joda.time.DateTimeConstants; 27 import org.joda.time.DateTimeUtils; 28 import org.joda.time.DateTimeZone; 29 import org.joda.time.chrono.GJChronology; 30 31 37 public class TestDateTimeFormat extends TestCase { 38 39 private static final DateTimeZone UTC = DateTimeZone.UTC; 40 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris"); 41 private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London"); 42 private static final DateTimeZone TOKYO = DateTimeZone.forID("Asia/Tokyo"); 43 private static final DateTimeZone NEWYORK = DateTimeZone.forID("America/New_York"); 44 45 long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 46 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 47 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 48 366 + 365; 49 private long TEST_TIME_NOW = 51 (y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY; 52 53 private DateTimeZone originalDateTimeZone = null; 54 private TimeZone originalTimeZone = null; 55 private Locale originalLocale = null; 56 57 public static void main(String [] args) { 58 junit.textui.TestRunner.run(suite()); 59 } 60 61 public static TestSuite suite() { 62 return new TestSuite(TestDateTimeFormat.class); 63 } 64 65 public TestDateTimeFormat(String name) { 66 super(name); 67 } 68 69 protected void setUp() throws Exception { 70 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW); 71 originalDateTimeZone = DateTimeZone.getDefault(); 72 originalTimeZone = TimeZone.getDefault(); 73 originalLocale = Locale.getDefault(); 74 DateTimeZone.setDefault(LONDON); 75 TimeZone.setDefault(TimeZone.getTimeZone("Europe/London")); 76 Locale.setDefault(Locale.UK); 77 } 78 79 protected void tearDown() throws Exception { 80 DateTimeUtils.setCurrentMillisSystem(); 81 DateTimeZone.setDefault(originalDateTimeZone); 82 TimeZone.setDefault(originalTimeZone); 83 Locale.setDefault(originalLocale); 84 originalDateTimeZone = null; 85 originalTimeZone = null; 86 originalLocale = null; 87 } 88 89 public void testSubclassableConstructor() { 91 DateTimeFormat f = new DateTimeFormat() { 92 }; 94 assertNotNull(f); 95 } 96 97 public void testFormat_era() { 99 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC); 100 DateTimeFormatter f = DateTimeFormat.forPattern("G").withLocale(Locale.UK); 101 assertEquals(dt.toString(), "AD", f.print(dt)); 102 103 dt = dt.withZone(NEWYORK); 104 assertEquals(dt.toString(), "AD", f.print(dt)); 105 106 dt = dt.withZone(PARIS); 107 assertEquals(dt.toString(), "AD", f.print(dt)); 108 } 109 110 public void testFormat_centuryOfEra() { 112 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC); 113 DateTimeFormatter f = DateTimeFormat.forPattern("C").withLocale(Locale.UK); 114 assertEquals(dt.toString(), "20", f.print(dt)); 115 116 dt = dt.withZone(NEWYORK); 117 assertEquals(dt.toString(), "20", f.print(dt)); 118 119 dt = dt.withZone(TOKYO); 120 assertEquals(dt.toString(), "20", f.print(dt)); 121 122 dt = new DateTime(-123, 6, 9, 10, 20, 30, 40, UTC); 123 assertEquals(dt.toString(), "1", f.print(dt)); 124 } 125 126 public void testFormat_yearOfEra() { 128 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC); 129 DateTimeFormatter f = DateTimeFormat.forPattern("Y").withLocale(Locale.UK); 130 assertEquals(dt.toString(), "2004", f.print(dt)); 131 132 dt = dt.withZone(NEWYORK); 133 assertEquals(dt.toString(), "2004", f.print(dt)); 134 135 dt = dt.withZone(TOKYO); 136 assertEquals(dt.toString(), "2004", f.print(dt)); 137 138 dt = new DateTime(-123, 6, 9, 10, 20, 30, 40, UTC); 139 assertEquals(dt.toString(), "124", f.print(dt)); } 141 142 public void testFormat_yearOfEra_twoDigit() { 143 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC); 144 DateTimeFormatter f = DateTimeFormat.forPattern("YY").withLocale(Locale.UK); 145 assertEquals(dt.toString(), "04", f.print(dt)); 146 147 dt = new DateTime(-123, 6, 9, 10, 20, 30, 40, UTC); 148 assertEquals(dt.toString(), "23", f.print(dt)); 149 150 f = f.withZone(UTC); 152 DateTime expect = null; 153 expect = new DateTime(2004, 1, 1, 0, 0, 0, 0, UTC); 154 assertEquals(expect, f.parseDateTime("04")); 155 156 expect = new DateTime(1922, 1, 1, 0, 0, 0, 0, UTC); 157 assertEquals(expect, f.parseDateTime("22")); 158 159 expect = new DateTime(2021, 1, 1, 0, 0, 0, 0, UTC); 160 assertEquals(expect, f.parseDateTime("21")); 161 162 try { 164 f.parseDateTime("-"); 165 fail(); 166 } catch (IllegalArgumentException ex) {} 167 168 try { 169 f.parseDateTime("+"); 170 fail(); 171 } catch (IllegalArgumentException ex) {} 172 173 f = f.withPivotYear(new Integer (2050)); 175 expect = new DateTime(2000, 1, 1, 0, 0, 0, 0, UTC); 176 assertEquals(expect, f.parseDateTime("00")); 177 178 expect = new DateTime(2099, 1, 1, 0, 0, 0, 0, UTC); 179 assertEquals(expect, f.parseDateTime("99")); 180 181 f = DateTimeFormat.forPattern("YY").withLocale(Locale.UK); 183 f = f.withZone(UTC); 184 f.parseDateTime("5"); 185 f.parseDateTime("005"); 186 f.parseDateTime("+50"); 187 f.parseDateTime("-50"); 188 } 189 190 public void testFormat_yearOfEraParse() { 191 Chronology chrono = GJChronology.getInstanceUTC(); 192 193 DateTimeFormatter f = DateTimeFormat 194 .forPattern("YYYY-MM GG") 195 .withChronology(chrono) 196 .withLocale(Locale.UK); 197 198 DateTime dt = new DateTime(2005, 10, 1, 0, 0, 0, 0, chrono); 199 assertEquals(dt, f.parseDateTime("2005-10 AD")); 200 assertEquals(dt, f.parseDateTime("2005-10 CE")); 201 202 dt = new DateTime(-2005, 10, 1, 0, 0, 0, 0, chrono); 203 assertEquals(dt, f.parseDateTime("2005-10 BC")); 204 assertEquals(dt, f.parseDateTime("2005-10 BCE")); 205 } 206 207 public void testFormat_year() { 209 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC); 210 DateTimeFormatter f = DateTimeFormat.forPattern("y").withLocale(Locale.UK); 211 assertEquals(dt.toString(), "2004", f.print(dt)); 212 213 dt = dt.withZone(NEWYORK); 214 assertEquals(dt.toString(), "2004", f.print(dt)); 215 216 dt = dt.withZone(TOKYO); 217 assertEquals(dt.toString(), "2004", f.print(dt)); 218 219 dt = new DateTime(-123, 6, 9, 10, 20, 30, 40, UTC); 220 assertEquals(dt.toString(), "-123", f.print(dt)); 221 222 try { 224 f.parseDateTime("-"); 225 fail(); 226 } catch (IllegalArgumentException ex) {} 227 228 try { 229 f.parseDateTime("+"); 230 fail(); 231 } catch (IllegalArgumentException ex) {} 232 } 233 234 public void testFormat_year_twoDigit() { 235 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC); 236 DateTimeFormatter f = DateTimeFormat.forPattern("yy").withLocale(Locale.UK); 237 assertEquals(dt.toString(), "04", f.print(dt)); 238 239 dt = new DateTime(-123, 6, 9, 10, 20, 30, 40, UTC); 240 assertEquals(dt.toString(), "23", f.print(dt)); 241 242 f = f.withZone(UTC); 244 DateTime expect = null; 245 expect = new DateTime(2004, 1, 1, 0, 0, 0, 0, UTC); 246 assertEquals(expect, f.parseDateTime("04")); 247 248 expect = new DateTime(1922, 1, 1, 0, 0, 0, 0, UTC); 249 assertEquals(expect, f.parseDateTime("22")); 250 251 expect = new DateTime(2021, 1, 1, 0, 0, 0, 0, UTC); 252 assertEquals(expect, f.parseDateTime("21")); 253 254 try { 256 f.parseDateTime("-"); 257 fail(); 258 } catch (IllegalArgumentException ex) {} 259 260 try { 261 f.parseDateTime("+"); 262 fail(); 263 } catch (IllegalArgumentException ex) {} 264 265 f = f.withPivotYear(new Integer (2050)); 267 expect = new DateTime(2000, 1, 1, 0, 0, 0, 0, UTC); 268 assertEquals(expect, f.parseDateTime("00")); 269 270 expect = new DateTime(2099, 1, 1, 0, 0, 0, 0, UTC); 271 assertEquals(expect, f.parseDateTime("99")); 272 273 f = new DateTimeFormatterBuilder().appendTwoDigitYear(2000).toFormatter(); 276 f = f.withZone(UTC); 277 try { 278 f.parseDateTime("5"); 279 fail(); 280 } catch (IllegalArgumentException ex) {} 281 try { 282 f.parseDateTime("005"); 283 fail(); 284 } catch (IllegalArgumentException ex) {} 285 try { 286 f.parseDateTime("+50"); 287 fail(); 288 } catch (IllegalArgumentException ex) {} 289 try { 290 f.parseDateTime("-50"); 291 fail(); 292 } catch (IllegalArgumentException ex) {} 293 294 f = DateTimeFormat.forPattern("yy").withLocale(Locale.UK); 296 f = f.withZone(UTC); 297 f.parseDateTime("5"); 298 f.parseDateTime("005"); 299 f.parseDateTime("+50"); 300 f.parseDateTime("-50"); 301 302 f = new DateTimeFormatterBuilder().appendTwoDigitYear(2000, true).toFormatter(); 304 f = f.withZone(UTC); 305 expect = new DateTime(2004, 1, 1, 0, 0, 0, 0, UTC); 306 assertEquals(expect, f.parseDateTime("04")); 307 308 expect = new DateTime(4, 1, 1, 0, 0, 0, 0, UTC); 309 assertEquals(expect, f.parseDateTime("+04")); 310 311 expect = new DateTime(-4, 1, 1, 0, 0, 0, 0, UTC); 312 assertEquals(expect, f.parseDateTime("-04")); 313 314 expect = new DateTime(4, 1, 1, 0, 0, 0, 0, UTC); 315 assertEquals(expect, f.parseDateTime("4")); 316 317 expect = new DateTime(-4, 1, 1, 0, 0, 0, 0, UTC); 318 assertEquals(expect, f.parseDateTime("-4")); 319 320 expect = new DateTime(4, 1, 1, 0, 0, 0, 0, UTC); 321 assertEquals(expect, f.parseDateTime("004")); 322 323 expect = new DateTime(4, 1, 1, 0, 0, 0, 0, UTC); 324 assertEquals(expect, f.parseDateTime("+004")); 325 326 expect = new DateTime(-4, 1, 1, 0, 0, 0, 0, UTC); 327 assertEquals(expect, f.parseDateTime("-004")); 328 329 expect = new DateTime(3004, 1, 1, 0, 0, 0, 0, UTC); 330 assertEquals(expect, f.parseDateTime("3004")); 331 332 expect = new DateTime(3004, 1, 1, 0, 0, 0, 0, UTC); 333 assertEquals(expect, f.parseDateTime("+3004")); 334 335 expect = new DateTime(-3004, 1, 1, 0, 0, 0, 0, UTC); 336 assertEquals(expect, f.parseDateTime("-3004")); 337 338 try { 339 f.parseDateTime("-"); 340 fail(); 341 } catch (IllegalArgumentException ex) {} 342 343 try { 344 f.parseDateTime("+"); 345 fail(); 346 } catch (IllegalArgumentException ex) {} 347 } 348 349 public void testFormat_year_long() { 350 DateTime dt = new DateTime(278004, 6, 9, 10, 20, 30, 40, UTC); 351 DateTimeFormatter f = DateTimeFormat.forPattern("yyyy"); 352 assertEquals(dt.toString(), "278004", f.print(dt)); 353 354 f = DateTimeFormat.forPattern("yyyyMMdd"); 356 assertEquals(dt.toString(), "2780040609", f.print(dt)); 357 358 f = DateTimeFormat.forPattern("yyyyddMM"); 360 assertEquals(dt.toString(), "2780040906", f.print(dt)); 361 } 362 363 public void testFormat_weekyear() { 365 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC); 366 DateTimeFormatter f = DateTimeFormat.forPattern("x").withLocale(Locale.UK); 367 assertEquals(dt.toString(), "2004", f.print(dt)); 368 369 dt = dt.withZone(NEWYORK); 370 assertEquals(dt.toString(), "2004", f.print(dt)); 371 372 dt = dt.withZone(TOKYO); 373 assertEquals(dt.toString(), "2004", f.print(dt)); 374 375 dt = new DateTime(-123, 6, 9, 10, 20, 30, 40, UTC); 376 assertEquals(dt.toString(), "-123", f.print(dt)); 377 } 378 379 public void testFormat_weekyearOfEra_twoDigit() { 380 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC); 381 DateTimeFormatter f = DateTimeFormat.forPattern("xx").withLocale(Locale.UK); 382 assertEquals(dt.toString(), "04", f.print(dt)); 383 384 dt = new DateTime(-123, 6, 9, 10, 20, 30, 40, UTC); 385 assertEquals(dt.toString(), "23", f.print(dt)); 386 387 f = f.withZone(UTC); 389 DateTime expect = null; 390 expect = new DateTime(2003, 12, 29, 0, 0, 0, 0, UTC); 391 assertEquals(expect, f.parseDateTime("04")); 392 393 expect = new DateTime(1922, 1, 2, 0, 0, 0, 0, UTC); 394 assertEquals(expect, f.parseDateTime("22")); 395 396 expect = new DateTime(2021, 1, 4, 0, 0, 0, 0, UTC); 397 assertEquals(expect, f.parseDateTime("21")); 398 399 try { 401 f.parseDateTime("-"); 402 fail(); 403 } catch (IllegalArgumentException ex) {} 404 405 try { 406 f.parseDateTime("+"); 407 fail(); 408 } catch (IllegalArgumentException ex) {} 409 410 f = f.withPivotYear(new Integer (2050)); 412 expect = new DateTime(2000, 1, 3, 0, 0, 0, 0, DateTimeZone.UTC); 413 assertEquals(expect, f.parseDateTime("00")); 414 415 expect = new DateTime(2098, 12, 29, 0, 0, 0, 0, DateTimeZone.UTC); 416 assertEquals(expect, f.parseDateTime("99")); 417 418 f = new DateTimeFormatterBuilder().appendTwoDigitWeekyear(2000).toFormatter(); 421 f = f.withZone(UTC); 422 try { 423 f.parseDateTime("5"); 424 fail(); 425 } catch (IllegalArgumentException ex) {} 426 try { 427 f.parseDateTime("005"); 428 fail(); 429 } catch (IllegalArgumentException ex) {} 430 try { 431 f.parseDateTime("+50"); 432 fail(); 433 } catch (IllegalArgumentException ex) {} 434 try { 435 f.parseDateTime("-50"); 436 fail(); 437 } catch (IllegalArgumentException ex) {} 438 439 f = DateTimeFormat.forPattern("xx").withLocale(Locale.UK); 441 f = f.withZone(UTC); 442 f.parseDateTime("5"); 443 f.parseDateTime("005"); 444 f.parseDateTime("+50"); 445 f.parseDateTime("-50"); 446 447 f = new DateTimeFormatterBuilder().appendTwoDigitWeekyear(2000, true).toFormatter(); 449 f = f.withZone(UTC); 450 expect = new DateTime(2003, 12, 29, 0, 0, 0, 0, UTC); 451 assertEquals(expect, f.parseDateTime("04")); 452 453 expect = new DateTime(3, 12, 29, 0, 0, 0, 0, UTC); 454 assertEquals(expect, f.parseDateTime("+04")); 455 456 expect = new DateTime(-4, 1, 1, 0, 0, 0, 0, UTC); 457 assertEquals(expect, f.parseDateTime("-04")); 458 459 expect = new DateTime(3, 12, 29, 0, 0, 0, 0, UTC); 460 assertEquals(expect, f.parseDateTime("4")); 461 462 expect = new DateTime(-4, 1, 1, 0, 0, 0, 0, UTC); 463 assertEquals(expect, f.parseDateTime("-4")); 464 465 expect = new DateTime(3, 12, 29, 0, 0, 0, 0, UTC); 466 assertEquals(expect, f.parseDateTime("004")); 467 468 expect = new DateTime(3, 12, 29, 0, 0, 0, 0, UTC); 469 assertEquals(expect, f.parseDateTime("+004")); 470 471 expect = new DateTime(-4, 1, 1, 0, 0, 0, 0, UTC); 472 assertEquals(expect, f.parseDateTime("-004")); 473 474 expect = new DateTime(3004, 1, 2, 0, 0, 0, 0, UTC); 475 assertEquals(expect, f.parseDateTime("3004")); 476 477 expect = new DateTime(3004, 1, 2, 0, 0, 0, 0, UTC); 478 assertEquals(expect, f.parseDateTime("+3004")); 479 480 expect = new DateTime(-3004, 1, 4, 0, 0, 0, 0, UTC); 481 assertEquals(expect, f.parseDateTime("-3004")); 482 483 try { 484 f.parseDateTime("-"); 485 fail(); 486 } catch (IllegalArgumentException ex) {} 487 488 try { 489 f.parseDateTime("+"); 490 fail(); 491 } catch (IllegalArgumentException ex) {} 492 } 493 494 public void testFormat_weekOfWeekyear() { 496 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC); 497 DateTimeFormatter f = DateTimeFormat.forPattern("w").withLocale(Locale.UK); 498 assertEquals(dt.toString(), "24", f.print(dt)); 499 500 dt = dt.withZone(NEWYORK); 501 assertEquals(dt.toString(), "24", f.print(dt)); 502 503 dt = dt.withZone(TOKYO); 504 assertEquals(dt.toString(), "24", f.print(dt)); 505 } 506 507 public void testFormat_dayOfWeek() { 509 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC); 510 DateTimeFormatter f = DateTimeFormat.forPattern("e").withLocale(Locale.UK); 511 assertEquals(dt.toString(), "3", f.print(dt)); 512 513 dt = dt.withZone(NEWYORK); 514 assertEquals(dt.toString(), "3", f.print(dt)); 515 516 dt = dt.withZone(TOKYO); 517 assertEquals(dt.toString(), "3", f.print(dt)); 518 } 519 520 public void testFormat_dayOfWeekShortText() { 522 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC); 523 DateTimeFormatter f = DateTimeFormat.forPattern("E").withLocale(Locale.UK); 524 assertEquals(dt.toString(), "Wed", f.print(dt)); 525 526 dt = dt.withZone(NEWYORK); 527 assertEquals(dt.toString(), "Wed", f.print(dt)); 528 529 dt = dt.withZone(TOKYO); 530 assertEquals(dt.toString(), "Wed", f.print(dt)); 531 532 f = f.withLocale(Locale.FRENCH); 533 assertEquals(dt.toString(), "mer.", f.print(dt)); 534 } 535 536 public void testFormat_dayOfWeekText() { 538 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC); 539 DateTimeFormatter f = DateTimeFormat.forPattern("EEEE").withLocale(Locale.UK); 540 assertEquals(dt.toString(), "Wednesday", f.print(dt)); 541 542 dt = dt.withZone(NEWYORK); 543 assertEquals(dt.toString(), "Wednesday", f.print(dt)); 544 545 dt = dt.withZone(TOKYO); 546 assertEquals(dt.toString(), "Wednesday", f.print(dt)); 547 548 f = f.withLocale(Locale.FRENCH); 549 assertEquals(dt.toString(), "mercredi", f.print(dt)); 550 } 551 552 public void testFormat_dayOfYearText() { 554 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC); 555 DateTimeFormatter f = DateTimeFormat.forPattern("D").withLocale(Locale.UK); 556 assertEquals(dt.toString(), "161", f.print(dt)); 557 558 dt = dt.withZone(NEWYORK); 559 assertEquals(dt.toString(), "161", f.print(dt)); 560 561 dt = dt.withZone(TOKYO); 562 assertEquals(dt.toString(), "161", f.print(dt)); 563 } 564 565 public void testFormat_monthOfYear() { 567 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC); 568 DateTimeFormatter f = DateTimeFormat.forPattern("M").withLocale(Locale.UK); 569 assertEquals(dt.toString(), "6", f.print(dt)); 570 571 dt = dt.withZone(NEWYORK); 572 assertEquals(dt.toString(), "6", f.print(dt)); 573 574 dt = dt.withZone(TOKYO); 575 assertEquals(dt.toString(), "6", f.print(dt)); 576 } 577 578 public void testFormat_monthOfYearShortText() { 580 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC); 581 DateTimeFormatter f = DateTimeFormat.forPattern("MMM").withLocale(Locale.UK); 582 assertEquals(dt.toString(), "Jun", f.print(dt)); 583 584 dt = dt.withZone(NEWYORK); 585 assertEquals(dt.toString(), "Jun", f.print(dt)); 586 587 dt = dt.withZone(TOKYO); 588 assertEquals(dt.toString(), "Jun", f.print(dt)); 589 590 f = f.withLocale(Locale.FRENCH); 591 assertEquals(dt.toString(), "juin", f.print(dt)); 592 } 593 594 public void testFormat_monthOfYearText() { 596 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC); 597 DateTimeFormatter f = DateTimeFormat.forPattern("MMMM").withLocale(Locale.UK); 598 assertEquals(dt.toString(), "June", f.print(dt)); 599 600 dt = dt.withZone(NEWYORK); 601 assertEquals(dt.toString(), "June", f.print(dt)); 602 603 dt = dt.withZone(TOKYO); 604 assertEquals(dt.toString(), "June", f.print(dt)); 605 606 f = f.withLocale(Locale.FRENCH); 607 assertEquals(dt.toString(), "juin", f.print(dt)); 608 } 609 610 public void testFormat_dayOfMonth() { 612 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC); 613 DateTimeFormatter f = DateTimeFormat.forPattern("d").withLocale(Locale.UK); 614 assertEquals(dt.toString(), "9", f.print(dt)); 615 616 dt = dt.withZone(NEWYORK); 617 assertEquals(dt.toString(), "9", f.print(dt)); 618 619 dt = dt.withZone(TOKYO); 620 assertEquals(dt.toString(), "9", f.print(dt)); 621 } 622 623 public void testFormat_halfdayOfDay() { 625 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC); 626 DateTimeFormatter f = DateTimeFormat.forPattern("a").withLocale(Locale.UK); 627 assertEquals(dt.toString(), "AM", f.print(dt)); 628 629 dt = dt.withZone(NEWYORK); 630 assertEquals(dt.toString(), "AM", f.print(dt)); 631 632 dt = dt.withZone(TOKYO); 633 assertEquals(dt.toString(), "PM", f.print(dt)); 634 } 635 636 public void testFormat_hourOfHalfday() { 638 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC); 639 DateTimeFormatter f = DateTimeFormat.forPattern("K").withLocale(Locale.UK); 640 assertEquals(dt.toString(), "10", f.print(dt)); 641 642 dt = dt.withZone(NEWYORK); 643 assertEquals(dt.toString(), "6", f.print(dt)); 644 645 dt = dt.withZone(TOKYO); 646 assertEquals(dt.toString(), "7", f.print(dt)); 647 648 dt = new DateTime(2004, 6, 9, 0, 0, 0, 0, UTC); 649 assertEquals(dt.toString(), "0", f.print(dt)); 650 } 651 652 public void testFormat_clockhourOfHalfday() { 654 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC); 655 DateTimeFormatter f = DateTimeFormat.forPattern("h").withLocale(Locale.UK); 656 assertEquals(dt.toString(), "10", f.print(dt)); 657 658 dt = dt.withZone(NEWYORK); 659 assertEquals(dt.toString(), "6", f.print(dt)); 660 661 dt = dt.withZone(TOKYO); 662 assertEquals(dt.toString(), "7", f.print(dt)); 663 664 dt = new DateTime(2004, 6, 9, 0, 0, 0, 0, UTC); 665 assertEquals(dt.toString(), "12", f.print(dt)); 666 } 667 668 public void testFormat_hourOfDay() { 670 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC); 671 DateTimeFormatter f = DateTimeFormat.forPattern("H").withLocale(Locale.UK); 672 assertEquals(dt.toString(), "10", f.print(dt)); 673 674 dt = dt.withZone(NEWYORK); 675 assertEquals(dt.toString(), "6", f.print(dt)); 676 677 dt = dt.withZone(TOKYO); 678 assertEquals(dt.toString(), "19", f.print(dt)); 679 680 dt = new DateTime(2004, 6, 9, 0, 0, 0, 0, UTC); 681 assertEquals(dt.toString(), "0", f.print(dt)); 682 } 683 684 public void testFormat_clockhourOfDay() { 686 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC); 687 DateTimeFormatter f = DateTimeFormat.forPattern("k").withLocale(Locale.UK); 688 assertEquals(dt.toString(), "10", f.print(dt)); 689 690 dt = dt.withZone(NEWYORK); 691 assertEquals(dt.toString(), "6", f.print(dt)); 692 693 dt = dt.withZone(TOKYO); 694 assertEquals(dt.toString(), "19", f.print(dt)); 695 696 dt = new DateTime(2004, 6, 9, 0, 0, 0, 0, UTC); 697 assertEquals(dt.toString(), "24", f.print(dt)); 698 } 699 700 public void testFormat_minute() { 702 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC); 703 DateTimeFormatter f = DateTimeFormat.forPattern("m").withLocale(Locale.UK); 704 assertEquals(dt.toString(), "20", f.print(dt)); 705 706 dt = dt.withZone(NEWYORK); 707 assertEquals(dt.toString(), "20", f.print(dt)); 708 709 dt = dt.withZone(TOKYO); 710 assertEquals(dt.toString(), "20", f.print(dt)); 711 } 712 713 public void testFormat_second() { 715 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC); 716 DateTimeFormatter f = DateTimeFormat.forPattern("s").withLocale(Locale.UK); 717 assertEquals(dt.toString(), "30", f.print(dt)); 718 719 dt = dt.withZone(NEWYORK); 720 assertEquals(dt.toString(), "30", f.print(dt)); 721 722 dt = dt.withZone(TOKYO); 723 assertEquals(dt.toString(), "30", f.print(dt)); 724 } 725 726 public void testFormat_fractionOfSecond() { 728 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC); 729 DateTimeFormatter f = DateTimeFormat.forPattern("SSS").withLocale(Locale.UK); 730 assertEquals(dt.toString(), "040", f.print(dt)); 731 732 dt = dt.withZone(NEWYORK); 733 assertEquals(dt.toString(), "040", f.print(dt)); 734 735 dt = dt.withZone(TOKYO); 736 assertEquals(dt.toString(), "040", f.print(dt)); 737 } 738 739 public void testFormat_fractionOfSecondLong() { 741 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC); 742 DateTimeFormatter f = DateTimeFormat.forPattern("SSSSSS").withLocale(Locale.UK); 743 assertEquals(dt.toString(), "040000", f.print(dt)); 744 745 dt = dt.withZone(NEWYORK); 746 assertEquals(dt.toString(), "040000", f.print(dt)); 747 748 dt = dt.withZone(TOKYO); 749 assertEquals(dt.toString(), "040000", f.print(dt)); 750 } 751 752 public void testFormat_zoneText() { 754 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC); 755 DateTimeFormatter f = DateTimeFormat.forPattern("z").withLocale(Locale.UK); 756 assertEquals(dt.toString(), "UTC", f.print(dt)); 757 758 dt = dt.withZone(NEWYORK); 759 assertEquals(dt.toString(), "EDT", f.print(dt)); 760 761 dt = dt.withZone(TOKYO); 762 assertEquals(dt.toString(), "JST", f.print(dt)); 763 } 764 765 public void testFormat_zoneLongText() { 766 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC); 767 DateTimeFormatter f = DateTimeFormat.forPattern("zzzz").withLocale(Locale.UK); 768 assertEquals(dt.toString(), "Coordinated Universal Time", f.print(dt)); 769 770 dt = dt.withZone(NEWYORK); 771 assertEquals(dt.toString(), "Eastern Daylight Time", f.print(dt)); 772 773 dt = dt.withZone(TOKYO); 774 assertEquals(dt.toString(), "Japan Standard Time", f.print(dt)); 775 } 776 777 public void testFormat_zoneAmount() { 779 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC); 780 DateTimeFormatter f = DateTimeFormat.forPattern("Z").withLocale(Locale.UK); 781 assertEquals(dt.toString(), "+0000", f.print(dt)); 782 783 dt = dt.withZone(NEWYORK); 784 assertEquals(dt.toString(), "-0400", f.print(dt)); 785 786 dt = dt.withZone(TOKYO); 787 assertEquals(dt.toString(), "+0900", f.print(dt)); 788 } 789 790 public void testFormat_zoneAmountColon() { 791 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC); 792 DateTimeFormatter f = DateTimeFormat.forPattern("ZZ").withLocale(Locale.UK); 793 assertEquals(dt.toString(), "+00:00", f.print(dt)); 794 795 dt = dt.withZone(NEWYORK); 796 assertEquals(dt.toString(), "-04:00", f.print(dt)); 797 798 dt = dt.withZone(TOKYO); 799 assertEquals(dt.toString(), "+09:00", f.print(dt)); 800 } 801 802 public void testFormat_zoneAmountID() { 803 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC); 804 DateTimeFormatter f = DateTimeFormat.forPattern("ZZZ").withLocale(Locale.UK); 805 assertEquals(dt.toString(), "UTC", f.print(dt)); 806 807 dt = dt.withZone(NEWYORK); 808 assertEquals(dt.toString(), "America/New_York", f.print(dt)); 809 810 dt = dt.withZone(TOKYO); 811 assertEquals(dt.toString(), "Asia/Tokyo", f.print(dt)); 812 } 813 814 public void testFormat_other() { 816 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC); 817 DateTimeFormatter f = DateTimeFormat.forPattern("'Hello' ''"); 818 assertEquals("Hello '", f.print(dt)); 819 } 820 821 public void testFormat_invalid() { 822 try { 823 DateTimeFormat.forPattern(null); 824 fail(); 825 } catch (IllegalArgumentException ex) {} 826 try { 827 DateTimeFormat.forPattern(""); 828 fail(); 829 } catch (IllegalArgumentException ex) {} 830 try { 831 DateTimeFormat.forPattern("A"); 832 fail(); 833 } catch (IllegalArgumentException ex) {} 834 try { 835 DateTimeFormat.forPattern("dd/mm/AA"); 836 fail(); 837 } catch (IllegalArgumentException ex) {} 838 } 839 840 public void testFormat_samples() { 841 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC); 842 DateTimeFormatter f = DateTimeFormat.forPattern("yyyy-MM-dd HH.mm.ss"); 843 assertEquals("2004-06-09 10.20.30", f.print(dt)); 844 } 845 846 public void testFormat_shortBasicParse() { 847 850 DateTime dt = new DateTime(2004, 3, 9, 0, 0, 0, 0); 851 852 DateTimeFormatter f = DateTimeFormat.forPattern("yyMMdd"); 853 assertEquals(dt, f.parseDateTime("040309")); 854 try { 855 assertEquals(dt, f.parseDateTime("20040309")); 856 fail(); 857 } catch (IllegalArgumentException ex) {} 858 859 f = DateTimeFormat.forPattern("yy/MM/dd"); 860 assertEquals(dt, f.parseDateTime("04/03/09")); 861 assertEquals(dt, f.parseDateTime("2004/03/09")); 862 } 863 864 } 865 | Popular Tags |