0001: /*
0002: * Copyright 2001-2006 Stephen Colebourne
0003: *
0004: * Licensed under the Apache License, Version 2.0 (the "License");
0005: * you may not use this file except in compliance with the License.
0006: * You may obtain a copy of the License at
0007: *
0008: * http://www.apache.org/licenses/LICENSE-2.0
0009: *
0010: * Unless required by applicable law or agreed to in writing, software
0011: * distributed under the License is distributed on an "AS IS" BASIS,
0012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013: * See the License for the specific language governing permissions and
0014: * limitations under the License.
0015: */
0016: package org.joda.time.format;
0017:
0018: import java.util.Locale;
0019: import java.util.TimeZone;
0020:
0021: import junit.framework.TestCase;
0022: import junit.framework.TestSuite;
0023:
0024: import org.joda.time.Chronology;
0025: import org.joda.time.DateTime;
0026: import org.joda.time.DateTimeConstants;
0027: import org.joda.time.DateTimeFieldType;
0028: import org.joda.time.DateTimeUtils;
0029: import org.joda.time.DateTimeZone;
0030: import org.joda.time.chrono.GJChronology;
0031:
0032: /**
0033: * This class is a Junit unit test for DateTime Formating.
0034: *
0035: * @author Stephen Colebourne
0036: * @author Fredrik Borgh
0037: */
0038: public class TestDateTimeFormat extends TestCase {
0039:
0040: private static final DateTimeZone UTC = DateTimeZone.UTC;
0041: private static final DateTimeZone PARIS = DateTimeZone
0042: .forID("Europe/Paris");
0043: private static final DateTimeZone LONDON = DateTimeZone
0044: .forID("Europe/London");
0045: private static final DateTimeZone TOKYO = DateTimeZone
0046: .forID("Asia/Tokyo");
0047: private static final DateTimeZone NEWYORK = DateTimeZone
0048: .forID("America/New_York");
0049:
0050: long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
0051: + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365
0052: + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365
0053: + 365 + 365 + 366 + 365;
0054: // 2002-06-09
0055: private long TEST_TIME_NOW = (y2002days + 31L + 28L + 31L + 30L
0056: + 31L + 9L - 1L)
0057: * DateTimeConstants.MILLIS_PER_DAY;
0058:
0059: private DateTimeZone originalDateTimeZone = null;
0060: private TimeZone originalTimeZone = null;
0061: private Locale originalLocale = null;
0062:
0063: public static void main(String[] args) {
0064: junit.textui.TestRunner.run(suite());
0065: }
0066:
0067: public static TestSuite suite() {
0068: return new TestSuite(TestDateTimeFormat.class);
0069: }
0070:
0071: public TestDateTimeFormat(String name) {
0072: super (name);
0073: }
0074:
0075: protected void setUp() throws Exception {
0076: DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW);
0077: originalDateTimeZone = DateTimeZone.getDefault();
0078: originalTimeZone = TimeZone.getDefault();
0079: originalLocale = Locale.getDefault();
0080: DateTimeZone.setDefault(LONDON);
0081: TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
0082: Locale.setDefault(Locale.UK);
0083: }
0084:
0085: protected void tearDown() throws Exception {
0086: DateTimeUtils.setCurrentMillisSystem();
0087: DateTimeZone.setDefault(originalDateTimeZone);
0088: TimeZone.setDefault(originalTimeZone);
0089: Locale.setDefault(originalLocale);
0090: originalDateTimeZone = null;
0091: originalTimeZone = null;
0092: originalLocale = null;
0093: }
0094:
0095: //-----------------------------------------------------------------------
0096: public void testSubclassableConstructor() {
0097: DateTimeFormat f = new DateTimeFormat() {
0098: // test constructor is protected
0099: };
0100: assertNotNull(f);
0101: }
0102:
0103: //-----------------------------------------------------------------------
0104: public void testFormat_era() {
0105: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
0106: DateTimeFormatter f = DateTimeFormat.forPattern("G")
0107: .withLocale(Locale.UK);
0108: assertEquals(dt.toString(), "AD", f.print(dt));
0109:
0110: dt = dt.withZone(NEWYORK);
0111: assertEquals(dt.toString(), "AD", f.print(dt));
0112:
0113: dt = dt.withZone(PARIS);
0114: assertEquals(dt.toString(), "AD", f.print(dt));
0115: }
0116:
0117: //-----------------------------------------------------------------------
0118: public void testFormat_centuryOfEra() {
0119: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
0120: DateTimeFormatter f = DateTimeFormat.forPattern("C")
0121: .withLocale(Locale.UK);
0122: assertEquals(dt.toString(), "20", f.print(dt));
0123:
0124: dt = dt.withZone(NEWYORK);
0125: assertEquals(dt.toString(), "20", f.print(dt));
0126:
0127: dt = dt.withZone(TOKYO);
0128: assertEquals(dt.toString(), "20", f.print(dt));
0129:
0130: dt = new DateTime(-123, 6, 9, 10, 20, 30, 40, UTC);
0131: assertEquals(dt.toString(), "1", f.print(dt));
0132: }
0133:
0134: //-----------------------------------------------------------------------
0135: public void testFormat_yearOfEra() {
0136: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
0137: DateTimeFormatter f = DateTimeFormat.forPattern("Y")
0138: .withLocale(Locale.UK);
0139: assertEquals(dt.toString(), "2004", f.print(dt));
0140:
0141: dt = dt.withZone(NEWYORK);
0142: assertEquals(dt.toString(), "2004", f.print(dt));
0143:
0144: dt = dt.withZone(TOKYO);
0145: assertEquals(dt.toString(), "2004", f.print(dt));
0146:
0147: dt = new DateTime(-123, 6, 9, 10, 20, 30, 40, UTC);
0148: assertEquals(dt.toString(), "124", f.print(dt)); // 124th year of BCE
0149: }
0150:
0151: public void testFormat_yearOfEra_twoDigit() {
0152: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
0153: DateTimeFormatter f = DateTimeFormat.forPattern("YY")
0154: .withLocale(Locale.UK);
0155: assertEquals(dt.toString(), "04", f.print(dt));
0156:
0157: dt = new DateTime(-123, 6, 9, 10, 20, 30, 40, UTC);
0158: assertEquals(dt.toString(), "23", f.print(dt));
0159:
0160: // current time set to 2002-06-09
0161: f = f.withZone(UTC);
0162: DateTime expect = null;
0163: expect = new DateTime(2004, 1, 1, 0, 0, 0, 0, UTC);
0164: assertEquals(expect, f.parseDateTime("04"));
0165:
0166: expect = new DateTime(1922, 1, 1, 0, 0, 0, 0, UTC);
0167: assertEquals(expect, f.parseDateTime("22"));
0168:
0169: expect = new DateTime(2021, 1, 1, 0, 0, 0, 0, UTC);
0170: assertEquals(expect, f.parseDateTime("21"));
0171:
0172: // Added tests to ensure single sign digit parse fails properly
0173: try {
0174: f.parseDateTime("-");
0175: fail();
0176: } catch (IllegalArgumentException ex) {
0177: }
0178:
0179: try {
0180: f.parseDateTime("+");
0181: fail();
0182: } catch (IllegalArgumentException ex) {
0183: }
0184:
0185: // Added tests for pivot year setting
0186: f = f.withPivotYear(new Integer(2050));
0187: expect = new DateTime(2000, 1, 1, 0, 0, 0, 0, UTC);
0188: assertEquals(expect, f.parseDateTime("00"));
0189:
0190: expect = new DateTime(2099, 1, 1, 0, 0, 0, 0, UTC);
0191: assertEquals(expect, f.parseDateTime("99"));
0192:
0193: // Added tests to ensure two digit parsing is lenient for DateTimeFormat
0194: f = DateTimeFormat.forPattern("YY").withLocale(Locale.UK);
0195: f = f.withZone(UTC);
0196: f.parseDateTime("5");
0197: f.parseDateTime("005");
0198: f.parseDateTime("+50");
0199: f.parseDateTime("-50");
0200: }
0201:
0202: public void testFormat_yearOfEraParse() {
0203: Chronology chrono = GJChronology.getInstanceUTC();
0204:
0205: DateTimeFormatter f = DateTimeFormat.forPattern("YYYY-MM GG")
0206: .withChronology(chrono).withLocale(Locale.UK);
0207:
0208: DateTime dt = new DateTime(2005, 10, 1, 0, 0, 0, 0, chrono);
0209: assertEquals(dt, f.parseDateTime("2005-10 AD"));
0210: assertEquals(dt, f.parseDateTime("2005-10 CE"));
0211:
0212: dt = new DateTime(-2005, 10, 1, 0, 0, 0, 0, chrono);
0213: assertEquals(dt, f.parseDateTime("2005-10 BC"));
0214: assertEquals(dt, f.parseDateTime("2005-10 BCE"));
0215: }
0216:
0217: //-----------------------------------------------------------------------
0218: public void testFormat_year() {
0219: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
0220: DateTimeFormatter f = DateTimeFormat.forPattern("y")
0221: .withLocale(Locale.UK);
0222: assertEquals(dt.toString(), "2004", f.print(dt));
0223:
0224: dt = dt.withZone(NEWYORK);
0225: assertEquals(dt.toString(), "2004", f.print(dt));
0226:
0227: dt = dt.withZone(TOKYO);
0228: assertEquals(dt.toString(), "2004", f.print(dt));
0229:
0230: dt = new DateTime(-123, 6, 9, 10, 20, 30, 40, UTC);
0231: assertEquals(dt.toString(), "-123", f.print(dt));
0232:
0233: // Added tests to ensure single sign digit parse fails properly
0234: try {
0235: f.parseDateTime("-");
0236: fail();
0237: } catch (IllegalArgumentException ex) {
0238: }
0239:
0240: try {
0241: f.parseDateTime("+");
0242: fail();
0243: } catch (IllegalArgumentException ex) {
0244: }
0245: }
0246:
0247: public void testFormat_year_twoDigit() {
0248: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
0249: DateTimeFormatter f = DateTimeFormat.forPattern("yy")
0250: .withLocale(Locale.UK);
0251: assertEquals(dt.toString(), "04", f.print(dt));
0252:
0253: dt = new DateTime(-123, 6, 9, 10, 20, 30, 40, UTC);
0254: assertEquals(dt.toString(), "23", f.print(dt));
0255:
0256: // current time set to 2002-06-09
0257: f = f.withZone(UTC);
0258: DateTime expect = null;
0259: expect = new DateTime(2004, 1, 1, 0, 0, 0, 0, UTC);
0260: assertEquals(expect, f.parseDateTime("04"));
0261:
0262: expect = new DateTime(1922, 1, 1, 0, 0, 0, 0, UTC);
0263: assertEquals(expect, f.parseDateTime("22"));
0264:
0265: expect = new DateTime(2021, 1, 1, 0, 0, 0, 0, UTC);
0266: assertEquals(expect, f.parseDateTime("21"));
0267:
0268: // Added tests to ensure single sign digit parse fails properly
0269: try {
0270: f.parseDateTime("-");
0271: fail();
0272: } catch (IllegalArgumentException ex) {
0273: }
0274:
0275: try {
0276: f.parseDateTime("+");
0277: fail();
0278: } catch (IllegalArgumentException ex) {
0279: }
0280:
0281: // Added tests for pivot year setting
0282: f = f.withPivotYear(new Integer(2050));
0283: expect = new DateTime(2000, 1, 1, 0, 0, 0, 0, UTC);
0284: assertEquals(expect, f.parseDateTime("00"));
0285:
0286: expect = new DateTime(2099, 1, 1, 0, 0, 0, 0, UTC);
0287: assertEquals(expect, f.parseDateTime("99"));
0288:
0289: // Added tests to ensure two digit parsing is strict by default for
0290: // DateTimeFormatterBuilder
0291: f = new DateTimeFormatterBuilder().appendTwoDigitYear(2000)
0292: .toFormatter();
0293: f = f.withZone(UTC);
0294: try {
0295: f.parseDateTime("5");
0296: fail();
0297: } catch (IllegalArgumentException ex) {
0298: }
0299: try {
0300: f.parseDateTime("005");
0301: fail();
0302: } catch (IllegalArgumentException ex) {
0303: }
0304: try {
0305: f.parseDateTime("+50");
0306: fail();
0307: } catch (IllegalArgumentException ex) {
0308: }
0309: try {
0310: f.parseDateTime("-50");
0311: fail();
0312: } catch (IllegalArgumentException ex) {
0313: }
0314:
0315: // Added tests to ensure two digit parsing is lenient for DateTimeFormat
0316: f = DateTimeFormat.forPattern("yy").withLocale(Locale.UK);
0317: f = f.withZone(UTC);
0318: f.parseDateTime("5");
0319: f.parseDateTime("005");
0320: f.parseDateTime("+50");
0321: f.parseDateTime("-50");
0322:
0323: // Added tests for lenient two digit parsing
0324: f = new DateTimeFormatterBuilder().appendTwoDigitYear(2000,
0325: true).toFormatter();
0326: f = f.withZone(UTC);
0327: expect = new DateTime(2004, 1, 1, 0, 0, 0, 0, UTC);
0328: assertEquals(expect, f.parseDateTime("04"));
0329:
0330: expect = new DateTime(4, 1, 1, 0, 0, 0, 0, UTC);
0331: assertEquals(expect, f.parseDateTime("+04"));
0332:
0333: expect = new DateTime(-4, 1, 1, 0, 0, 0, 0, UTC);
0334: assertEquals(expect, f.parseDateTime("-04"));
0335:
0336: expect = new DateTime(4, 1, 1, 0, 0, 0, 0, UTC);
0337: assertEquals(expect, f.parseDateTime("4"));
0338:
0339: expect = new DateTime(-4, 1, 1, 0, 0, 0, 0, UTC);
0340: assertEquals(expect, f.parseDateTime("-4"));
0341:
0342: expect = new DateTime(4, 1, 1, 0, 0, 0, 0, UTC);
0343: assertEquals(expect, f.parseDateTime("004"));
0344:
0345: expect = new DateTime(4, 1, 1, 0, 0, 0, 0, UTC);
0346: assertEquals(expect, f.parseDateTime("+004"));
0347:
0348: expect = new DateTime(-4, 1, 1, 0, 0, 0, 0, UTC);
0349: assertEquals(expect, f.parseDateTime("-004"));
0350:
0351: expect = new DateTime(3004, 1, 1, 0, 0, 0, 0, UTC);
0352: assertEquals(expect, f.parseDateTime("3004"));
0353:
0354: expect = new DateTime(3004, 1, 1, 0, 0, 0, 0, UTC);
0355: assertEquals(expect, f.parseDateTime("+3004"));
0356:
0357: expect = new DateTime(-3004, 1, 1, 0, 0, 0, 0, UTC);
0358: assertEquals(expect, f.parseDateTime("-3004"));
0359:
0360: try {
0361: f.parseDateTime("-");
0362: fail();
0363: } catch (IllegalArgumentException ex) {
0364: }
0365:
0366: try {
0367: f.parseDateTime("+");
0368: fail();
0369: } catch (IllegalArgumentException ex) {
0370: }
0371: }
0372:
0373: public void testFormat_year_long() {
0374: DateTime dt = new DateTime(278004, 6, 9, 10, 20, 30, 40, UTC);
0375: DateTimeFormatter f = DateTimeFormat.forPattern("yyyy");
0376: assertEquals(dt.toString(), "278004", f.print(dt));
0377:
0378: // for coverage
0379: f = DateTimeFormat.forPattern("yyyyMMdd");
0380: assertEquals(dt.toString(), "2780040609", f.print(dt));
0381:
0382: // for coverage
0383: f = DateTimeFormat.forPattern("yyyyddMM");
0384: assertEquals(dt.toString(), "2780040906", f.print(dt));
0385: }
0386:
0387: //-----------------------------------------------------------------------
0388: public void testFormat_weekyear() {
0389: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
0390: DateTimeFormatter f = DateTimeFormat.forPattern("x")
0391: .withLocale(Locale.UK);
0392: assertEquals(dt.toString(), "2004", f.print(dt));
0393:
0394: dt = dt.withZone(NEWYORK);
0395: assertEquals(dt.toString(), "2004", f.print(dt));
0396:
0397: dt = dt.withZone(TOKYO);
0398: assertEquals(dt.toString(), "2004", f.print(dt));
0399:
0400: dt = new DateTime(-123, 6, 9, 10, 20, 30, 40, UTC);
0401: assertEquals(dt.toString(), "-123", f.print(dt));
0402: }
0403:
0404: public void testFormat_weekyearOfEra_twoDigit() {
0405: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
0406: DateTimeFormatter f = DateTimeFormat.forPattern("xx")
0407: .withLocale(Locale.UK);
0408: assertEquals(dt.toString(), "04", f.print(dt));
0409:
0410: dt = new DateTime(-123, 6, 9, 10, 20, 30, 40, UTC);
0411: assertEquals(dt.toString(), "23", f.print(dt));
0412:
0413: // current time set to 2002-06-09
0414: f = f.withZone(UTC);
0415: DateTime expect = null;
0416: expect = new DateTime(2003, 12, 29, 0, 0, 0, 0, UTC);
0417: assertEquals(expect, f.parseDateTime("04"));
0418:
0419: expect = new DateTime(1922, 1, 2, 0, 0, 0, 0, UTC);
0420: assertEquals(expect, f.parseDateTime("22"));
0421:
0422: expect = new DateTime(2021, 1, 4, 0, 0, 0, 0, UTC);
0423: assertEquals(expect, f.parseDateTime("21"));
0424:
0425: // Added tests to ensure single sign digit parse fails properly
0426: try {
0427: f.parseDateTime("-");
0428: fail();
0429: } catch (IllegalArgumentException ex) {
0430: }
0431:
0432: try {
0433: f.parseDateTime("+");
0434: fail();
0435: } catch (IllegalArgumentException ex) {
0436: }
0437:
0438: // Added tests for pivot year setting
0439: f = f.withPivotYear(new Integer(2050));
0440: expect = new DateTime(2000, 1, 3, 0, 0, 0, 0, DateTimeZone.UTC);
0441: assertEquals(expect, f.parseDateTime("00"));
0442:
0443: expect = new DateTime(2098, 12, 29, 0, 0, 0, 0,
0444: DateTimeZone.UTC);
0445: assertEquals(expect, f.parseDateTime("99"));
0446:
0447: // Added tests to ensure two digit parsing is strict by default for
0448: // DateTimeFormatterBuilder
0449: f = new DateTimeFormatterBuilder().appendTwoDigitWeekyear(2000)
0450: .toFormatter();
0451: f = f.withZone(UTC);
0452: try {
0453: f.parseDateTime("5");
0454: fail();
0455: } catch (IllegalArgumentException ex) {
0456: }
0457: try {
0458: f.parseDateTime("005");
0459: fail();
0460: } catch (IllegalArgumentException ex) {
0461: }
0462: try {
0463: f.parseDateTime("+50");
0464: fail();
0465: } catch (IllegalArgumentException ex) {
0466: }
0467: try {
0468: f.parseDateTime("-50");
0469: fail();
0470: } catch (IllegalArgumentException ex) {
0471: }
0472:
0473: // Added tests to ensure two digit parsing is lenient for DateTimeFormat
0474: f = DateTimeFormat.forPattern("xx").withLocale(Locale.UK);
0475: f = f.withZone(UTC);
0476: f.parseDateTime("5");
0477: f.parseDateTime("005");
0478: f.parseDateTime("+50");
0479: f.parseDateTime("-50");
0480:
0481: // Added tests for lenient two digit parsing
0482: f = new DateTimeFormatterBuilder().appendTwoDigitWeekyear(2000,
0483: true).toFormatter();
0484: f = f.withZone(UTC);
0485: expect = new DateTime(2003, 12, 29, 0, 0, 0, 0, UTC);
0486: assertEquals(expect, f.parseDateTime("04"));
0487:
0488: expect = new DateTime(3, 12, 29, 0, 0, 0, 0, UTC);
0489: assertEquals(expect, f.parseDateTime("+04"));
0490:
0491: expect = new DateTime(-4, 1, 1, 0, 0, 0, 0, UTC);
0492: assertEquals(expect, f.parseDateTime("-04"));
0493:
0494: expect = new DateTime(3, 12, 29, 0, 0, 0, 0, UTC);
0495: assertEquals(expect, f.parseDateTime("4"));
0496:
0497: expect = new DateTime(-4, 1, 1, 0, 0, 0, 0, UTC);
0498: assertEquals(expect, f.parseDateTime("-4"));
0499:
0500: expect = new DateTime(3, 12, 29, 0, 0, 0, 0, UTC);
0501: assertEquals(expect, f.parseDateTime("004"));
0502:
0503: expect = new DateTime(3, 12, 29, 0, 0, 0, 0, UTC);
0504: assertEquals(expect, f.parseDateTime("+004"));
0505:
0506: expect = new DateTime(-4, 1, 1, 0, 0, 0, 0, UTC);
0507: assertEquals(expect, f.parseDateTime("-004"));
0508:
0509: expect = new DateTime(3004, 1, 2, 0, 0, 0, 0, UTC);
0510: assertEquals(expect, f.parseDateTime("3004"));
0511:
0512: expect = new DateTime(3004, 1, 2, 0, 0, 0, 0, UTC);
0513: assertEquals(expect, f.parseDateTime("+3004"));
0514:
0515: expect = new DateTime(-3004, 1, 4, 0, 0, 0, 0, UTC);
0516: assertEquals(expect, f.parseDateTime("-3004"));
0517:
0518: try {
0519: f.parseDateTime("-");
0520: fail();
0521: } catch (IllegalArgumentException ex) {
0522: }
0523:
0524: try {
0525: f.parseDateTime("+");
0526: fail();
0527: } catch (IllegalArgumentException ex) {
0528: }
0529: }
0530:
0531: //-----------------------------------------------------------------------
0532: public void testFormat_weekOfWeekyear() {
0533: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
0534: DateTimeFormatter f = DateTimeFormat.forPattern("w")
0535: .withLocale(Locale.UK);
0536: assertEquals(dt.toString(), "24", f.print(dt));
0537:
0538: dt = dt.withZone(NEWYORK);
0539: assertEquals(dt.toString(), "24", f.print(dt));
0540:
0541: dt = dt.withZone(TOKYO);
0542: assertEquals(dt.toString(), "24", f.print(dt));
0543: }
0544:
0545: //-----------------------------------------------------------------------
0546: public void testFormat_dayOfWeek() {
0547: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
0548: DateTimeFormatter f = DateTimeFormat.forPattern("e")
0549: .withLocale(Locale.UK);
0550: assertEquals(dt.toString(), "3", f.print(dt));
0551:
0552: dt = dt.withZone(NEWYORK);
0553: assertEquals(dt.toString(), "3", f.print(dt));
0554:
0555: dt = dt.withZone(TOKYO);
0556: assertEquals(dt.toString(), "3", f.print(dt));
0557: }
0558:
0559: //-----------------------------------------------------------------------
0560: public void testFormat_dayOfWeekShortText() {
0561: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
0562: DateTimeFormatter f = DateTimeFormat.forPattern("E")
0563: .withLocale(Locale.UK);
0564: assertEquals(dt.toString(), "Wed", f.print(dt));
0565:
0566: dt = dt.withZone(NEWYORK);
0567: assertEquals(dt.toString(), "Wed", f.print(dt));
0568:
0569: dt = dt.withZone(TOKYO);
0570: assertEquals(dt.toString(), "Wed", f.print(dt));
0571:
0572: f = f.withLocale(Locale.FRENCH);
0573: assertEquals(dt.toString(), "mer.", f.print(dt));
0574: }
0575:
0576: //-----------------------------------------------------------------------
0577: public void testFormat_dayOfWeekText() {
0578: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
0579: DateTimeFormatter f = DateTimeFormat.forPattern("EEEE")
0580: .withLocale(Locale.UK);
0581: assertEquals(dt.toString(), "Wednesday", f.print(dt));
0582:
0583: dt = dt.withZone(NEWYORK);
0584: assertEquals(dt.toString(), "Wednesday", f.print(dt));
0585:
0586: dt = dt.withZone(TOKYO);
0587: assertEquals(dt.toString(), "Wednesday", f.print(dt));
0588:
0589: f = f.withLocale(Locale.FRENCH);
0590: assertEquals(dt.toString(), "mercredi", f.print(dt));
0591: }
0592:
0593: //-----------------------------------------------------------------------
0594: public void testFormat_dayOfYearText() {
0595: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
0596: DateTimeFormatter f = DateTimeFormat.forPattern("D")
0597: .withLocale(Locale.UK);
0598: assertEquals(dt.toString(), "161", f.print(dt));
0599:
0600: dt = dt.withZone(NEWYORK);
0601: assertEquals(dt.toString(), "161", f.print(dt));
0602:
0603: dt = dt.withZone(TOKYO);
0604: assertEquals(dt.toString(), "161", f.print(dt));
0605: }
0606:
0607: //-----------------------------------------------------------------------
0608: public void testFormat_monthOfYear() {
0609: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
0610: DateTimeFormatter f = DateTimeFormat.forPattern("M")
0611: .withLocale(Locale.UK);
0612: assertEquals(dt.toString(), "6", f.print(dt));
0613:
0614: dt = dt.withZone(NEWYORK);
0615: assertEquals(dt.toString(), "6", f.print(dt));
0616:
0617: dt = dt.withZone(TOKYO);
0618: assertEquals(dt.toString(), "6", f.print(dt));
0619: }
0620:
0621: //-----------------------------------------------------------------------
0622: public void testFormat_monthOfYearShortText() {
0623: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
0624: DateTimeFormatter f = DateTimeFormat.forPattern("MMM")
0625: .withLocale(Locale.UK);
0626: assertEquals(dt.toString(), "Jun", f.print(dt));
0627:
0628: dt = dt.withZone(NEWYORK);
0629: assertEquals(dt.toString(), "Jun", f.print(dt));
0630:
0631: dt = dt.withZone(TOKYO);
0632: assertEquals(dt.toString(), "Jun", f.print(dt));
0633:
0634: f = f.withLocale(Locale.FRENCH);
0635: assertEquals(dt.toString(), "juin", f.print(dt));
0636: }
0637:
0638: //-----------------------------------------------------------------------
0639: public void testFormat_monthOfYearText() {
0640: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
0641: DateTimeFormatter f = DateTimeFormat.forPattern("MMMM")
0642: .withLocale(Locale.UK);
0643: assertEquals(dt.toString(), "June", f.print(dt));
0644:
0645: dt = dt.withZone(NEWYORK);
0646: assertEquals(dt.toString(), "June", f.print(dt));
0647:
0648: dt = dt.withZone(TOKYO);
0649: assertEquals(dt.toString(), "June", f.print(dt));
0650:
0651: f = f.withLocale(Locale.FRENCH);
0652: assertEquals(dt.toString(), "juin", f.print(dt));
0653: }
0654:
0655: //-----------------------------------------------------------------------
0656: public void testFormat_dayOfMonth() {
0657: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
0658: DateTimeFormatter f = DateTimeFormat.forPattern("d")
0659: .withLocale(Locale.UK);
0660: assertEquals(dt.toString(), "9", f.print(dt));
0661:
0662: dt = dt.withZone(NEWYORK);
0663: assertEquals(dt.toString(), "9", f.print(dt));
0664:
0665: dt = dt.withZone(TOKYO);
0666: assertEquals(dt.toString(), "9", f.print(dt));
0667: }
0668:
0669: //-----------------------------------------------------------------------
0670: public void testFormat_halfdayOfDay() {
0671: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
0672: DateTimeFormatter f = DateTimeFormat.forPattern("a")
0673: .withLocale(Locale.UK);
0674: assertEquals(dt.toString(), "AM", f.print(dt));
0675:
0676: dt = dt.withZone(NEWYORK);
0677: assertEquals(dt.toString(), "AM", f.print(dt));
0678:
0679: dt = dt.withZone(TOKYO);
0680: assertEquals(dt.toString(), "PM", f.print(dt));
0681: }
0682:
0683: //-----------------------------------------------------------------------
0684: public void testFormat_hourOfHalfday() {
0685: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
0686: DateTimeFormatter f = DateTimeFormat.forPattern("K")
0687: .withLocale(Locale.UK);
0688: assertEquals(dt.toString(), "10", f.print(dt));
0689:
0690: dt = dt.withZone(NEWYORK);
0691: assertEquals(dt.toString(), "6", f.print(dt));
0692:
0693: dt = dt.withZone(TOKYO);
0694: assertEquals(dt.toString(), "7", f.print(dt));
0695:
0696: dt = new DateTime(2004, 6, 9, 0, 0, 0, 0, UTC);
0697: assertEquals(dt.toString(), "0", f.print(dt));
0698: }
0699:
0700: //-----------------------------------------------------------------------
0701: public void testFormat_clockhourOfHalfday() {
0702: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
0703: DateTimeFormatter f = DateTimeFormat.forPattern("h")
0704: .withLocale(Locale.UK);
0705: assertEquals(dt.toString(), "10", f.print(dt));
0706:
0707: dt = dt.withZone(NEWYORK);
0708: assertEquals(dt.toString(), "6", f.print(dt));
0709:
0710: dt = dt.withZone(TOKYO);
0711: assertEquals(dt.toString(), "7", f.print(dt));
0712:
0713: dt = new DateTime(2004, 6, 9, 0, 0, 0, 0, UTC);
0714: assertEquals(dt.toString(), "12", f.print(dt));
0715: }
0716:
0717: //-----------------------------------------------------------------------
0718: public void testFormat_hourOfDay() {
0719: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
0720: DateTimeFormatter f = DateTimeFormat.forPattern("H")
0721: .withLocale(Locale.UK);
0722: assertEquals(dt.toString(), "10", f.print(dt));
0723:
0724: dt = dt.withZone(NEWYORK);
0725: assertEquals(dt.toString(), "6", f.print(dt));
0726:
0727: dt = dt.withZone(TOKYO);
0728: assertEquals(dt.toString(), "19", f.print(dt));
0729:
0730: dt = new DateTime(2004, 6, 9, 0, 0, 0, 0, UTC);
0731: assertEquals(dt.toString(), "0", f.print(dt));
0732: }
0733:
0734: //-----------------------------------------------------------------------
0735: public void testFormat_clockhourOfDay() {
0736: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
0737: DateTimeFormatter f = DateTimeFormat.forPattern("k")
0738: .withLocale(Locale.UK);
0739: assertEquals(dt.toString(), "10", f.print(dt));
0740:
0741: dt = dt.withZone(NEWYORK);
0742: assertEquals(dt.toString(), "6", f.print(dt));
0743:
0744: dt = dt.withZone(TOKYO);
0745: assertEquals(dt.toString(), "19", f.print(dt));
0746:
0747: dt = new DateTime(2004, 6, 9, 0, 0, 0, 0, UTC);
0748: assertEquals(dt.toString(), "24", f.print(dt));
0749: }
0750:
0751: //-----------------------------------------------------------------------
0752: public void testFormat_minute() {
0753: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
0754: DateTimeFormatter f = DateTimeFormat.forPattern("m")
0755: .withLocale(Locale.UK);
0756: assertEquals(dt.toString(), "20", f.print(dt));
0757:
0758: dt = dt.withZone(NEWYORK);
0759: assertEquals(dt.toString(), "20", f.print(dt));
0760:
0761: dt = dt.withZone(TOKYO);
0762: assertEquals(dt.toString(), "20", f.print(dt));
0763: }
0764:
0765: //-----------------------------------------------------------------------
0766: public void testFormat_second() {
0767: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
0768: DateTimeFormatter f = DateTimeFormat.forPattern("s")
0769: .withLocale(Locale.UK);
0770: assertEquals(dt.toString(), "30", f.print(dt));
0771:
0772: dt = dt.withZone(NEWYORK);
0773: assertEquals(dt.toString(), "30", f.print(dt));
0774:
0775: dt = dt.withZone(TOKYO);
0776: assertEquals(dt.toString(), "30", f.print(dt));
0777: }
0778:
0779: //-----------------------------------------------------------------------
0780: public void testFormat_fractionOfSecond() {
0781: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
0782: DateTimeFormatter f = DateTimeFormat.forPattern("SSS")
0783: .withLocale(Locale.UK);
0784: assertEquals(dt.toString(), "040", f.print(dt));
0785:
0786: dt = dt.withZone(NEWYORK);
0787: assertEquals(dt.toString(), "040", f.print(dt));
0788:
0789: dt = dt.withZone(TOKYO);
0790: assertEquals(dt.toString(), "040", f.print(dt));
0791: }
0792:
0793: //-----------------------------------------------------------------------
0794: public void testFormat_fractionOfSecondLong() {
0795: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
0796: DateTimeFormatter f = DateTimeFormat.forPattern("SSSSSS")
0797: .withLocale(Locale.UK);
0798: assertEquals(dt.toString(), "040000", f.print(dt));
0799:
0800: dt = dt.withZone(NEWYORK);
0801: assertEquals(dt.toString(), "040000", f.print(dt));
0802:
0803: dt = dt.withZone(TOKYO);
0804: assertEquals(dt.toString(), "040000", f.print(dt));
0805: }
0806:
0807: //-----------------------------------------------------------------------
0808: public void testFormat_zoneText() {
0809: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
0810: DateTimeFormatter f = DateTimeFormat.forPattern("z")
0811: .withLocale(Locale.UK);
0812: assertEquals(dt.toString(), "UTC", f.print(dt));
0813:
0814: dt = dt.withZone(NEWYORK);
0815: assertEquals(dt.toString(), "EDT", f.print(dt));
0816:
0817: dt = dt.withZone(TOKYO);
0818: assertEquals(dt.toString(), "JST", f.print(dt));
0819: }
0820:
0821: public void testFormat_zoneLongText() {
0822: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
0823: DateTimeFormatter f = DateTimeFormat.forPattern("zzzz")
0824: .withLocale(Locale.UK);
0825: assertEquals(dt.toString(), "Coordinated Universal Time", f
0826: .print(dt));
0827:
0828: dt = dt.withZone(NEWYORK);
0829: assertEquals(dt.toString(), "Eastern Daylight Time", f
0830: .print(dt));
0831:
0832: dt = dt.withZone(TOKYO);
0833: assertEquals(dt.toString(), "Japan Standard Time", f.print(dt));
0834: }
0835:
0836: //-----------------------------------------------------------------------
0837: public void testFormat_zoneAmount() {
0838: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
0839: DateTimeFormatter f = DateTimeFormat.forPattern("Z")
0840: .withLocale(Locale.UK);
0841: assertEquals(dt.toString(), "+0000", f.print(dt));
0842:
0843: dt = dt.withZone(NEWYORK);
0844: assertEquals(dt.toString(), "-0400", f.print(dt));
0845:
0846: dt = dt.withZone(TOKYO);
0847: assertEquals(dt.toString(), "+0900", f.print(dt));
0848: }
0849:
0850: public void testFormat_zoneAmountColon() {
0851: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
0852: DateTimeFormatter f = DateTimeFormat.forPattern("ZZ")
0853: .withLocale(Locale.UK);
0854: assertEquals(dt.toString(), "+00:00", f.print(dt));
0855:
0856: dt = dt.withZone(NEWYORK);
0857: assertEquals(dt.toString(), "-04:00", f.print(dt));
0858:
0859: dt = dt.withZone(TOKYO);
0860: assertEquals(dt.toString(), "+09:00", f.print(dt));
0861: }
0862:
0863: public void testFormat_zoneAmountID() {
0864: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
0865: DateTimeFormatter f = DateTimeFormat.forPattern("ZZZ")
0866: .withLocale(Locale.UK);
0867: assertEquals(dt.toString(), "UTC", f.print(dt));
0868:
0869: dt = dt.withZone(NEWYORK);
0870: assertEquals(dt.toString(), "America/New_York", f.print(dt));
0871:
0872: dt = dt.withZone(TOKYO);
0873: assertEquals(dt.toString(), "Asia/Tokyo", f.print(dt));
0874: }
0875:
0876: //-----------------------------------------------------------------------
0877: public void testFormat_other() {
0878: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
0879: DateTimeFormatter f = DateTimeFormat.forPattern("'Hello' ''");
0880: assertEquals("Hello '", f.print(dt));
0881: }
0882:
0883: public void testFormat_invalid() {
0884: try {
0885: DateTimeFormat.forPattern(null);
0886: fail();
0887: } catch (IllegalArgumentException ex) {
0888: }
0889: try {
0890: DateTimeFormat.forPattern("");
0891: fail();
0892: } catch (IllegalArgumentException ex) {
0893: }
0894: try {
0895: DateTimeFormat.forPattern("A");
0896: fail();
0897: } catch (IllegalArgumentException ex) {
0898: }
0899: try {
0900: DateTimeFormat.forPattern("dd/mm/AA");
0901: fail();
0902: } catch (IllegalArgumentException ex) {
0903: }
0904: }
0905:
0906: public void testFormat_samples() {
0907: DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 40, UTC);
0908: DateTimeFormatter f = DateTimeFormat
0909: .forPattern("yyyy-MM-dd HH.mm.ss");
0910: assertEquals("2004-06-09 10.20.30", f.print(dt));
0911: }
0912:
0913: public void testFormat_shortBasicParse() {
0914: // Tests special two digit parse to make sure it properly switches
0915: // between lenient and strict parsing.
0916:
0917: DateTime dt = new DateTime(2004, 3, 9, 0, 0, 0, 0);
0918:
0919: DateTimeFormatter f = DateTimeFormat.forPattern("yyMMdd");
0920: assertEquals(dt, f.parseDateTime("040309"));
0921: try {
0922: assertEquals(dt, f.parseDateTime("20040309"));
0923: fail();
0924: } catch (IllegalArgumentException ex) {
0925: }
0926:
0927: f = DateTimeFormat.forPattern("yy/MM/dd");
0928: assertEquals(dt, f.parseDateTime("04/03/09"));
0929: assertEquals(dt, f.parseDateTime("2004/03/09"));
0930: }
0931:
0932: //-----------------------------------------------------------------------
0933: public void testParse_pivotYear() {
0934: DateTimeFormatter dateFormatter = DateTimeFormat.forPattern(
0935: "dd.MM.yy").withPivotYear(2050).withZone(
0936: DateTimeZone.UTC);
0937:
0938: DateTime date = dateFormatter.parseDateTime("25.12.15");
0939: assertEquals(date.getYear(), 2015);
0940:
0941: date = dateFormatter.parseDateTime("25.12.00");
0942: assertEquals(date.getYear(), 2000);
0943:
0944: date = dateFormatter.parseDateTime("25.12.99");
0945: assertEquals(date.getYear(), 2099);
0946: }
0947:
0948: public void testParse_pivotYear_ignored4DigitYear() {
0949: DateTimeFormatter dateFormatter = DateTimeFormat.forPattern(
0950: "dd.MM.yyyy").withPivotYear(2050).withZone(
0951: DateTimeZone.UTC);
0952:
0953: DateTime date = dateFormatter.parseDateTime("25.12.15");
0954: assertEquals(date.getYear(), 15);
0955:
0956: date = dateFormatter.parseDateTime("25.12.00");
0957: assertEquals(date.getYear(), 0);
0958:
0959: date = dateFormatter.parseDateTime("25.12.99");
0960: assertEquals(date.getYear(), 99);
0961: }
0962:
0963: //-----------------------------------------------------------------------
0964: public void testFormatParse_textMonthJanShort_UK() {
0965: DateTimeFormatter dateFormatter = DateTimeFormat.forPattern(
0966: "dd MMM yyyy").withLocale(Locale.UK).withZone(
0967: DateTimeZone.UTC);
0968:
0969: String str = new DateTime(2007, 1, 23, 0, 0, 0, 0, UTC)
0970: .toString(dateFormatter);
0971: assertEquals(str, "23 Jan 2007");
0972: DateTime date = dateFormatter.parseDateTime(str);
0973: check(date, 2007, 1, 23);
0974: }
0975:
0976: public void testFormatParse_textMonthJanShortLowerCase_UK() {
0977: DateTimeFormatter dateFormatter = DateTimeFormat.forPattern(
0978: "dd MMM yyyy").withLocale(Locale.UK).withZone(
0979: DateTimeZone.UTC);
0980: DateTime date = dateFormatter.parseDateTime("23 jan 2007");
0981: check(date, 2007, 1, 23);
0982: }
0983:
0984: public void testFormatParse_textMonthJanShortUpperCase_UK() {
0985: DateTimeFormatter dateFormatter = DateTimeFormat.forPattern(
0986: "dd MMM yyyy").withLocale(Locale.UK).withZone(
0987: DateTimeZone.UTC);
0988: DateTime date = dateFormatter.parseDateTime("23 JAN 2007");
0989: check(date, 2007, 1, 23);
0990: }
0991:
0992: public void testParse_textMonthJanLong_UK() {
0993: DateTimeFormatter dateFormatter = DateTimeFormat.forPattern(
0994: "dd MMM yyyy").withLocale(Locale.UK).withZone(
0995: DateTimeZone.UTC);
0996:
0997: DateTime date = dateFormatter.parseDateTime("23 January 2007");
0998: check(date, 2007, 1, 23);
0999: }
1000:
1001: public void testFormatParse_textMonthJanLongLowerCase_UK() {
1002: DateTimeFormatter dateFormatter = DateTimeFormat.forPattern(
1003: "dd MMM yyyy").withLocale(Locale.UK).withZone(
1004: DateTimeZone.UTC);
1005: DateTime date = dateFormatter.parseDateTime("23 january 2007");
1006: check(date, 2007, 1, 23);
1007: }
1008:
1009: public void testFormatParse_textMonthJanLongUpperCase_UK() {
1010: DateTimeFormatter dateFormatter = DateTimeFormat.forPattern(
1011: "dd MMM yyyy").withLocale(Locale.UK).withZone(
1012: DateTimeZone.UTC);
1013: DateTime date = dateFormatter.parseDateTime("23 JANUARY 2007");
1014: check(date, 2007, 1, 23);
1015: }
1016:
1017: public void testFormatParse_textMonthJanShort_France() {
1018: DateTimeFormatter dateFormatter = DateTimeFormat.forPattern(
1019: "dd MMM yyyy").withLocale(Locale.FRANCE).withZone(
1020: DateTimeZone.UTC);
1021:
1022: String str = new DateTime(2007, 1, 23, 0, 0, 0, 0, UTC)
1023: .toString(dateFormatter);
1024: assertEquals("23 janv. 2007", str);
1025: DateTime date = dateFormatter.parseDateTime(str);
1026: check(date, 2007, 1, 23);
1027: }
1028:
1029: public void testFormatParse_textMonthJanLong_France() {
1030: DateTimeFormatter dateFormatter = DateTimeFormat.forPattern(
1031: "dd MMM yyyy").withLocale(Locale.FRANCE).withZone(
1032: DateTimeZone.UTC);
1033:
1034: DateTime date = dateFormatter.parseDateTime("23 janvier 2007");
1035: check(date, 2007, 1, 23);
1036: }
1037:
1038: public void testFormatParse_textMonthApr_France() {
1039: DateTimeFormatter dateFormatter = DateTimeFormat.forPattern(
1040: "dd MMM yyyy").withLocale(Locale.FRANCE).withZone(
1041: DateTimeZone.UTC);
1042:
1043: String str = new DateTime(2007, 2, 23, 0, 0, 0, 0, UTC)
1044: .toString(dateFormatter);
1045: assertEquals("23 f\u00E9vr. 2007", str); // e acute
1046: DateTime date = dateFormatter.parseDateTime(str);
1047: check(date, 2007, 2, 23);
1048: }
1049:
1050: public void testFormatParse_textMonthAtEnd_France() {
1051: DateTimeFormatter dateFormatter = DateTimeFormat.forPattern(
1052: "dd MMM").withLocale(Locale.FRANCE).withZone(
1053: DateTimeZone.UTC);
1054:
1055: String str = new DateTime(2007, 6, 23, 0, 0, 0, 0, UTC)
1056: .toString(dateFormatter);
1057: assertEquals("23 juin", str);
1058: DateTime date = dateFormatter.parseDateTime(str);
1059: check(date, 1970, 6, 23);
1060: }
1061:
1062: public void testFormatParse_textMonthApr_Korean() {
1063: DateTimeFormatter dateFormatter = DateTimeFormat.forPattern(
1064: "EEEE, d MMMM yyyy HH:mm").withLocale(Locale.KOREAN)
1065: .withZone(DateTimeZone.UTC);
1066:
1067: String str = new DateTime(2007, 3, 8, 22, 0, 0, 0, UTC)
1068: .toString(dateFormatter);
1069: DateTime date = dateFormatter.parseDateTime(str);
1070: assertEquals(new DateTime(2007, 3, 8, 22, 0, 0, 0, UTC), date);
1071: }
1072:
1073: //-----------------------------------------------------------------------
1074: public void testFormatParse_textHalfdayAM_UK() {
1075: DateTimeFormatter dateFormatter = new DateTimeFormatterBuilder()
1076: .appendLiteral('$').appendClockhourOfHalfday(2)
1077: .appendLiteral('-').appendHalfdayOfDayText()
1078: .appendLiteral('-').appendYear(4, 4).toFormatter()
1079: .withLocale(Locale.UK).withZone(DateTimeZone.UTC);
1080:
1081: String str = new DateTime(2007, 6, 23, 18, 0, 0, 0, UTC)
1082: .toString(dateFormatter);
1083: assertEquals("$06-PM-2007", str);
1084: DateTime date = dateFormatter.parseDateTime(str);
1085: check(date, 2007, 1, 1);
1086: }
1087:
1088: public void testFormatParse_textHalfdayAM_France() {
1089: DateTimeFormatter dateFormatter = new DateTimeFormatterBuilder()
1090: .appendLiteral('$').appendClockhourOfHalfday(2)
1091: .appendLiteral('-').appendHalfdayOfDayText()
1092: .appendLiteral('-').appendYear(4, 4).toFormatter()
1093: .withLocale(Locale.FRANCE).withZone(DateTimeZone.UTC);
1094:
1095: String str = new DateTime(2007, 6, 23, 18, 0, 0, 0, UTC)
1096: .toString(dateFormatter);
1097: assertEquals("$06-PM-2007", str);
1098: DateTime date = dateFormatter.parseDateTime(str);
1099: check(date, 2007, 1, 1);
1100: }
1101:
1102: //-----------------------------------------------------------------------
1103: public void testFormatParse_textEraAD_UK() {
1104: DateTimeFormatter dateFormatter = new DateTimeFormatterBuilder()
1105: .appendLiteral('$').appendEraText().appendYear(4, 4)
1106: .toFormatter().withLocale(Locale.UK).withZone(
1107: DateTimeZone.UTC);
1108:
1109: String str = new DateTime(2007, 6, 23, 0, 0, 0, 0, UTC)
1110: .toString(dateFormatter);
1111: assertEquals("$AD2007", str);
1112: DateTime date = dateFormatter.parseDateTime(str);
1113: check(date, 2007, 1, 1);
1114: }
1115:
1116: public void testFormatParse_textEraAD_France() {
1117: DateTimeFormatter dateFormatter = new DateTimeFormatterBuilder()
1118: .appendLiteral('$').appendEraText().appendYear(4, 4)
1119: .toFormatter().withLocale(Locale.FRANCE).withZone(
1120: DateTimeZone.UTC);
1121:
1122: String str = new DateTime(2007, 6, 23, 0, 0, 0, 0, UTC)
1123: .toString(dateFormatter);
1124: assertEquals("$ap. J.-C.2007", str);
1125: DateTime date = dateFormatter.parseDateTime(str);
1126: check(date, 2007, 1, 1);
1127: }
1128:
1129: public void testFormatParse_textEraBC_France() {
1130: DateTimeFormatter dateFormatter = new DateTimeFormatterBuilder()
1131: .appendLiteral('$').appendEraText().appendYear(4, 4)
1132: .toFormatter().withLocale(Locale.FRANCE).withZone(
1133: DateTimeZone.UTC);
1134:
1135: String str = new DateTime(-1, 6, 23, 0, 0, 0, 0, UTC)
1136: .toString(dateFormatter);
1137: assertEquals("$BC-0001", str);
1138: DateTime date = dateFormatter.parseDateTime(str);
1139: check(date, -1, 1, 1);
1140: }
1141:
1142: //-----------------------------------------------------------------------
1143: public void testFormatParse_textYear_UK() {
1144: DateTimeFormatter dateFormatter = new DateTimeFormatterBuilder()
1145: .appendLiteral('$')
1146: .appendText(DateTimeFieldType.year()).toFormatter()
1147: .withLocale(Locale.UK).withZone(DateTimeZone.UTC);
1148:
1149: String str = new DateTime(2007, 6, 23, 0, 0, 0, 0, UTC)
1150: .toString(dateFormatter);
1151: assertEquals("$2007", str);
1152: try {
1153: dateFormatter.parseDateTime(str);
1154: fail();
1155: } catch (IllegalArgumentException ex) {
1156: // expected
1157: }
1158: }
1159:
1160: public void testFormatParse_textYear_France() {
1161: DateTimeFormatter dateFormatter = new DateTimeFormatterBuilder()
1162: .appendLiteral('$')
1163: .appendText(DateTimeFieldType.year()).toFormatter()
1164: .withLocale(Locale.FRANCE).withZone(DateTimeZone.UTC);
1165:
1166: String str = new DateTime(2007, 6, 23, 0, 0, 0, 0, UTC)
1167: .toString(dateFormatter);
1168: assertEquals("$2007", str);
1169: try {
1170: dateFormatter.parseDateTime(str);
1171: fail();
1172: } catch (IllegalArgumentException ex) {
1173: // expected
1174: }
1175: }
1176:
1177: //-----------------------------------------------------------------------
1178: public void testFormatParse_textAdjoiningHelloWorld_UK() {
1179: DateTimeFormatter dateFormatter = new DateTimeFormatterBuilder()
1180: .appendLiteral('$').appendDayOfMonth(2)
1181: .appendMonthOfYearShortText().appendLiteral(
1182: "HelloWorld").toFormatter().withLocale(
1183: Locale.UK).withZone(DateTimeZone.UTC);
1184:
1185: String str = new DateTime(2007, 6, 23, 0, 0, 0, 0, UTC)
1186: .toString(dateFormatter);
1187: assertEquals("$23JunHelloWorld", str);
1188: dateFormatter.parseDateTime(str);
1189: }
1190:
1191: public void testFormatParse_textAdjoiningMonthDOW_UK() {
1192: DateTimeFormatter dateFormatter = new DateTimeFormatterBuilder()
1193: .appendLiteral('$').appendDayOfMonth(2)
1194: .appendMonthOfYearShortText()
1195: .appendDayOfWeekShortText().toFormatter().withLocale(
1196: Locale.UK).withZone(DateTimeZone.UTC);
1197:
1198: String str = new DateTime(2007, 6, 23, 0, 0, 0, 0, UTC)
1199: .toString(dateFormatter);
1200: assertEquals("$23JunSat", str);
1201: dateFormatter.parseDateTime(str);
1202: }
1203:
1204: //-----------------------------------------------------------------------
1205: private void check(DateTime test, int hour, int min, int sec) {
1206: assertEquals(hour, test.getYear());
1207: assertEquals(min, test.getMonthOfYear());
1208: assertEquals(sec, test.getDayOfMonth());
1209: }
1210:
1211: }
|