KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joda > time > format > TestDateTimeFormatter


1 /*
2  * Copyright 2001-2006 Stephen Colebourne
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.joda.time.format;
17
18 import java.io.CharArrayWriter JavaDoc;
19 import java.util.Locale JavaDoc;
20 import java.util.TimeZone JavaDoc;
21
22 import junit.framework.TestCase;
23 import junit.framework.TestSuite;
24
25 import org.joda.time.Chronology;
26 import org.joda.time.DateTime;
27 import org.joda.time.DateTimeConstants;
28 import org.joda.time.DateTimeUtils;
29 import org.joda.time.DateTimeZone;
30 import org.joda.time.MutableDateTime;
31 import org.joda.time.ReadablePartial;
32 import org.joda.time.chrono.BuddhistChronology;
33 import org.joda.time.chrono.ISOChronology;
34
35 /**
36  * This class is a Junit unit test for DateTime Formating.
37  *
38  * @author Stephen Colebourne
39  */

40 public class TestDateTimeFormatter extends TestCase {
41
42     private static final DateTimeZone UTC = DateTimeZone.UTC;
43     private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris");
44     private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London");
45     private static final DateTimeZone TOKYO = DateTimeZone.forID("Asia/Tokyo");
46     private static final DateTimeZone NEWYORK = DateTimeZone.forID("America/New_York");
47     private static final Chronology ISO_UTC = ISOChronology.getInstanceUTC();
48     private static final Chronology ISO_PARIS = ISOChronology.getInstance(PARIS);
49     private static final Chronology BUDDHIST_PARIS = BuddhistChronology.getInstance(PARIS);
50
51     long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
52                      366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 +
53                      365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 +
54                      366 + 365;
55     // 2002-06-09
56
private long TEST_TIME_NOW =
57             (y2002days + 31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY;
58
59     private DateTimeZone originalDateTimeZone = null;
60     private TimeZone JavaDoc originalTimeZone = null;
61     private Locale JavaDoc originalLocale = null;
62     private DateTimeFormatter f = null;
63     private DateTimeFormatter g = null;
64
65     public static void main(String JavaDoc[] args) {
66         junit.textui.TestRunner.run(suite());
67     }
68
69     public static TestSuite suite() {
70         return new TestSuite(TestDateTimeFormatter.class);
71     }
72
73     public TestDateTimeFormatter(String JavaDoc name) {
74         super(name);
75     }
76
77     protected void setUp() throws Exception JavaDoc {
78         DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
79         originalDateTimeZone = DateTimeZone.getDefault();
80         originalTimeZone = TimeZone.getDefault();
81         originalLocale = Locale.getDefault();
82         DateTimeZone.setDefault(LONDON);
83         TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
84         Locale.setDefault(Locale.UK);
85         f = new DateTimeFormatterBuilder()
86                 .appendDayOfWeekShortText()
87                 .appendLiteral(' ')
88                 .append(ISODateTimeFormat.dateTimeNoMillis())
89                 .toFormatter();
90         g = ISODateTimeFormat.dateTimeNoMillis();
91     }
92
93     protected void tearDown() throws Exception JavaDoc {
94         DateTimeUtils.setCurrentMillisSystem();
95         DateTimeZone.setDefault(originalDateTimeZone);
96         TimeZone.setDefault(originalTimeZone);
97         Locale.setDefault(originalLocale);
98         originalDateTimeZone = null;
99         originalTimeZone = null;
100         originalLocale = null;
101         f = null;
102         g = null;
103     }
104
105     //-----------------------------------------------------------------------
106
public void testPrint_simple() {
107         DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
108         assertEquals("Wed 2004-06-09T10:20:30Z", f.print(dt));
109         
110         dt = dt.withZone(PARIS);
111         assertEquals("Wed 2004-06-09T12:20:30+02:00", f.print(dt));
112         
113         dt = dt.withZone(NEWYORK);
114         assertEquals("Wed 2004-06-09T06:20:30-04:00", f.print(dt));
115         
116         dt = dt.withChronology(BUDDHIST_PARIS);
117         assertEquals("Wed 2547-06-09T12:20:30+02:00", f.print(dt));
118     }
119
120     //-----------------------------------------------------------------------
121
public void testPrint_locale() {
122         DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
123         assertEquals("mer. 2004-06-09T10:20:30Z", f.withLocale(Locale.FRENCH).print(dt));
124         assertEquals("Wed 2004-06-09T10:20:30Z", f.withLocale(null).print(dt));
125     }
126
127     //-----------------------------------------------------------------------
128
public void testPrint_zone() {
129         DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
130         assertEquals("Wed 2004-06-09T06:20:30-04:00", f.withZone(NEWYORK).print(dt));
131         assertEquals("Wed 2004-06-09T12:20:30+02:00", f.withZone(PARIS).print(dt));
132         assertEquals("Wed 2004-06-09T10:20:30Z", f.withZone(null).print(dt));
133         
134         dt = dt.withZone(NEWYORK);
135         assertEquals("Wed 2004-06-09T06:20:30-04:00", f.withZone(NEWYORK).print(dt));
136         assertEquals("Wed 2004-06-09T12:20:30+02:00", f.withZone(PARIS).print(dt));
137         assertEquals("Wed 2004-06-09T10:20:30Z", f.withZone(UTC).print(dt));
138         assertEquals("Wed 2004-06-09T06:20:30-04:00", f.withZone(null).print(dt));
139     }
140
141     //-----------------------------------------------------------------------
142
public void testPrint_chrono() {
143         DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
144         assertEquals("Wed 2004-06-09T12:20:30+02:00", f.withChronology(ISO_PARIS).print(dt));
145         assertEquals("Wed 2547-06-09T12:20:30+02:00", f.withChronology(BUDDHIST_PARIS).print(dt));
146         assertEquals("Wed 2004-06-09T10:20:30Z", f.withChronology(null).print(dt));
147         
148         dt = dt.withChronology(BUDDHIST_PARIS);
149         assertEquals("Wed 2004-06-09T12:20:30+02:00", f.withChronology(ISO_PARIS).print(dt));
150         assertEquals("Wed 2547-06-09T12:20:30+02:00", f.withChronology(BUDDHIST_PARIS).print(dt));
151         assertEquals("Wed 2004-06-09T10:20:30Z", f.withChronology(ISO_UTC).print(dt));
152         assertEquals("Wed 2547-06-09T12:20:30+02:00", f.withChronology(null).print(dt));
153     }
154
155     //-----------------------------------------------------------------------
156
public void testPrint_bufferMethods() throws Exception JavaDoc {
157         DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
158         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
159         f.printTo(buf, dt);
160         assertEquals("Wed 2004-06-09T10:20:30Z", buf.toString());
161         
162         buf = new StringBuffer JavaDoc();
163         f.printTo(buf, dt.getMillis());
164         assertEquals("Wed 2004-06-09T11:20:30+01:00", buf.toString());
165         
166         buf = new StringBuffer JavaDoc();
167         ISODateTimeFormat.yearMonthDay().printTo(buf, dt.toYearMonthDay());
168         assertEquals("2004-06-09", buf.toString());
169         
170         buf = new StringBuffer JavaDoc();
171         try {
172             ISODateTimeFormat.yearMonthDay().printTo(buf, (ReadablePartial) null);
173             fail();
174         } catch (IllegalArgumentException JavaDoc ex) {}
175     }
176
177     //-----------------------------------------------------------------------
178
public void testPrint_writerMethods() throws Exception JavaDoc {
179         DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
180         CharArrayWriter JavaDoc out = new CharArrayWriter JavaDoc();
181         f.printTo(out, dt);
182         assertEquals("Wed 2004-06-09T10:20:30Z", out.toString());
183         
184         out = new CharArrayWriter JavaDoc();
185         f.printTo(out, dt.getMillis());
186         assertEquals("Wed 2004-06-09T11:20:30+01:00", out.toString());
187         
188         out = new CharArrayWriter JavaDoc();
189         ISODateTimeFormat.yearMonthDay().printTo(out, dt.toYearMonthDay());
190         assertEquals("2004-06-09", out.toString());
191         
192         out = new CharArrayWriter JavaDoc();
193         try {
194             ISODateTimeFormat.yearMonthDay().printTo(out, (ReadablePartial) null);
195             fail();
196         } catch (IllegalArgumentException JavaDoc ex) {}
197     }
198
199     //-----------------------------------------------------------------------
200
public void testPrint_chrono_and_zone() {
201         DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
202         assertEquals("Wed 2004-06-09T10:20:30Z",
203                 f.withChronology(null).withZone(null).print(dt));
204         assertEquals("Wed 2004-06-09T12:20:30+02:00",
205                 f.withChronology(ISO_PARIS).withZone(null).print(dt));
206         assertEquals("Wed 2004-06-09T12:20:30+02:00",
207                 f.withChronology(ISO_PARIS).withZone(PARIS).print(dt));
208         assertEquals("Wed 2004-06-09T06:20:30-04:00",
209                 f.withChronology(ISO_PARIS).withZone(NEWYORK).print(dt));
210         assertEquals("Wed 2004-06-09T06:20:30-04:00",
211                 f.withChronology(null).withZone(NEWYORK).print(dt));
212         
213         dt = dt.withChronology(ISO_PARIS);
214         assertEquals("Wed 2004-06-09T12:20:30+02:00",
215                 f.withChronology(null).withZone(null).print(dt));
216         assertEquals("Wed 2004-06-09T12:20:30+02:00",
217                 f.withChronology(ISO_PARIS).withZone(null).print(dt));
218         assertEquals("Wed 2004-06-09T12:20:30+02:00",
219                 f.withChronology(ISO_PARIS).withZone(PARIS).print(dt));
220         assertEquals("Wed 2004-06-09T06:20:30-04:00",
221                 f.withChronology(ISO_PARIS).withZone(NEWYORK).print(dt));
222         assertEquals("Wed 2004-06-09T06:20:30-04:00",
223                 f.withChronology(null).withZone(NEWYORK).print(dt));
224         
225         dt = dt.withChronology(BUDDHIST_PARIS);
226         assertEquals("Wed 2547-06-09T12:20:30+02:00",
227                 f.withChronology(null).withZone(null).print(dt));
228         assertEquals("Wed 2004-06-09T12:20:30+02:00",
229                 f.withChronology(ISO_PARIS).withZone(null).print(dt));
230         assertEquals("Wed 2004-06-09T12:20:30+02:00",
231                 f.withChronology(ISO_PARIS).withZone(PARIS).print(dt));
232         assertEquals("Wed 2004-06-09T06:20:30-04:00",
233                 f.withChronology(ISO_PARIS).withZone(NEWYORK).print(dt));
234         assertEquals("Wed 2547-06-09T06:20:30-04:00",
235                 f.withChronology(null).withZone(NEWYORK).print(dt));
236     }
237
238     public void testWithGetLocale() {
239         DateTimeFormatter f2 = f.withLocale(Locale.FRENCH);
240         assertEquals(Locale.FRENCH, f2.getLocale());
241         assertSame(f2, f2.withLocale(Locale.FRENCH));
242         
243         f2 = f.withLocale(null);
244         assertEquals(null, f2.getLocale());
245         assertSame(f2, f2.withLocale(null));
246     }
247
248     public void testWithGetZone() {
249         DateTimeFormatter f2 = f.withZone(PARIS);
250         assertEquals(PARIS, f2.getZone());
251         assertSame(f2, f2.withZone(PARIS));
252         
253         f2 = f.withZone(null);
254         assertEquals(null, f2.getZone());
255         assertSame(f2, f2.withZone(null));
256     }
257
258     public void testWithGetChronology() {
259         DateTimeFormatter f2 = f.withChronology(BUDDHIST_PARIS);
260         assertEquals(BUDDHIST_PARIS, f2.getChronolgy());
261         assertSame(f2, f2.withChronology(BUDDHIST_PARIS));
262         
263         f2 = f.withChronology(null);
264         assertEquals(null, f2.getChronolgy());
265         assertSame(f2, f2.withChronology(null));
266     }
267
268     public void testWithGetPivotYear() {
269         DateTimeFormatter f2 = f.withPivotYear(13);
270         assertEquals(new Integer JavaDoc(13), f2.getPivotYear());
271         assertSame(f2, f2.withPivotYear(13));
272         
273         f2 = f.withPivotYear(new Integer JavaDoc(14));
274         assertEquals(new Integer JavaDoc(14), f2.getPivotYear());
275         assertSame(f2, f2.withPivotYear(new Integer JavaDoc(14)));
276         
277         f2 = f.withPivotYear(null);
278         assertEquals(null, f2.getPivotYear());
279         assertSame(f2, f2.withPivotYear(null));
280     }
281
282     public void testWithGetOffsetParsedMethods() {
283         DateTimeFormatter f2 = f;
284         assertEquals(false, f2.isOffsetParsed());
285         assertEquals(null, f2.getZone());
286         
287         f2 = f.withOffsetParsed();
288         assertEquals(true, f2.isOffsetParsed());
289         assertEquals(null, f2.getZone());
290         
291         f2 = f2.withZone(PARIS);
292         assertEquals(false, f2.isOffsetParsed());
293         assertEquals(PARIS, f2.getZone());
294         
295         f2 = f2.withOffsetParsed();
296         assertEquals(true, f2.isOffsetParsed());
297         assertEquals(null, f2.getZone());
298         
299         f2 = f.withOffsetParsed();
300         assertNotSame(f, f2);
301         DateTimeFormatter f3 = f2.withOffsetParsed();
302         assertSame(f2, f3);
303     }
304
305     public void testPrinterParserMethods() {
306         DateTimeFormatter f2 = new DateTimeFormatter(f.getPrinter(), f.getParser());
307         assertEquals(f.getPrinter(), f2.getPrinter());
308         assertEquals(f.getParser(), f2.getParser());
309         assertEquals(true, f2.isPrinter());
310         assertEquals(true, f2.isParser());
311         assertNotNull(f2.print(0L));
312         assertNotNull(f2.parseDateTime("Thu 1970-01-01T00:00:00Z"));
313         
314         f2 = new DateTimeFormatter(f.getPrinter(), null);
315         assertEquals(f.getPrinter(), f2.getPrinter());
316         assertEquals(null, f2.getParser());
317         assertEquals(true, f2.isPrinter());
318         assertEquals(false, f2.isParser());
319         assertNotNull(f2.print(0L));
320         try {
321             f2.parseDateTime("Thu 1970-01-01T00:00:00Z");
322             fail();
323         } catch (UnsupportedOperationException JavaDoc ex) {}
324         
325         f2 = new DateTimeFormatter(null, f.getParser());
326         assertEquals(null, f2.getPrinter());
327         assertEquals(f.getParser(), f2.getParser());
328         assertEquals(false, f2.isPrinter());
329         assertEquals(true, f2.isParser());
330         try {
331             f2.print(0L);
332             fail();
333         } catch (UnsupportedOperationException JavaDoc ex) {}
334         assertNotNull(f2.parseDateTime("Thu 1970-01-01T00:00:00Z"));
335     }
336
337     //-----------------------------------------------------------------------
338
public void testParseDateTime_simple() {
339         DateTime expect = null;
340         expect = new DateTime(2004, 6, 9, 11, 20, 30, 0, LONDON);
341         assertEquals(expect, g.parseDateTime("2004-06-09T10:20:30Z"));
342         
343         try {
344             g.parseDateTime("ABC");
345             fail();
346         } catch (IllegalArgumentException JavaDoc ex) {}
347     }
348
349     public void testParseDateTime_zone() {
350         DateTime expect = null;
351         expect = new DateTime(2004, 6, 9, 11, 20, 30, 0, LONDON);
352         assertEquals(expect, g.withZone(LONDON).parseDateTime("2004-06-09T10:20:30Z"));
353         
354         expect = new DateTime(2004, 6, 9, 11, 20, 30, 0, LONDON);
355         assertEquals(expect, g.withZone(null).parseDateTime("2004-06-09T10:20:30Z"));
356         
357         expect = new DateTime(2004, 6, 9, 12, 20, 30, 0, PARIS);
358         assertEquals(expect, g.withZone(PARIS).parseDateTime("2004-06-09T10:20:30Z"));
359     }
360
361     public void testParseDateTime_zone2() {
362         DateTime expect = null;
363         expect = new DateTime(2004, 6, 9, 11, 20, 30, 0, LONDON);
364         assertEquals(expect, g.withZone(LONDON).parseDateTime("2004-06-09T06:20:30-04:00"));
365         
366         expect = new DateTime(2004, 6, 9, 11, 20, 30, 0, LONDON);
367         assertEquals(expect, g.withZone(null).parseDateTime("2004-06-09T06:20:30-04:00"));
368         
369         expect = new DateTime(2004, 6, 9, 12, 20, 30, 0, PARIS);
370         assertEquals(expect, g.withZone(PARIS).parseDateTime("2004-06-09T06:20:30-04:00"));
371     }
372
373     public void testParseDateTime_zone3() {
374         DateTimeFormatter h = new DateTimeFormatterBuilder()
375         .append(ISODateTimeFormat.date())
376         .appendLiteral('T')
377         .append(ISODateTimeFormat.timeElementParser())
378         .toFormatter();
379         
380         DateTime expect = null;
381         expect = new DateTime(2004, 6, 9, 10, 20, 30, 0, LONDON);
382         assertEquals(expect, h.withZone(LONDON).parseDateTime("2004-06-09T10:20:30"));
383         
384         expect = new DateTime(2004, 6, 9, 10, 20, 30, 0, LONDON);
385         assertEquals(expect, h.withZone(null).parseDateTime("2004-06-09T10:20:30"));
386         
387         expect = new DateTime(2004, 6, 9, 10, 20, 30, 0, PARIS);
388         assertEquals(expect, h.withZone(PARIS).parseDateTime("2004-06-09T10:20:30"));
389     }
390
391     public void testParseDateTime_simple_precedence() {
392         DateTime expect = null;
393         // use correct day of week
394
expect = new DateTime(2004, 6, 9, 11, 20, 30, 0, LONDON);
395         assertEquals(expect, f.parseDateTime("Wed 2004-06-09T10:20:30Z"));
396         
397         // use wrong day of week
398
expect = new DateTime(2004, 6, 7, 11, 20, 30, 0, LONDON);
399         // DayOfWeek takes precedence, because week < month in length
400
assertEquals(expect, f.parseDateTime("Mon 2004-06-09T10:20:30Z"));
401     }
402
403     public void testParseDateTime_offsetParsed() {
404         DateTime expect = null;
405         expect = new DateTime(2004, 6, 9, 10, 20, 30, 0, UTC);
406         assertEquals(expect, g.withOffsetParsed().parseDateTime("2004-06-09T10:20:30Z"));
407         
408         expect = new DateTime(2004, 6, 9, 6, 20, 30, 0, DateTimeZone.forOffsetHours(-4));
409         assertEquals(expect, g.withOffsetParsed().parseDateTime("2004-06-09T06:20:30-04:00"));
410         
411         expect = new DateTime(2004, 6, 9, 10, 20, 30, 0, UTC);
412         assertEquals(expect, g.withZone(PARIS).withOffsetParsed().parseDateTime("2004-06-09T10:20:30Z"));
413         expect = new DateTime(2004, 6, 9, 12, 20, 30, 0, PARIS);
414         assertEquals(expect, g.withOffsetParsed().withZone(PARIS).parseDateTime("2004-06-09T10:20:30Z"));
415     }
416
417     public void testParseDateTime_chrono() {
418         DateTime expect = null;
419         expect = new DateTime(2004, 6, 9, 12, 20, 30, 0, PARIS);
420         assertEquals(expect, g.withChronology(ISO_PARIS).parseDateTime("2004-06-09T10:20:30Z"));
421         
422         expect = new DateTime(2004, 6, 9, 11, 20, 30, 0,LONDON);
423         assertEquals(expect, g.withChronology(null).parseDateTime("2004-06-09T10:20:30Z"));
424         
425         expect = new DateTime(2547, 6, 9, 12, 20, 30, 0, BUDDHIST_PARIS);
426         assertEquals(expect, g.withChronology(BUDDHIST_PARIS).parseDateTime("2547-06-09T10:20:30Z"));
427         
428         expect = new DateTime(2004, 6, 9, 10, 29, 51, 0, BUDDHIST_PARIS); // zone is +00:09:21 in 1451
429
assertEquals(expect, g.withChronology(BUDDHIST_PARIS).parseDateTime("2004-06-09T10:20:30Z"));
430     }
431
432     //-----------------------------------------------------------------------
433
public void testParseMutableDateTime_simple() {
434         MutableDateTime expect = null;
435         expect = new MutableDateTime(2004, 6, 9, 11, 20, 30, 0, LONDON);
436         assertEquals(expect, g.parseMutableDateTime("2004-06-09T10:20:30Z"));
437         
438         try {
439             g.parseMutableDateTime("ABC");
440             fail();
441         } catch (IllegalArgumentException JavaDoc ex) {}
442     }
443
444     public void testParseMutableDateTime_zone() {
445         MutableDateTime expect = null;
446         expect = new MutableDateTime(2004, 6, 9, 11, 20, 30, 0, LONDON);
447         assertEquals(expect, g.withZone(LONDON).parseMutableDateTime("2004-06-09T10:20:30Z"));
448         
449         expect = new MutableDateTime(2004, 6, 9, 11, 20, 30, 0, LONDON);
450         assertEquals(expect, g.withZone(null).parseMutableDateTime("2004-06-09T10:20:30Z"));
451         
452         expect = new MutableDateTime(2004, 6, 9, 12, 20, 30, 0, PARIS);
453         assertEquals(expect, g.withZone(PARIS).parseMutableDateTime("2004-06-09T10:20:30Z"));
454     }
455
456     public void testParseMutableDateTime_zone2() {
457         MutableDateTime expect = null;
458         expect = new MutableDateTime(2004, 6, 9, 11, 20, 30, 0, LONDON);
459         assertEquals(expect, g.withZone(LONDON).parseMutableDateTime("2004-06-09T06:20:30-04:00"));
460         
461         expect = new MutableDateTime(2004, 6, 9, 11, 20, 30, 0, LONDON);
462         assertEquals(expect, g.withZone(null).parseMutableDateTime("2004-06-09T06:20:30-04:00"));
463         
464         expect = new MutableDateTime(2004, 6, 9, 12, 20, 30, 0, PARIS);
465         assertEquals(expect, g.withZone(PARIS).parseMutableDateTime("2004-06-09T06:20:30-04:00"));
466     }
467
468     public void testParseMutableDateTime_zone3() {
469         DateTimeFormatter h = new DateTimeFormatterBuilder()
470         .append(ISODateTimeFormat.date())
471         .appendLiteral('T')
472         .append(ISODateTimeFormat.timeElementParser())
473         .toFormatter();
474         
475         MutableDateTime expect = null;
476         expect = new MutableDateTime(2004, 6, 9, 10, 20, 30, 0, LONDON);
477         assertEquals(expect, h.withZone(LONDON).parseMutableDateTime("2004-06-09T10:20:30"));
478         
479         expect = new MutableDateTime(2004, 6, 9, 10, 20, 30, 0, LONDON);
480         assertEquals(expect, h.withZone(null).parseMutableDateTime("2004-06-09T10:20:30"));
481         
482         expect = new MutableDateTime(2004, 6, 9, 10, 20, 30, 0, PARIS);
483         assertEquals(expect, h.withZone(PARIS).parseMutableDateTime("2004-06-09T10:20:30"));
484     }
485
486     public void testParseMutableDateTime_simple_precedence() {
487         MutableDateTime expect = null;
488         // use correct day of week
489
expect = new MutableDateTime(2004, 6, 9, 11, 20, 30, 0, LONDON);
490         assertEquals(expect, f.parseDateTime("Wed 2004-06-09T10:20:30Z"));
491         
492         // use wrong day of week
493
expect = new MutableDateTime(2004, 6, 7, 11, 20, 30, 0, LONDON);
494         // DayOfWeek takes precedence, because week < month in length
495
assertEquals(expect, f.parseDateTime("Mon 2004-06-09T10:20:30Z"));
496     }
497
498     public void testParseMutableDateTime_offsetParsed() {
499         MutableDateTime expect = null;
500         expect = new MutableDateTime(2004, 6, 9, 10, 20, 30, 0, UTC);
501         assertEquals(expect, g.withOffsetParsed().parseMutableDateTime("2004-06-09T10:20:30Z"));
502         
503         expect = new MutableDateTime(2004, 6, 9, 6, 20, 30, 0, DateTimeZone.forOffsetHours(-4));
504         assertEquals(expect, g.withOffsetParsed().parseMutableDateTime("2004-06-09T06:20:30-04:00"));
505         
506         expect = new MutableDateTime(2004, 6, 9, 10, 20, 30, 0, UTC);
507         assertEquals(expect, g.withZone(PARIS).withOffsetParsed().parseMutableDateTime("2004-06-09T10:20:30Z"));
508         expect = new MutableDateTime(2004, 6, 9, 12, 20, 30, 0, PARIS);
509         assertEquals(expect, g.withOffsetParsed().withZone(PARIS).parseMutableDateTime("2004-06-09T10:20:30Z"));
510     }
511
512     public void testParseMutableDateTime_chrono() {
513         MutableDateTime expect = null;
514         expect = new MutableDateTime(2004, 6, 9, 12, 20, 30, 0, PARIS);
515         assertEquals(expect, g.withChronology(ISO_PARIS).parseMutableDateTime("2004-06-09T10:20:30Z"));
516         
517         expect = new MutableDateTime(2004, 6, 9, 11, 20, 30, 0,LONDON);
518         assertEquals(expect, g.withChronology(null).parseMutableDateTime("2004-06-09T10:20:30Z"));
519         
520         expect = new MutableDateTime(2547, 6, 9, 12, 20, 30, 0, BUDDHIST_PARIS);
521         assertEquals(expect, g.withChronology(BUDDHIST_PARIS).parseMutableDateTime("2547-06-09T10:20:30Z"));
522         
523         expect = new MutableDateTime(2004, 6, 9, 10, 29, 51, 0, BUDDHIST_PARIS); // zone is +00:09:21 in 1451
524
assertEquals(expect, g.withChronology(BUDDHIST_PARIS).parseMutableDateTime("2004-06-09T10:20:30Z"));
525     }
526
527     //-----------------------------------------------------------------------
528
public void testParseInto_simple() {
529         MutableDateTime expect = null;
530         expect = new MutableDateTime(2004, 6, 9, 11, 20, 30, 0, LONDON);
531         MutableDateTime result = new MutableDateTime(0L);
532         assertEquals(20, g.parseInto(result, "2004-06-09T10:20:30Z", 0));
533         assertEquals(expect, result);
534         
535         try {
536             g.parseInto(null, "2004-06-09T10:20:30Z", 0);
537             fail();
538         } catch (IllegalArgumentException JavaDoc ex) {}
539         
540         assertEquals(~0, g.parseInto(result, "ABC", 0));
541         assertEquals(~10, g.parseInto(result, "2004-06-09", 0));
542         assertEquals(~13, g.parseInto(result, "XX2004-06-09T", 2));
543     }
544
545     public void testParseInto_zone() {
546         MutableDateTime expect = null;
547         MutableDateTime result = null;
548         expect = new MutableDateTime(2004, 6, 9, 11, 20, 30, 0, LONDON);
549         result = new MutableDateTime(0L);
550         assertEquals(20, g.withZone(LONDON).parseInto(result, "2004-06-09T10:20:30Z", 0));
551         assertEquals(expect, result);
552         
553         expect = new MutableDateTime(2004, 6, 9, 11, 20, 30, 0, LONDON);
554         result = new MutableDateTime(0L);
555         assertEquals(20, g.withZone(null).parseInto(result, "2004-06-09T10:20:30Z", 0));
556         assertEquals(expect, result);
557         
558         expect = new MutableDateTime(2004, 6, 9, 12, 20, 30, 0, PARIS);
559         result = new MutableDateTime(0L);
560         assertEquals(20, g.withZone(PARIS).parseInto(result, "2004-06-09T10:20:30Z", 0));
561         assertEquals(expect, result);
562     }
563
564     public void testParseInto_zone2() {
565         MutableDateTime expect = null;
566         MutableDateTime result = null;
567         expect = new MutableDateTime(2004, 6, 9, 11, 20, 30, 0, LONDON);
568         result = new MutableDateTime(0L);
569         assertEquals(25, g.withZone(LONDON).parseInto(result, "2004-06-09T06:20:30-04:00", 0));
570         assertEquals(expect, result);
571         
572         expect = new MutableDateTime(2004, 6, 9, 11, 20, 30, 0, LONDON);
573         assertEquals(25, g.withZone(null).parseInto(result, "2004-06-09T06:20:30-04:00", 0));
574         assertEquals(expect, result);
575         
576         expect = new MutableDateTime(2004, 6, 9, 12, 20, 30, 0, PARIS);
577         assertEquals(25, g.withZone(PARIS).parseInto(result, "2004-06-09T06:20:30-04:00", 0));
578         assertEquals(expect, result);
579     }
580
581     public void testParseInto_zone3() {
582         DateTimeFormatter h = new DateTimeFormatterBuilder()
583         .append(ISODateTimeFormat.date())
584         .appendLiteral('T')
585         .append(ISODateTimeFormat.timeElementParser())
586         .toFormatter();
587         
588         MutableDateTime expect = null;
589         MutableDateTime result = null;
590         expect = new MutableDateTime(2004, 6, 9, 10, 20, 30, 0, LONDON);
591         result = new MutableDateTime(0L);
592         assertEquals(19, h.withZone(LONDON).parseInto(result, "2004-06-09T10:20:30", 0));
593         assertEquals(expect, result);
594         
595         expect = new MutableDateTime(2004, 6, 9, 10, 20, 30, 0, LONDON);
596         result = new MutableDateTime(0L);
597         assertEquals(19, h.withZone(null).parseInto(result, "2004-06-09T10:20:30", 0));
598         assertEquals(expect, result);
599         
600         expect = new MutableDateTime(2004, 6, 9, 10, 20, 30, 0, PARIS);
601         result = new MutableDateTime(0L);
602         assertEquals(19, h.withZone(PARIS).parseInto(result, "2004-06-09T10:20:30", 0));
603         assertEquals(expect, result);
604     }
605
606     public void testParseInto_simple_precedence() {
607         MutableDateTime expect = null;
608         MutableDateTime result = null;
609         expect = new MutableDateTime(2004, 6, 7, 11, 20, 30, 0, LONDON);
610         result = new MutableDateTime(0L);
611         // DayOfWeek takes precedence, because week < month in length
612
assertEquals(24, f.parseInto(result, "Mon 2004-06-09T10:20:30Z", 0));
613         assertEquals(expect, result);
614     }
615
616     public void testParseInto_offsetParsed() {
617         MutableDateTime expect = null;
618         MutableDateTime result = null;
619         expect = new MutableDateTime(2004, 6, 9, 10, 20, 30, 0, UTC);
620         result = new MutableDateTime(0L);
621         assertEquals(20, g.withOffsetParsed().parseInto(result, "2004-06-09T10:20:30Z", 0));
622         assertEquals(expect, result);
623         
624         expect = new MutableDateTime(2004, 6, 9, 6, 20, 30, 0, DateTimeZone.forOffsetHours(-4));
625         result = new MutableDateTime(0L);
626         assertEquals(25, g.withOffsetParsed().parseInto(result, "2004-06-09T06:20:30-04:00", 0));
627         assertEquals(expect, result);
628         
629         expect = new MutableDateTime(2004, 6, 9, 10, 20, 30, 0, UTC);
630         result = new MutableDateTime(0L);
631         assertEquals(20, g.withZone(PARIS).withOffsetParsed().parseInto(result, "2004-06-09T10:20:30Z", 0));
632         assertEquals(expect, result);
633         expect = new MutableDateTime(2004, 6, 9, 12, 20, 30, 0, PARIS);
634         result = new MutableDateTime(0L);
635         assertEquals(20, g.withOffsetParsed().withZone(PARIS).parseInto(result, "2004-06-09T10:20:30Z", 0));
636         assertEquals(expect, result);
637     }
638
639     public void testParseInto_chrono() {
640         MutableDateTime expect = null;
641         MutableDateTime result = null;
642         expect = new MutableDateTime(2004, 6, 9, 12, 20, 30, 0, PARIS);
643         result = new MutableDateTime(0L);
644         assertEquals(20, g.withChronology(ISO_PARIS).parseInto(result, "2004-06-09T10:20:30Z", 0));
645         assertEquals(expect, result);
646         
647         expect = new MutableDateTime(2004, 6, 9, 11, 20, 30, 0, LONDON);
648         result = new MutableDateTime(0L);
649         assertEquals(20, g.withChronology(null).parseInto(result, "2004-06-09T10:20:30Z", 0));
650         assertEquals(expect, result);
651         
652         expect = new MutableDateTime(2547, 6, 9, 12, 20, 30, 0, BUDDHIST_PARIS);
653         result = new MutableDateTime(0L);
654         assertEquals(20, g.withChronology(BUDDHIST_PARIS).parseInto(result, "2547-06-09T10:20:30Z", 0));
655         assertEquals(expect, result);
656         
657         expect = new MutableDateTime(2004, 6, 9, 10, 29, 51, 0, BUDDHIST_PARIS);
658         result = new MutableDateTime(0L);
659         assertEquals(20, g.withChronology(BUDDHIST_PARIS).parseInto(result, "2004-06-09T10:20:30Z", 0));
660         assertEquals(expect, result);
661     }
662
663     public void testParseMillis_fractionOfSecondLong() {
664         DateTimeFormatter f = new DateTimeFormatterBuilder()
665             .appendSecondOfDay(2).appendLiteral('.').appendFractionOfSecond(1, 9)
666                 .toFormatter().withZone(DateTimeZone.UTC);
667         assertEquals(10512, f.parseMillis("10.5123456"));
668         assertEquals(10512, f.parseMillis("10.512999"));
669     }
670
671     //-----------------------------------------------------------------------
672
// Ensure time zone name switches properly at the zone DST transition.
673
public void testZoneNameNearTransition() {
674         DateTime inDST_1 = new DateTime(2005, 10, 30, 1, 0, 0, 0, NEWYORK);
675         DateTime inDST_2 = new DateTime(2005, 10, 30, 1, 59, 59, 999, NEWYORK);
676         DateTime onDST = new DateTime(2005, 10, 30, 2, 0, 0, 0, NEWYORK);
677         DateTime outDST = new DateTime(2005, 10, 30, 2, 0, 0, 1, NEWYORK);
678         DateTime outDST_2 = new DateTime(2005, 10, 30, 2, 0, 1, 0, NEWYORK);
679
680         DateTimeFormatter fmt = DateTimeFormat.forPattern("yyy-MM-dd HH:mm:ss.S zzzz");
681         assertEquals("2005-10-30 01:00:00.0 Eastern Daylight Time", fmt.print(inDST_1));
682         assertEquals("2005-10-30 01:59:59.9 Eastern Daylight Time", fmt.print(inDST_2));
683         assertEquals("2005-10-30 02:00:00.0 Eastern Standard Time", fmt.print(onDST));
684         assertEquals("2005-10-30 02:00:00.0 Eastern Standard Time", fmt.print(outDST));
685         assertEquals("2005-10-30 02:00:01.0 Eastern Standard Time", fmt.print(outDST_2));
686     }
687
688     // Ensure time zone name switches properly at the zone DST transition.
689
public void testZoneShortNameNearTransition() {
690         DateTime inDST_1 = new DateTime(2005, 10, 30, 1, 0, 0, 0, NEWYORK);
691         DateTime inDST_2 = new DateTime(2005, 10, 30, 1, 59, 59, 999, NEWYORK);
692         DateTime onDST = new DateTime(2005, 10, 30, 2, 0, 0, 0, NEWYORK);
693         DateTime outDST = new DateTime(2005, 10, 30, 2, 0, 0, 1, NEWYORK);
694         DateTime outDST_2 = new DateTime(2005, 10, 30, 2, 0, 1, 0, NEWYORK);
695
696         DateTimeFormatter fmt = DateTimeFormat.forPattern("yyy-MM-dd HH:mm:ss.S z");
697         assertEquals("2005-10-30 01:00:00.0 EDT", fmt.print(inDST_1));
698         assertEquals("2005-10-30 01:59:59.9 EDT", fmt.print(inDST_2));
699         assertEquals("2005-10-30 02:00:00.0 EST", fmt.print(onDST));
700         assertEquals("2005-10-30 02:00:00.0 EST", fmt.print(outDST));
701         assertEquals("2005-10-30 02:00:01.0 EST", fmt.print(outDST_2));
702     }
703
704 }
705
Popular Tags