1 16 package org.joda.time.format; 17 18 import java.util.Locale ; 19 import java.util.TimeZone ; 20 21 import junit.framework.TestCase; 22 import junit.framework.TestSuite; 23 24 import org.joda.time.DateTimeConstants; 25 import org.joda.time.DateTimeUtils; 26 import org.joda.time.DateTimeZone; 27 import org.joda.time.Period; 28 import org.joda.time.PeriodType; 29 30 35 public class TestPeriodFormatParsing extends TestCase { 36 37 private static final Period PERIOD = new Period(1, 2, 3, 4, 5, 6, 7, 8); 38 private static final Period EMPTY_PERIOD = new Period(0, 0, 0, 0, 0, 0, 0, 0); 39 private static final Period YEAR_DAY_PERIOD = new Period(1, 0, 0, 4, 5, 6, 7, 8, PeriodType.yearDayTime()); 40 private static final Period EMPTY_YEAR_DAY_PERIOD = new Period(0, 0, 0, 0, 0, 0, 0, 0, PeriodType.yearDayTime()); 41 private static final Period TIME_PERIOD = new Period(0, 0, 0, 0, 5, 6, 7, 8); 42 private static final Period DATE_PERIOD = new Period(1, 2, 3, 4, 0, 0, 0, 0); 43 44 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris"); 45 private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London"); 46 private static final DateTimeZone TOKYO = DateTimeZone.forID("Asia/Tokyo"); 47 48 long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 49 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 50 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 51 366 + 365; 52 private long TEST_TIME_NOW = (y2002days + 31L + 28L + 31L + 30L + 31L + 9L - 1L) * DateTimeConstants.MILLIS_PER_DAY; 54 55 private DateTimeZone originalDateTimeZone = null; 56 private TimeZone originalTimeZone = null; 57 private Locale originalLocale = null; 58 59 public static void main(String [] args) { 60 junit.textui.TestRunner.run(suite()); 61 } 62 63 public static TestSuite suite() { 64 return new TestSuite(TestPeriodFormatParsing.class); 65 } 66 67 public TestPeriodFormatParsing(String name) { 68 super(name); 69 } 70 71 protected void setUp() throws Exception { 72 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW); 73 originalDateTimeZone = DateTimeZone.getDefault(); 74 originalTimeZone = TimeZone.getDefault(); 75 originalLocale = Locale.getDefault(); 76 DateTimeZone.setDefault(LONDON); 77 TimeZone.setDefault(TimeZone.getTimeZone("Europe/London")); 78 Locale.setDefault(Locale.UK); 79 } 80 81 protected void tearDown() throws Exception { 82 DateTimeUtils.setCurrentMillisSystem(); 83 DateTimeZone.setDefault(originalDateTimeZone); 84 TimeZone.setDefault(originalTimeZone); 85 Locale.setDefault(originalLocale); 86 originalDateTimeZone = null; 87 originalTimeZone = null; 88 originalLocale = null; 89 } 90 91 public void testParseStandard1() { 93 PeriodFormatter parser = PeriodFormat.getDefault(); 94 Period p = parser.parsePeriod("6 years, 3 months and 2 days"); 95 assertEquals(new Period(6, 3, 0, 2, 0, 0, 0, 0), p); 96 } 97 98 public void testParseCustom1() { 99 PeriodFormatter formatter = new PeriodFormatterBuilder() 100 .printZeroAlways() 101 .appendHours() 102 .appendSuffix(":") 103 .minimumPrintedDigits(2) 104 .appendMinutes() 105 .toFormatter(); 106 107 Period p; 108 109 p = new Period(47, 55, 0, 0); 110 assertEquals("47:55", formatter.print(p)); 111 assertEquals(p, formatter.parsePeriod("47:55")); 112 assertEquals(p, formatter.parsePeriod("047:055")); 113 114 p = new Period(7, 5, 0, 0); 115 assertEquals("7:05", formatter.print(p)); 116 assertEquals(p, formatter.parsePeriod("7:05")); 117 assertEquals(p, formatter.parsePeriod("7:5")); 118 assertEquals(p, formatter.parsePeriod("07:05")); 119 120 p = new Period(0, 5, 0, 0); 121 assertEquals("0:05", formatter.print(p)); 122 assertEquals(p, formatter.parsePeriod("0:05")); 123 assertEquals(p, formatter.parsePeriod("0:5")); 124 assertEquals(p, formatter.parsePeriod("00:005")); 125 assertEquals(p, formatter.parsePeriod("0:005")); 126 127 p = new Period(0, 0, 0, 0); 128 assertEquals("0:00", formatter.print(p)); 129 assertEquals(p, formatter.parsePeriod("0:00")); 130 assertEquals(p, formatter.parsePeriod("0:0")); 131 assertEquals(p, formatter.parsePeriod("00:00")); 132 } 133 134 } 135 | Popular Tags |