001 /*
002 * Copyright 2001-2006 Stephen Colebourne
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.joda.time.format;
017
018 import java.util.Locale;
019 import java.util.TimeZone;
020
021 import junit.framework.TestCase;
022 import junit.framework.TestSuite;
023
024 import org.joda.time.Chronology;
025 import org.joda.time.DateTime;
026 import org.joda.time.DateTimeConstants;
027 import org.joda.time.DateTimeFieldType;
028 import org.joda.time.DateTimeUtils;
029 import org.joda.time.DateTimeZone;
030 import org.joda.time.chrono.GJChronology;
031
032 /**
033 * This class is a Junit unit test for DateTime Formating.
034 *
035 * @author Stephen Colebourne
036 * @author Fredrik Borgh
037 */
038 public class TestDateTimeFormat extends TestCase {
039
040 private static final DateTimeZone UTC = DateTimeZone.UTC;
041 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
042 private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London");
043 private static final DateTimeZone TOKYO = DateTimeZone.forID("Asia/Tokyo");
044 private static final DateTimeZone NEWYORK = DateTimeZone.forID("America/New_York");
045
046 long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
047 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 +
048 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
049 366 + 365;
050 // 2002-06-09
051 private long TEST_TIME_NOW =
052 (y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
053
054 private DateTimeZone originalDateTimeZone = null;
055 private TimeZone originalTimeZone = null;
056 private Locale originalLocale = null;
057
058 public static void main(String[] args) {
059 junit.textui.TestRunner.run(suite());
060 }
061
062 public static TestSuite suite() {
063 return new TestSuite(TestDateTimeFormat.class);
064 }
065
066 public TestDateTimeFormat(String name) {
067 super(name);
068 }
069
070 protected void setUp() throws Exception {
071 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
072 originalDateTimeZone = DateTimeZone.getDefault();
073 originalTimeZone = TimeZone.getDefault();
074 originalLocale = Locale.getDefault();
075 DateTimeZone.setDefault(LONDON);
076 TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
077 Locale.setDefault(Locale.UK);
078 }
079
080 protected void tearDown() throws Exception {
081 DateTimeUtils.setCurrentMillisSystem();
082 DateTimeZone.setDefault(originalDateTimeZone);
083 TimeZone.setDefault(originalTimeZone);
084 Locale.setDefault(originalLocale);
085 originalDateTimeZone = null;
086 originalTimeZone = null;
087 originalLocale = null;
088 }
089
090 //-----------------------------------------------------------------------
091 public void testSubclassableConstructor() {
092 DateTimeFormat f = new DateTimeFormat() {
093 // test constructor is protected
094 };
095 assertNotNull(f);
096 }
097
098 //-----------------------------------------------------------------------
099 public void testFormat_era() {
100 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
101 DateTimeFormatter f = DateTimeFormat.forPattern("G").withLocale(Locale.UK);
102 assertEquals(dt.toString(), "AD", f.print(dt));
103
104 dt = dt.withZone(NEWYORK);
105 assertEquals(dt.toString(), "AD", f.print(dt));
106
107 dt = dt.withZone(PARIS);
108 assertEquals(dt.toString(), "AD", f.print(dt));
109 }
110
111 //-----------------------------------------------------------------------
112 public void testFormat_centuryOfEra() {
113 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
114 DateTimeFormatter f = DateTimeFormat.forPattern("C").withLocale(Locale.UK);
115 assertEquals(dt.toString(), "20", f.print(dt));
116
117 dt = dt.withZone(NEWYORK);
118 assertEquals(dt.toString(), "20", f.print(dt));
119
120 dt = dt.withZone(TOKYO);
121 assertEquals(dt.toString(), "20", f.print(dt));
122
123 dt = new DateTime(-123, 6, 9, 10, 20, 30, 40, UTC);
124 assertEquals(dt.toString(), "1", f.print(dt));
125 }
126
127 //-----------------------------------------------------------------------
128 public void testFormat_yearOfEra() {
129 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
130 DateTimeFormatter f = DateTimeFormat.forPattern("Y").withLocale(Locale.UK);
131 assertEquals(dt.toString(), "2004", f.print(dt));
132
133 dt = dt.withZone(NEWYORK);
134 assertEquals(dt.toString(), "2004", f.print(dt));
135
136 dt = dt.withZone(TOKYO);
137 assertEquals(dt.toString(), "2004", f.print(dt));
138
139 dt = new DateTime(-123, 6, 9, 10, 20, 30, 40, UTC);
140 assertEquals(dt.toString(), "124", f.print(dt)); // 124th year of BCE
141 }
142
143 public void testFormat_yearOfEra_twoDigit() {
144 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
145 DateTimeFormatter f = DateTimeFormat.forPattern("YY").withLocale(Locale.UK);
146 assertEquals(dt.toString(), "04", f.print(dt));
147
148 dt = new DateTime(-123, 6, 9, 10, 20, 30, 40, UTC);
149 assertEquals(dt.toString(), "23", f.print(dt));
150
151 // current time set to 2002-06-09
152 f = f.withZoneUTC();
153 DateTime expect = null;
154 expect = new DateTime(2004, 1, 1, 0, 0, 0, 0, UTC);
155 assertEquals(expect, f.parseDateTime("04"));
156
157 expect = new DateTime(1922, 1, 1, 0, 0, 0, 0, UTC);
158 assertEquals(expect, f.parseDateTime("22"));
159
160 expect = new DateTime(2021, 1, 1, 0, 0, 0, 0, UTC);
161 assertEquals(expect, f.parseDateTime("21"));
162
163 // Added tests to ensure single sign digit parse fails properly
164 try {
165 f.parseDateTime("-");
166 fail();
167 } catch (IllegalArgumentException ex) {}
168
169 try {
170 f.parseDateTime("+");
171 fail();
172 } catch (IllegalArgumentException ex) {}
173
174 // Added tests for pivot year setting
175 f = f.withPivotYear(new Integer(2050));
176 expect = new DateTime(2000, 1, 1, 0, 0, 0, 0, UTC);
177 assertEquals(expect, f.parseDateTime("00"));
178
179 expect = new DateTime(2099, 1, 1, 0, 0, 0, 0, UTC);
180 assertEquals(expect, f.parseDateTime("99"));
181
182 // Added tests to ensure two digit parsing is lenient for DateTimeFormat
183 f = DateTimeFormat.forPattern("YY").withLocale(Locale.UK);
184 f = f.withZoneUTC();
185 f.parseDateTime("5");
186 f.parseDateTime("005");
187 f.parseDateTime("+50");
188 f.parseDateTime("-50");
189 }
190
191 public void testFormat_yearOfEraParse() {
192 Chronology chrono = GJChronology.getInstanceUTC();
193
194 DateTimeFormatter f = DateTimeFormat
195 .forPattern("YYYY-MM GG")
196 .withChronology(chrono)
197 .withLocale(Locale.UK);
198
199 DateTime dt = new DateTime(2005, 10, 1, 0, 0, 0, 0, chrono);
200 assertEquals(dt, f.parseDateTime("2005-10 AD"));
201 assertEquals(dt, f.parseDateTime("2005-10 CE"));
202
203 dt = new DateTime(-2005, 10, 1, 0, 0, 0, 0, chrono);
204 assertEquals(dt, f.parseDateTime("2005-10 BC"));
205 assertEquals(dt, f.parseDateTime("2005-10 BCE"));
206 }
207
208 //-----------------------------------------------------------------------
209 public void testFormat_year() {
210 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
211 DateTimeFormatter f = DateTimeFormat.forPattern("y").withLocale(Locale.UK);
212 assertEquals(dt.toString(), "2004", f.print(dt));
213
214 dt = dt.withZone(NEWYORK);
215 assertEquals(dt.toString(), "2004", f.print(dt));
216
217 dt = dt.withZone(TOKYO);
218 assertEquals(dt.toString(), "2004", f.print(dt));
219
220 dt = new DateTime(-123, 6, 9, 10, 20, 30, 40, UTC);
221 assertEquals(dt.toString(), "-123", f.print(dt));
222
223 // Added tests to ensure single sign digit parse fails properly
224 try {
225 f.parseDateTime("-");
226 fail();
227 } catch (IllegalArgumentException ex) {}
228
229 try {
230 f.parseDateTime("+");
231 fail();
232 } catch (IllegalArgumentException ex) {}
233 }
234
235 public void testFormat_year_twoDigit() {
236 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
237 DateTimeFormatter f = DateTimeFormat.forPattern("yy").withLocale(Locale.UK);
238 assertEquals(dt.toString(), "04", f.print(dt));
239
240 dt = new DateTime(-123, 6, 9, 10, 20, 30, 40, UTC);
241 assertEquals(dt.toString(), "23", f.print(dt));
242
243 // current time set to 2002-06-09
244 f = f.withZoneUTC();
245 DateTime expect = null;
246 expect = new DateTime(2004, 1, 1, 0, 0, 0, 0, UTC);
247 assertEquals(expect, f.parseDateTime("04"));
248
249 expect = new DateTime(1922, 1, 1, 0, 0, 0, 0, UTC);
250 assertEquals(expect, f.parseDateTime("22"));
251
252 expect = new DateTime(2021, 1, 1, 0, 0, 0, 0, UTC);
253 assertEquals(expect, f.parseDateTime("21"));
254
255 // Added tests to ensure single sign digit parse fails properly
256 try {
257 f.parseDateTime("-");
258 fail();
259 } catch (IllegalArgumentException ex) {}
260
261 try {
262 f.parseDateTime("+");
263 fail();
264 } catch (IllegalArgumentException ex) {}
265
266 // Added tests for pivot year setting
267 f = f.withPivotYear(new Integer(2050));
268 expect = new DateTime(2000, 1, 1, 0, 0, 0, 0, UTC);
269 assertEquals(expect, f.parseDateTime("00"));
270
271 expect = new DateTime(2099, 1, 1, 0, 0, 0, 0, UTC);
272 assertEquals(expect, f.parseDateTime("99"));
273
274 // Added tests to ensure two digit parsing is strict by default for
275 // DateTimeFormatterBuilder
276 f = new DateTimeFormatterBuilder().appendTwoDigitYear(2000).toFormatter();
277 f = f.withZoneUTC();
278 try {
279 f.parseDateTime("5");
280 fail();
281 } catch (IllegalArgumentException ex) {}
282 try {
283 f.parseDateTime("005");
284 fail();
285 } catch (IllegalArgumentException ex) {}
286 try {
287 f.parseDateTime("+50");
288 fail();
289 } catch (IllegalArgumentException ex) {}
290 try {
291 f.parseDateTime("-50");
292 fail();
293 } catch (IllegalArgumentException ex) {}
294
295 // Added tests to ensure two digit parsing is lenient for DateTimeFormat
296 f = DateTimeFormat.forPattern("yy").withLocale(Locale.UK);
297 f = f.withZoneUTC();
298 f.parseDateTime("5");
299 f.parseDateTime("005");
300 f.parseDateTime("+50");
301 f.parseDateTime("-50");
302
303 // Added tests for lenient two digit parsing
304 f = new DateTimeFormatterBuilder().appendTwoDigitYear(2000, true).toFormatter();
305 f = f.withZoneUTC();
306 expect = new DateTime(2004, 1, 1, 0, 0, 0, 0, UTC);
307 assertEquals(expect, f.parseDateTime("04"));
308
309 expect = new DateTime(4, 1, 1, 0, 0, 0, 0, UTC);
310 assertEquals(expect, f.parseDateTime("+04"));
311
312 expect = new DateTime(-4, 1, 1, 0, 0, 0, 0, UTC);
313 assertEquals(expect, f.parseDateTime("-04"));
314
315 expect = new DateTime(4, 1, 1, 0, 0, 0, 0, UTC);
316 assertEquals(expect, f.parseDateTime("4"));
317
318 expect = new DateTime(-4, 1, 1, 0, 0, 0, 0, UTC);
319 assertEquals(expect, f.parseDateTime("-4"));
320
321 expect = new DateTime(4, 1, 1, 0, 0, 0, 0, UTC);
322 assertEquals(expect, f.parseDateTime("004"));
323
324 expect = new DateTime(4, 1, 1, 0, 0, 0, 0, UTC);
325 assertEquals(expect, f.parseDateTime("+004"));
326
327 expect = new DateTime(-4, 1, 1, 0, 0, 0, 0, UTC);
328 assertEquals(expect, f.parseDateTime("-004"));
329
330 expect = new DateTime(3004, 1, 1, 0, 0, 0, 0, UTC);
331 assertEquals(expect, f.parseDateTime("3004"));
332
333 expect = new DateTime(3004, 1, 1, 0, 0, 0, 0, UTC);
334 assertEquals(expect, f.parseDateTime("+3004"));
335
336 expect = new DateTime(-3004, 1, 1, 0, 0, 0, 0, UTC);
337 assertEquals(expect, f.parseDateTime("-3004"));
338
339 try {
340 f.parseDateTime("-");
341 fail();
342 } catch (IllegalArgumentException ex) {}
343
344 try {
345 f.parseDateTime("+");
346 fail();
347 } catch (IllegalArgumentException ex) {}
348 }
349
350 public void testFormat_year_long() {
351 DateTime dt = new DateTime(278004, 6, 9, 10, 20, 30, 40, UTC);
352 DateTimeFormatter f = DateTimeFormat.forPattern("yyyy");
353 assertEquals(dt.toString(), "278004", f.print(dt));
354
355 // for coverage
356 f = DateTimeFormat.forPattern("yyyyMMdd");
357 assertEquals(dt.toString(), "2780040609", f.print(dt));
358
359 // for coverage
360 f = DateTimeFormat.forPattern("yyyyddMM");
361 assertEquals(dt.toString(), "2780040906", f.print(dt));
362 }
363
364 //-----------------------------------------------------------------------
365 public void testFormat_weekyear() {
366 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
367 DateTimeFormatter f = DateTimeFormat.forPattern("x").withLocale(Locale.UK);
368 assertEquals(dt.toString(), "2004", f.print(dt));
369
370 dt = dt.withZone(NEWYORK);
371 assertEquals(dt.toString(), "2004", f.print(dt));
372
373 dt = dt.withZone(TOKYO);
374 assertEquals(dt.toString(), "2004", f.print(dt));
375
376 dt = new DateTime(-123, 6, 9, 10, 20, 30, 40, UTC);
377 assertEquals(dt.toString(), "-123", f.print(dt));
378 }
379
380 public void testFormat_weekyearOfEra_twoDigit() {
381 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
382 DateTimeFormatter f = DateTimeFormat.forPattern("xx").withLocale(Locale.UK);
383 assertEquals(dt.toString(), "04", f.print(dt));
384
385 dt = new DateTime(-123, 6, 9, 10, 20, 30, 40, UTC);
386 assertEquals(dt.toString(), "23", f.print(dt));
387
388 // current time set to 2002-06-09
389 f = f.withZoneUTC();
390 DateTime expect = null;
391 expect = new DateTime(2003, 12, 29, 0, 0, 0, 0, UTC);
392 assertEquals(expect, f.parseDateTime("04"));
393
394 expect = new DateTime(1922, 1, 2, 0, 0, 0, 0, UTC);
395 assertEquals(expect, f.parseDateTime("22"));
396
397 expect = new DateTime(2021, 1, 4, 0, 0, 0, 0, UTC);
398 assertEquals(expect, f.parseDateTime("21"));
399
400 // Added tests to ensure single sign digit parse fails properly
401 try {
402 f.parseDateTime("-");
403 fail();
404 } catch (IllegalArgumentException ex) {}
405
406 try {
407 f.parseDateTime("+");
408 fail();
409 } catch (IllegalArgumentException ex) {}
410
411 // Added tests for pivot year setting
412 f = f.withPivotYear(new Integer(2050));
413 expect = new DateTime(2000, 1, 3, 0, 0, 0, 0, DateTimeZone.UTC);
414 assertEquals(expect, f.parseDateTime("00"));
415
416 expect = new DateTime(2098, 12, 29, 0, 0, 0, 0, DateTimeZone.UTC);
417 assertEquals(expect, f.parseDateTime("99"));
418
419 // Added tests to ensure two digit parsing is strict by default for
420 // DateTimeFormatterBuilder
421 f = new DateTimeFormatterBuilder().appendTwoDigitWeekyear(2000).toFormatter();
422 f = f.withZoneUTC();
423 try {
424 f.parseDateTime("5");
425 fail();
426 } catch (IllegalArgumentException ex) {}
427 try {
428 f.parseDateTime("005");
429 fail();
430 } catch (IllegalArgumentException ex) {}
431 try {
432 f.parseDateTime("+50");
433 fail();
434 } catch (IllegalArgumentException ex) {}
435 try {
436 f.parseDateTime("-50");
437 fail();
438 } catch (IllegalArgumentException ex) {}
439
440 // Added tests to ensure two digit parsing is lenient for DateTimeFormat
441 f = DateTimeFormat.forPattern("xx").withLocale(Locale.UK);
442 f = f.withZoneUTC();
443 f.parseDateTime("5");
444 f.parseDateTime("005");
445 f.parseDateTime("+50");
446 f.parseDateTime("-50");
447
448 // Added tests for lenient two digit parsing
449 f = new DateTimeFormatterBuilder().appendTwoDigitWeekyear(2000, true).toFormatter();
450 f = f.withZoneUTC();
451 expect = new DateTime(2003, 12, 29, 0, 0, 0, 0, UTC);
452 assertEquals(expect, f.parseDateTime("04"));
453
454 expect = new DateTime(3, 12, 29, 0, 0, 0, 0, UTC);
455 assertEquals(expect, f.parseDateTime("+04"));
456
457 expect = new DateTime(-4, 1, 1, 0, 0, 0, 0, UTC);
458 assertEquals(expect, f.parseDateTime("-04"));
459
460 expect = new DateTime(3, 12, 29, 0, 0, 0, 0, UTC);
461 assertEquals(expect, f.parseDateTime("4"));
462
463 expect = new DateTime(-4, 1, 1, 0, 0, 0, 0, UTC);
464 assertEquals(expect, f.parseDateTime("-4"));
465
466 expect = new DateTime(3, 12, 29, 0, 0, 0, 0, UTC);
467 assertEquals(expect, f.parseDateTime("004"));
468
469 expect = new DateTime(3, 12, 29, 0, 0, 0, 0, UTC);
470 assertEquals(expect, f.parseDateTime("+004"));
471
472 expect = new DateTime(-4, 1, 1, 0, 0, 0, 0, UTC);
473 assertEquals(expect, f.parseDateTime("-004"));
474
475 expect = new DateTime(3004, 1, 2, 0, 0, 0, 0, UTC);
476 assertEquals(expect, f.parseDateTime("3004"));
477
478 expect = new DateTime(3004, 1, 2, 0, 0, 0, 0, UTC);
479 assertEquals(expect, f.parseDateTime("+3004"));
480
481 expect = new DateTime(-3004, 1, 4, 0, 0, 0, 0, UTC);
482 assertEquals(expect, f.parseDateTime("-3004"));
483
484 try {
485 f.parseDateTime("-");
486 fail();
487 } catch (IllegalArgumentException ex) {}
488
489 try {
490 f.parseDateTime("+");
491 fail();
492 } catch (IllegalArgumentException ex) {}
493 }
494
495 //-----------------------------------------------------------------------
496 public void testFormat_weekOfWeekyear() {
497 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
498 DateTimeFormatter f = DateTimeFormat.forPattern("w").withLocale(Locale.UK);
499 assertEquals(dt.toString(), "24", f.print(dt));
500
501 dt = dt.withZone(NEWYORK);
502 assertEquals(dt.toString(), "24", f.print(dt));
503
504 dt = dt.withZone(TOKYO);
505 assertEquals(dt.toString(), "24", f.print(dt));
506 }
507
508 //-----------------------------------------------------------------------
509 public void testFormat_dayOfWeek() {
510 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
511 DateTimeFormatter f = DateTimeFormat.forPattern("e").withLocale(Locale.UK);
512 assertEquals(dt.toString(), "3", f.print(dt));
513
514 dt = dt.withZone(NEWYORK);
515 assertEquals(dt.toString(), "3", f.print(dt));
516
517 dt = dt.withZone(TOKYO);
518 assertEquals(dt.toString(), "3", f.print(dt));
519 }
520
521 //-----------------------------------------------------------------------
522 public void testFormat_dayOfWeekShortText() {
523 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
524 DateTimeFormatter f = DateTimeFormat.forPattern("E").withLocale(Locale.UK);
525 assertEquals(dt.toString(), "Wed", f.print(dt));
526
527 dt = dt.withZone(NEWYORK);
528 assertEquals(dt.toString(), "Wed", f.print(dt));
529
530 dt = dt.withZone(TOKYO);
531 assertEquals(dt.toString(), "Wed", f.print(dt));
532
533 f = f.withLocale(Locale.FRENCH);
534 assertEquals(dt.toString(), "mer.", f.print(dt));
535 }
536
537 //-----------------------------------------------------------------------
538 public void testFormat_dayOfWeekText() {
539 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
540 DateTimeFormatter f = DateTimeFormat.forPattern("EEEE").withLocale(Locale.UK);
541 assertEquals(dt.toString(), "Wednesday", f.print(dt));
542
543 dt = dt.withZone(NEWYORK);
544 assertEquals(dt.toString(), "Wednesday", f.print(dt));
545
546 dt = dt.withZone(TOKYO);
547 assertEquals(dt.toString(), "Wednesday", f.print(dt));
548
549 f = f.withLocale(Locale.FRENCH);
550 assertEquals(dt.toString(), "mercredi", f.print(dt));
551 }
552
553 //-----------------------------------------------------------------------
554 public void testFormat_dayOfYearText() {
555 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
556 DateTimeFormatter f = DateTimeFormat.forPattern("D").withLocale(Locale.UK);
557 assertEquals(dt.toString(), "161", f.print(dt));
558
559 dt = dt.withZone(NEWYORK);
560 assertEquals(dt.toString(), "161", f.print(dt));
561
562 dt = dt.withZone(TOKYO);
563 assertEquals(dt.toString(), "161", f.print(dt));
564 }
565
566 //-----------------------------------------------------------------------
567 public void testFormat_monthOfYear() {
568 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
569 DateTimeFormatter f = DateTimeFormat.forPattern("M").withLocale(Locale.UK);
570 assertEquals(dt.toString(), "6", f.print(dt));
571
572 dt = dt.withZone(NEWYORK);
573 assertEquals(dt.toString(), "6", f.print(dt));
574
575 dt = dt.withZone(TOKYO);
576 assertEquals(dt.toString(), "6", f.print(dt));
577 }
578
579 //-----------------------------------------------------------------------
580 public void testFormat_monthOfYearShortText() {
581 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
582 DateTimeFormatter f = DateTimeFormat.forPattern("MMM").withLocale(Locale.UK);
583 assertEquals(dt.toString(), "Jun", f.print(dt));
584
585 dt = dt.withZone(NEWYORK);
586 assertEquals(dt.toString(), "Jun", f.print(dt));
587
588 dt = dt.withZone(TOKYO);
589 assertEquals(dt.toString(), "Jun", f.print(dt));
590
591 f = f.withLocale(Locale.FRENCH);
592 assertEquals(dt.toString(), "juin", f.print(dt));
593 }
594
595 //-----------------------------------------------------------------------
596 public void testFormat_monthOfYearText() {
597 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
598 DateTimeFormatter f = DateTimeFormat.forPattern("MMMM").withLocale(Locale.UK);
599 assertEquals(dt.toString(), "June", f.print(dt));
600
601 dt = dt.withZone(NEWYORK);
602 assertEquals(dt.toString(), "June", f.print(dt));
603
604 dt = dt.withZone(TOKYO);
605 assertEquals(dt.toString(), "June", f.print(dt));
606
607 f = f.withLocale(Locale.FRENCH);
608 assertEquals(dt.toString(), "juin", f.print(dt));
609 }
610
611 //-----------------------------------------------------------------------
612 public void testFormat_dayOfMonth() {
613 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
614 DateTimeFormatter f = DateTimeFormat.forPattern("d").withLocale(Locale.UK);
615 assertEquals(dt.toString(), "9", f.print(dt));
616
617 dt = dt.withZone(NEWYORK);
618 assertEquals(dt.toString(), "9", f.print(dt));
619
620 dt = dt.withZone(TOKYO);
621 assertEquals(dt.toString(), "9", f.print(dt));
622 }
623
624 //-----------------------------------------------------------------------
625 public void testFormat_halfdayOfDay() {
626 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
627 DateTimeFormatter f = DateTimeFormat.forPattern("a").withLocale(Locale.UK);
628 assertEquals(dt.toString(), "AM", f.print(dt));
629
630 dt = dt.withZone(NEWYORK);
631 assertEquals(dt.toString(), "AM", f.print(dt));
632
633 dt = dt.withZone(TOKYO);
634 assertEquals(dt.toString(), "PM", f.print(dt));
635 }
636
637 //-----------------------------------------------------------------------
638 public void testFormat_hourOfHalfday() {
639 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
640 DateTimeFormatter f = DateTimeFormat.forPattern("K").withLocale(Locale.UK);
641 assertEquals(dt.toString(), "10", f.print(dt));
642
643 dt = dt.withZone(NEWYORK);
644 assertEquals(dt.toString(), "6", f.print(dt));
645
646 dt = dt.withZone(TOKYO);
647 assertEquals(dt.toString(), "7", f.print(dt));
648
649 dt = new DateTime(2004, 6, 9, 0, 0, 0, 0, UTC);
650 assertEquals(dt.toString(), "0", f.print(dt));
651 }
652
653 //-----------------------------------------------------------------------
654 public void testFormat_clockhourOfHalfday() {
655 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
656 DateTimeFormatter f = DateTimeFormat.forPattern("h").withLocale(Locale.UK);
657 assertEquals(dt.toString(), "10", f.print(dt));
658
659 dt = dt.withZone(NEWYORK);
660 assertEquals(dt.toString(), "6", f.print(dt));
661
662 dt = dt.withZone(TOKYO);
663 assertEquals(dt.toString(), "7", f.print(dt));
664
665 dt = new DateTime(2004, 6, 9, 0, 0, 0, 0, UTC);
666 assertEquals(dt.toString(), "12", f.print(dt));
667 }
668
669 //-----------------------------------------------------------------------
670 public void testFormat_hourOfDay() {
671 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
672 DateTimeFormatter f = DateTimeFormat.forPattern("H").withLocale(Locale.UK);
673 assertEquals(dt.toString(), "10", f.print(dt));
674
675 dt = dt.withZone(NEWYORK);
676 assertEquals(dt.toString(), "6", f.print(dt));
677
678 dt = dt.withZone(TOKYO);
679 assertEquals(dt.toString(), "19", f.print(dt));
680
681 dt = new DateTime(2004, 6, 9, 0, 0, 0, 0, UTC);
682 assertEquals(dt.toString(), "0", f.print(dt));
683 }
684
685 //-----------------------------------------------------------------------
686 public void testFormat_clockhourOfDay() {
687 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
688 DateTimeFormatter f = DateTimeFormat.forPattern("k").withLocale(Locale.UK);
689 assertEquals(dt.toString(), "10", f.print(dt));
690
691 dt = dt.withZone(NEWYORK);
692 assertEquals(dt.toString(), "6", f.print(dt));
693
694 dt = dt.withZone(TOKYO);
695 assertEquals(dt.toString(), "19", f.print(dt));
696
697 dt = new DateTime(2004, 6, 9, 0, 0, 0, 0, UTC);
698 assertEquals(dt.toString(), "24", f.print(dt));
699 }
700
701 //-----------------------------------------------------------------------
702 public void testFormat_minute() {
703 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
704 DateTimeFormatter f = DateTimeFormat.forPattern("m").withLocale(Locale.UK);
705 assertEquals(dt.toString(), "20", f.print(dt));
706
707 dt = dt.withZone(NEWYORK);
708 assertEquals(dt.toString(), "20", f.print(dt));
709
710 dt = dt.withZone(TOKYO);
711 assertEquals(dt.toString(), "20", f.print(dt));
712 }
713
714 //-----------------------------------------------------------------------
715 public void testFormat_second() {
716 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
717 DateTimeFormatter f = DateTimeFormat.forPattern("s").withLocale(Locale.UK);
718 assertEquals(dt.toString(), "30", f.print(dt));
719
720 dt = dt.withZone(NEWYORK);
721 assertEquals(dt.toString(), "30", f.print(dt));
722
723 dt = dt.withZone(TOKYO);
724 assertEquals(dt.toString(), "30", f.print(dt));
725 }
726
727 //-----------------------------------------------------------------------
728 public void testFormat_fractionOfSecond() {
729 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
730 DateTimeFormatter f = DateTimeFormat.forPattern("SSS").withLocale(Locale.UK);
731 assertEquals(dt.toString(), "040", f.print(dt));
732
733 dt = dt.withZone(NEWYORK);
734 assertEquals(dt.toString(), "040", f.print(dt));
735
736 dt = dt.withZone(TOKYO);
737 assertEquals(dt.toString(), "040", f.print(dt));
738 }
739
740 //-----------------------------------------------------------------------
741 public void testFormat_fractionOfSecondLong() {
742 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
743 DateTimeFormatter f = DateTimeFormat.forPattern("SSSSSS").withLocale(Locale.UK);
744 assertEquals(dt.toString(), "040000", f.print(dt));
745
746 dt = dt.withZone(NEWYORK);
747 assertEquals(dt.toString(), "040000", f.print(dt));
748
749 dt = dt.withZone(TOKYO);
750 assertEquals(dt.toString(), "040000", f.print(dt));
751 }
752
753 //-----------------------------------------------------------------------
754 public void testFormat_zoneText() {
755 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
756 DateTimeFormatter f = DateTimeFormat.forPattern("z").withLocale(Locale.UK);
757 assertEquals(dt.toString(), "UTC", f.print(dt));
758
759 dt = dt.withZone(NEWYORK);
760 assertEquals(dt.toString(), "EDT", f.print(dt));
761
762 dt = dt.withZone(TOKYO);
763 assertEquals(dt.toString(), "JST", f.print(dt));
764 }
765
766 public void testFormat_zoneLongText() {
767 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
768 DateTimeFormatter f = DateTimeFormat.forPattern("zzzz").withLocale(Locale.UK);
769 assertEquals(dt.toString(), "Coordinated Universal Time", f.print(dt));
770
771 dt = dt.withZone(NEWYORK);
772 assertEquals(dt.toString(), "Eastern Daylight Time", f.print(dt));
773
774 dt = dt.withZone(TOKYO);
775 assertEquals(dt.toString(), "Japan Standard Time", f.print(dt));
776 }
777
778 //-----------------------------------------------------------------------
779 public void testFormat_zoneAmount() {
780 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
781 DateTimeFormatter f = DateTimeFormat.forPattern("Z").withLocale(Locale.UK);
782 assertEquals(dt.toString(), "+0000", f.print(dt));
783
784 dt = dt.withZone(NEWYORK);
785 assertEquals(dt.toString(), "-0400", f.print(dt));
786
787 dt = dt.withZone(TOKYO);
788 assertEquals(dt.toString(), "+0900", f.print(dt));
789 }
790
791 public void testFormat_zoneAmountColon() {
792 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
793 DateTimeFormatter f = DateTimeFormat.forPattern("ZZ").withLocale(Locale.UK);
794 assertEquals(dt.toString(), "+00:00", f.print(dt));
795
796 dt = dt.withZone(NEWYORK);
797 assertEquals(dt.toString(), "-04:00", f.print(dt));
798
799 dt = dt.withZone(TOKYO);
800 assertEquals(dt.toString(), "+09:00", f.print(dt));
801 }
802
803 public void testFormat_zoneAmountID() {
804 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
805 DateTimeFormatter f = DateTimeFormat.forPattern("ZZZ").withLocale(Locale.UK);
806 assertEquals(dt.toString(), "UTC", f.print(dt));
807
808 dt = dt.withZone(NEWYORK);
809 assertEquals(dt.toString(), "America/New_York", f.print(dt));
810
811 dt = dt.withZone(TOKYO);
812 assertEquals(dt.toString(), "Asia/Tokyo", f.print(dt));
813 }
814
815 //-----------------------------------------------------------------------
816 public void testFormat_other() {
817 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
818 DateTimeFormatter f = DateTimeFormat.forPattern("'Hello' ''");
819 assertEquals("Hello '", f.print(dt));
820 }
821
822 public void testFormat_invalid() {
823 try {
824 DateTimeFormat.forPattern(null);
825 fail();
826 } catch (IllegalArgumentException ex) {}
827 try {
828 DateTimeFormat.forPattern("");
829 fail();
830 } catch (IllegalArgumentException ex) {}
831 try {
832 DateTimeFormat.forPattern("A");
833 fail();
834 } catch (IllegalArgumentException ex) {}
835 try {
836 DateTimeFormat.forPattern("dd/mm/AA");
837 fail();
838 } catch (IllegalArgumentException ex) {}
839 }
840
841 public void testFormat_samples() {
842 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
843 DateTimeFormatter f = DateTimeFormat.forPattern("yyyy-MM-dd HH.mm.ss");
844 assertEquals("2004-06-09 10.20.30", f.print(dt));
845 }
846
847 public void testFormat_shortBasicParse() {
848 // Tests special two digit parse to make sure it properly switches
849 // between lenient and strict parsing.
850
851 DateTime dt = new DateTime(2004, 3, 9, 0, 0, 0, 0);
852
853 DateTimeFormatter f = DateTimeFormat.forPattern("yyMMdd");
854 assertEquals(dt, f.parseDateTime("040309"));
855 try {
856 assertEquals(dt, f.parseDateTime("20040309"));
857 fail();
858 } catch (IllegalArgumentException ex) {}
859
860 f = DateTimeFormat.forPattern("yy/MM/dd");
861 assertEquals(dt, f.parseDateTime("04/03/09"));
862 assertEquals(dt, f.parseDateTime("2004/03/09"));
863 }
864
865 //-----------------------------------------------------------------------
866 public void testParse_pivotYear() {
867 DateTimeFormatter dateFormatter = DateTimeFormat.forPattern("dd.MM.yy").withPivotYear(2050).withZoneUTC();
868
869 DateTime date = dateFormatter.parseDateTime("25.12.15");
870 assertEquals(date.getYear(), 2015);
871
872 date = dateFormatter.parseDateTime("25.12.00");
873 assertEquals(date.getYear(), 2000);
874
875 date = dateFormatter.parseDateTime("25.12.99");
876 assertEquals(date.getYear(), 2099);
877 }
878
879 public void testParse_pivotYear_ignored4DigitYear() {
880 DateTimeFormatter dateFormatter = DateTimeFormat.forPattern("dd.MM.yyyy").withPivotYear(2050).withZoneUTC();
881
882 DateTime date = dateFormatter.parseDateTime("25.12.15");
883 assertEquals(date.getYear(), 15);
884
885 date = dateFormatter.parseDateTime("25.12.00");
886 assertEquals(date.getYear(), 0);
887
888 date = dateFormatter.parseDateTime("25.12.99");
889 assertEquals(date.getYear(), 99);
890 }
891
892 //-----------------------------------------------------------------------
893 public void testFormatParse_textMonthJanShort_UK() {
894 DateTimeFormatter dateFormatter = DateTimeFormat.forPattern("dd MMM yyyy")
895 .withLocale(Locale.UK).withZoneUTC();
896
897 String str = new DateTime(2007, 1, 23, 0, 0, 0, 0, UTC).toString(dateFormatter);
898 assertEquals(str, "23 Jan 2007");
899 DateTime date = dateFormatter.parseDateTime(str);
900 check(date, 2007, 1, 23);
901 }
902
903 public void testFormatParse_textMonthJanShortLowerCase_UK() {
904 DateTimeFormatter dateFormatter = DateTimeFormat.forPattern("dd MMM yyyy")
905 .withLocale(Locale.UK).withZoneUTC();
906 DateTime date = dateFormatter.parseDateTime("23 jan 2007");
907 check(date, 2007, 1, 23);
908 }
909
910 public void testFormatParse_textMonthJanShortUpperCase_UK() {
911 DateTimeFormatter dateFormatter = DateTimeFormat.forPattern("dd MMM yyyy")
912 .withLocale(Locale.UK).withZoneUTC();
913 DateTime date = dateFormatter.parseDateTime("23 JAN 2007");
914 check(date, 2007, 1, 23);
915 }
916
917 public void testParse_textMonthJanLong_UK() {
918 DateTimeFormatter dateFormatter = DateTimeFormat.forPattern("dd MMM yyyy")
919 .withLocale(Locale.UK).withZoneUTC();
920
921 DateTime date = dateFormatter.parseDateTime("23 January 2007");
922 check(date, 2007, 1, 23);
923 }
924
925 public void testFormatParse_textMonthJanLongLowerCase_UK() {
926 DateTimeFormatter dateFormatter = DateTimeFormat.forPattern("dd MMM yyyy")
927 .withLocale(Locale.UK).withZoneUTC();
928 DateTime date = dateFormatter.parseDateTime("23 january 2007");
929 check(date, 2007, 1, 23);
930 }
931
932 public void testFormatParse_textMonthJanLongUpperCase_UK() {
933 DateTimeFormatter dateFormatter = DateTimeFormat.forPattern("dd MMM yyyy")
934 .withLocale(Locale.UK).withZoneUTC();
935 DateTime date = dateFormatter.parseDateTime("23 JANUARY 2007");
936 check(date, 2007, 1, 23);
937 }
938
939 public void testFormatParse_textMonthJanShort_France() {
940 DateTimeFormatter dateFormatter = DateTimeFormat.forPattern("dd MMM yyyy")
941 .withLocale(Locale.FRANCE).withZoneUTC();
942
943 String str = new DateTime(2007, 1, 23, 0, 0, 0, 0, UTC).toString(dateFormatter);
944 assertEquals("23 janv. 2007", str);
945 DateTime date = dateFormatter.parseDateTime(str);
946 check(date, 2007, 1, 23);
947 }
948
949 public void testFormatParse_textMonthJanLong_France() {
950 DateTimeFormatter dateFormatter = DateTimeFormat.forPattern("dd MMM yyyy")
951 .withLocale(Locale.FRANCE).withZoneUTC();
952
953 DateTime date = dateFormatter.parseDateTime("23 janvier 2007");
954 check(date, 2007, 1, 23);
955 }
956
957 public void testFormatParse_textMonthApr_France() {
958 DateTimeFormatter dateFormatter = DateTimeFormat.forPattern("dd MMM yyyy")
959 .withLocale(Locale.FRANCE).withZoneUTC();
960
961 String str = new DateTime(2007, 2, 23, 0, 0, 0, 0, UTC).toString(dateFormatter);
962 assertEquals("23 f\u00E9vr. 2007", str); // e acute
963 DateTime date = dateFormatter.parseDateTime(str);
964 check(date, 2007, 2, 23);
965 }
966
967 public void testFormatParse_textMonthAtEnd_France() {
968 DateTimeFormatter dateFormatter = DateTimeFormat.forPattern("dd MMM")
969 .withLocale(Locale.FRANCE).withZoneUTC();
970
971 String str = new DateTime(2007, 6, 23, 0, 0, 0, 0, UTC).toString(dateFormatter);
972 assertEquals("23 juin", str);
973 DateTime date = dateFormatter.parseDateTime(str);
974 check(date, 2000, 6, 23);
975 }
976
977 public void testFormatParse_textMonthAtEnd_France_withSpecifiedDefault() {
978 DateTimeFormatter dateFormatter = DateTimeFormat.forPattern("dd MMM")
979 .withLocale(Locale.FRANCE).withZoneUTC().withDefaultYear(1980);
980
981 String str = new DateTime(2007, 6, 23, 0, 0, 0, 0, UTC).toString(dateFormatter);
982 assertEquals("23 juin", str);
983 DateTime date = dateFormatter.parseDateTime(str);
984 check(date, 1980, 6, 23);
985 }
986
987 public void testFormatParse_textMonthApr_Korean() {
988 DateTimeFormatter dateFormatter = DateTimeFormat.forPattern("EEEE, d MMMM yyyy HH:mm")
989 .withLocale(Locale.KOREAN).withZoneUTC();
990
991 String str = new DateTime(2007, 3, 8, 22, 0, 0, 0, UTC).toString(dateFormatter);
992 DateTime date = dateFormatter.parseDateTime(str);
993 assertEquals(new DateTime(2007, 3, 8, 22, 0, 0, 0, UTC), date);
994 }
995
996 //-----------------------------------------------------------------------
997 public void testFormatParse_textHalfdayAM_UK() {
998 DateTimeFormatter dateFormatter = new DateTimeFormatterBuilder()
999 .appendLiteral('$')
1000 .appendClockhourOfHalfday(2)
1001 .appendLiteral('-')
1002 .appendHalfdayOfDayText()
1003 .appendLiteral('-')
1004 .appendYear(4, 4)
1005 .toFormatter()
1006 .withLocale(Locale.UK).withZoneUTC();
1007
1008 String str = new DateTime(2007, 6, 23, 18, 0, 0, 0, UTC).toString(dateFormatter);
1009 assertEquals("$06-PM-2007", str);
1010 DateTime date = dateFormatter.parseDateTime(str);
1011 check(date, 2007, 1, 1);
1012 }
1013
1014 public void testFormatParse_textHalfdayAM_France() {
1015 DateTimeFormatter dateFormatter = new DateTimeFormatterBuilder()
1016 .appendLiteral('$')
1017 .appendClockhourOfHalfday(2)
1018 .appendLiteral('-')
1019 .appendHalfdayOfDayText()
1020 .appendLiteral('-')
1021 .appendYear(4, 4)
1022 .toFormatter()
1023 .withLocale(Locale.FRANCE).withZoneUTC();
1024
1025 String str = new DateTime(2007, 6, 23, 18, 0, 0, 0, UTC).toString(dateFormatter);
1026 assertEquals("$06-PM-2007", str);
1027 DateTime date = dateFormatter.parseDateTime(str);
1028 check(date, 2007, 1, 1);
1029 }
1030
1031 //-----------------------------------------------------------------------
1032 public void testFormatParse_textEraAD_UK() {
1033 DateTimeFormatter dateFormatter = new DateTimeFormatterBuilder()
1034 .appendLiteral('$')
1035 .appendEraText()
1036 .appendYear(4, 4)
1037 .toFormatter()
1038 .withLocale(Locale.UK).withZoneUTC();
1039
1040 String str = new DateTime(2007, 6, 23, 0, 0, 0, 0, UTC).toString(dateFormatter);
1041 assertEquals("$AD2007", str);
1042 DateTime date = dateFormatter.parseDateTime(str);
1043 check(date, 2007, 1, 1);
1044 }
1045
1046 public void testFormatParse_textEraAD_France() {
1047 DateTimeFormatter dateFormatter = new DateTimeFormatterBuilder()
1048 .appendLiteral('$')
1049 .appendEraText()
1050 .appendYear(4, 4)
1051 .toFormatter()
1052 .withLocale(Locale.FRANCE).withZoneUTC();
1053
1054 String str = new DateTime(2007, 6, 23, 0, 0, 0, 0, UTC).toString(dateFormatter);
1055 assertEquals("$ap. J.-C.2007", str);
1056 DateTime date = dateFormatter.parseDateTime(str);
1057 check(date, 2007, 1, 1);
1058 }
1059
1060 public void testFormatParse_textEraBC_France() {
1061 DateTimeFormatter dateFormatter = new DateTimeFormatterBuilder()
1062 .appendLiteral('$')
1063 .appendEraText()
1064 .appendYear(4, 4)
1065 .toFormatter()
1066 .withLocale(Locale.FRANCE).withZoneUTC();
1067
1068 String str = new DateTime(-1, 6, 23, 0, 0, 0, 0, UTC).toString(dateFormatter);
1069 assertEquals("$BC-0001", str);
1070 DateTime date = dateFormatter.parseDateTime(str);
1071 check(date, -1, 1, 1);
1072 }
1073
1074 //-----------------------------------------------------------------------
1075 public void testFormatParse_textYear_UK() {
1076 DateTimeFormatter dateFormatter = new DateTimeFormatterBuilder()
1077 .appendLiteral('$')
1078 .appendText(DateTimeFieldType.year())
1079 .toFormatter()
1080 .withLocale(Locale.UK).withZoneUTC();
1081
1082 String str = new DateTime(2007, 6, 23, 0, 0, 0, 0, UTC).toString(dateFormatter);
1083 assertEquals("$2007", str);
1084 try {
1085 dateFormatter.parseDateTime(str);
1086 fail();
1087 } catch (IllegalArgumentException ex) {
1088 // expected
1089 }
1090 }
1091
1092 public void testFormatParse_textYear_France() {
1093 DateTimeFormatter dateFormatter = new DateTimeFormatterBuilder()
1094 .appendLiteral('$')
1095 .appendText(DateTimeFieldType.year())
1096 .toFormatter()
1097 .withLocale(Locale.FRANCE).withZoneUTC();
1098
1099 String str = new DateTime(2007, 6, 23, 0, 0, 0, 0, UTC).toString(dateFormatter);
1100 assertEquals("$2007", str);
1101 try {
1102 dateFormatter.parseDateTime(str);
1103 fail();
1104 } catch (IllegalArgumentException ex) {
1105 // expected
1106 }
1107 }
1108
1109 //-----------------------------------------------------------------------
1110 public void testFormatParse_textAdjoiningHelloWorld_UK() {
1111 DateTimeFormatter dateFormatter = new DateTimeFormatterBuilder()
1112 .appendLiteral('$')
1113 .appendDayOfMonth(2)
1114 .appendMonthOfYearShortText()
1115 .appendLiteral("HelloWorld")
1116 .toFormatter()
1117 .withLocale(Locale.UK).withZoneUTC();
1118
1119 String str = new DateTime(2007, 6, 23, 0, 0, 0, 0, UTC).toString(dateFormatter);
1120 assertEquals("$23JunHelloWorld", str);
1121 dateFormatter.parseDateTime(str);
1122 }
1123
1124 public void testFormatParse_textAdjoiningMonthDOW_UK() {
1125 DateTimeFormatter dateFormatter = new DateTimeFormatterBuilder()
1126 .appendLiteral('$')
1127 .appendDayOfMonth(2)
1128 .appendMonthOfYearShortText()
1129 .appendDayOfWeekShortText()
1130 .toFormatter()
1131 .withLocale(Locale.UK).withZoneUTC();
1132
1133 String str = new DateTime(2007, 6, 23, 0, 0, 0, 0, UTC).toString(dateFormatter);
1134 assertEquals("$23JunSat", str);
1135 dateFormatter.parseDateTime(str);
1136 }
1137
1138 //-----------------------------------------------------------------------
1139 public void testFormatParse_zoneId_noColon() {
1140 DateTimeFormatter dateFormatter = DateTimeFormat.forPattern("HH:mm Z").withZoneUTC();
1141 String str = new DateTime(2007, 6, 23, 1, 2, 0, 0, UTC).toString(dateFormatter);
1142 assertEquals("01:02 +0000", str);
1143 DateTime parsed = dateFormatter.parseDateTime(str);
1144 assertEquals(1, parsed.getHourOfDay());
1145 assertEquals(2, parsed.getMinuteOfHour());
1146 }
1147
1148 public void testFormatParse_zoneId_noColon_parseZ() {
1149 DateTimeFormatter dateFormatter = DateTimeFormat.forPattern("HH:mm Z").withZoneUTC();
1150 DateTime parsed = dateFormatter.parseDateTime("01:02 Z");
1151 assertEquals(1, parsed.getHourOfDay());
1152 assertEquals(2, parsed.getMinuteOfHour());
1153 }
1154
1155 public void testFormatParse_zoneId_colon() {
1156 DateTimeFormatter dateFormatter = DateTimeFormat.forPattern("HH:mm ZZ").withZoneUTC();
1157 String str = new DateTime(2007, 6, 23, 1, 2, 0, 0, UTC).toString(dateFormatter);
1158 assertEquals("01:02 +00:00", str);
1159 DateTime parsed = dateFormatter.parseDateTime(str);
1160 assertEquals(1, parsed.getHourOfDay());
1161 assertEquals(2, parsed.getMinuteOfHour());
1162 }
1163
1164 public void testFormatParse_zoneId_colon_parseZ() {
1165 DateTimeFormatter dateFormatter = DateTimeFormat.forPattern("HH:mm ZZ").withZoneUTC();
1166 DateTime parsed = dateFormatter.parseDateTime("01:02 Z");
1167 assertEquals(1, parsed.getHourOfDay());
1168 assertEquals(2, parsed.getMinuteOfHour());
1169 }
1170
1171 //-----------------------------------------------------------------------
1172 private void check(DateTime test, int hour, int min, int sec) {
1173 assertEquals(hour, test.getYear());
1174 assertEquals(min, test.getMonthOfYear());
1175 assertEquals(sec, test.getDayOfMonth());
1176 }
1177
1178 }