1 16 package org.joda.time.format; 17 18 import java.text.DateFormat ; 19 import java.util.Locale ; 20 import java.util.SimpleTimeZone ; 21 import java.util.TimeZone ; 22 23 import junit.framework.TestCase; 24 import junit.framework.TestSuite; 25 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 31 36 public class TestDateTimeFormatStyle extends TestCase { 37 38 private static final Locale UK = Locale.UK; 39 private static final Locale US = Locale.US; 40 private static final Locale FRANCE = Locale.FRANCE; 41 private static final DateTimeZone UTC = DateTimeZone.UTC; 42 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris"); 43 private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London"); 44 private static final DateTimeZone TOKYO = DateTimeZone.forID("Asia/Tokyo"); 45 private static final DateTimeZone NEWYORK = DateTimeZone.forID("America/New_York"); 46 47 long y2002days = 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 48 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 49 365 + 365 + 366 + 365 + 365 + 365 + 366 + 365 + 365 + 365 + 50 366 + 365; 51 private long TEST_TIME_NOW = 53 (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(TestDateTimeFormatStyle.class); 65 } 66 67 public TestDateTimeFormatStyle(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(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 testForStyle_stringLengths() { 93 try { 94 DateTimeFormat.forStyle(null); 95 fail(); 96 } catch (IllegalArgumentException ex) {} 97 try { 98 DateTimeFormat.forStyle(""); 99 fail(); 100 } catch (IllegalArgumentException ex) {} 101 try { 102 DateTimeFormat.forStyle("S"); 103 fail(); 104 } catch (IllegalArgumentException ex) {} 105 try { 106 DateTimeFormat.forStyle("SSS"); 107 fail(); 108 } catch (IllegalArgumentException ex) {} 109 } 110 111 public void testForStyle_invalidStrings() { 112 try { 113 DateTimeFormat.forStyle("AA"); 114 fail(); 115 } catch (IllegalArgumentException ex) {} 116 try { 117 DateTimeFormat.forStyle("--"); 118 fail(); 119 } catch (IllegalArgumentException ex) {} 120 try { 121 DateTimeFormat.forStyle("ss"); 122 fail(); 123 } catch (IllegalArgumentException ex) {} 124 } 125 126 public void testForStyle_shortDate() throws Exception { 128 DateTimeFormatter f = DateTimeFormat.shortDate(); 129 DateTimeFormatter g = DateTimeFormat.forStyle("S-"); 130 assertSame(g, f); 131 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0); 132 String expect = DateFormat.getDateInstance(DateFormat.SHORT, UK).format(dt.toDate()); 133 assertEquals(expect, f.print(dt)); 134 expect = DateFormat.getDateInstance(DateFormat.SHORT, US).format(dt.toDate()); 135 assertEquals(expect, f.withLocale(US).print(dt)); 136 expect = DateFormat.getDateInstance(DateFormat.SHORT, FRANCE).format(dt.toDate()); 137 assertEquals(expect, f.withLocale(FRANCE).print(dt)); 138 139 DateTime date = new DateTime( 140 DateFormat.getDateInstance(DateFormat.SHORT, FRANCE).parse(expect)); 141 assertEquals(date, f.withLocale(FRANCE).parseDateTime(expect)); 142 } 143 144 public void testForStyle_shortTime() throws Exception { 145 DateTimeFormatter f = DateTimeFormat.shortTime(); 146 DateTimeFormatter g = DateTimeFormat.forStyle("-S"); 147 assertSame(g, f); 148 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0); 149 String expect = DateFormat.getTimeInstance(DateFormat.SHORT, UK).format(dt.toDate()); 150 assertEquals(expect, f.print(dt)); 151 expect = DateFormat.getTimeInstance(DateFormat.SHORT, US).format(dt.toDate()); 152 assertEquals(expect, f.withLocale(US).print(dt)); 153 expect = DateFormat.getTimeInstance(DateFormat.SHORT, FRANCE).format(dt.toDate()); 154 assertEquals(expect, f.withLocale(FRANCE).print(dt)); 155 156 if (TimeZone.getDefault() instanceof SimpleTimeZone ) { 157 } else { 159 DateTime date = new DateTime( 160 DateFormat.getTimeInstance(DateFormat.SHORT, FRANCE).parse(expect)); 161 assertEquals(date, f.withLocale(FRANCE).parseDateTime(expect)); 162 } 163 } 164 165 public void testForStyle_shortDateTime() throws Exception { 166 DateTimeFormatter f = DateTimeFormat.shortDateTime(); 167 DateTimeFormatter g = DateTimeFormat.forStyle("SS"); 168 assertSame(g, f); 169 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0); 170 String expect = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, UK).format(dt.toDate()); 171 assertEquals(expect, f.print(dt)); 172 expect = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, US).format(dt.toDate()); 173 assertEquals(expect, f.withLocale(US).print(dt)); 174 expect = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, FRANCE).format(dt.toDate()); 175 assertEquals(expect, f.withLocale(FRANCE).print(dt)); 176 177 DateTime date = new DateTime( 178 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, FRANCE).parse(expect)); 179 assertEquals(date, f.withLocale(FRANCE).parseDateTime(expect)); 180 } 181 182 public void testForStyle_mediumDate() throws Exception { 184 DateTimeFormatter f = DateTimeFormat.mediumDate(); 185 DateTimeFormatter g = DateTimeFormat.forStyle("M-"); 186 assertSame(g, f); 187 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0); 188 String expect = DateFormat.getDateInstance(DateFormat.MEDIUM, UK).format(dt.toDate()); 189 assertEquals(expect, f.print(dt)); 190 expect = DateFormat.getDateInstance(DateFormat.MEDIUM, US).format(dt.toDate()); 191 assertEquals(expect, f.withLocale(US).print(dt)); 192 expect = DateFormat.getDateInstance(DateFormat.MEDIUM, FRANCE).format(dt.toDate()); 193 assertEquals(expect, f.withLocale(FRANCE).print(dt)); 194 } 195 196 public void testForStyle_mediumTime() throws Exception { 197 DateTimeFormatter f = DateTimeFormat.mediumTime(); 198 DateTimeFormatter g = DateTimeFormat.forStyle("-M"); 199 assertSame(g, f); 200 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0); 201 String expect = DateFormat.getTimeInstance(DateFormat.MEDIUM, UK).format(dt.toDate()); 202 assertEquals(expect, f.print(dt)); 203 expect = DateFormat.getTimeInstance(DateFormat.MEDIUM, US).format(dt.toDate()); 204 assertEquals(expect, f.withLocale(US).print(dt)); 205 expect = DateFormat.getTimeInstance(DateFormat.MEDIUM, FRANCE).format(dt.toDate()); 206 assertEquals(expect, f.withLocale(FRANCE).print(dt)); 207 } 208 209 public void testForStyle_mediumDateTime() throws Exception { 210 DateTimeFormatter f = DateTimeFormat.mediumDateTime(); 211 DateTimeFormatter g = DateTimeFormat.forStyle("MM"); 212 assertSame(g, f); 213 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0); 214 String expect = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, UK).format(dt.toDate()); 215 assertEquals(expect, f.print(dt)); 216 expect = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, US).format(dt.toDate()); 217 assertEquals(expect, f.withLocale(US).print(dt)); 218 expect = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, FRANCE).format(dt.toDate()); 219 assertEquals(expect, f.withLocale(FRANCE).print(dt)); 220 } 221 222 public void testForStyle_longDate() throws Exception { 224 DateTimeFormatter f = DateTimeFormat.longDate(); 225 DateTimeFormatter g = DateTimeFormat.forStyle("L-"); 226 assertSame(g, f); 227 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0); 228 String expect = DateFormat.getDateInstance(DateFormat.LONG, UK).format(dt.toDate()); 229 assertEquals(expect, f.print(dt)); 230 expect = DateFormat.getDateInstance(DateFormat.LONG, US).format(dt.toDate()); 231 assertEquals(expect, f.withLocale(US).print(dt)); 232 expect = DateFormat.getDateInstance(DateFormat.LONG, FRANCE).format(dt.toDate()); 233 assertEquals(expect, f.withLocale(FRANCE).print(dt)); 234 } 235 236 public void testForStyle_longTime() throws Exception { 237 DateTimeFormatter f = DateTimeFormat.longTime(); 238 DateTimeFormatter g = DateTimeFormat.forStyle("-L"); 239 assertSame(g, f); 240 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0); 241 String expect = DateFormat.getTimeInstance(DateFormat.LONG, UK).format(dt.toDate()); 242 assertEquals(expect, f.print(dt)); 243 expect = DateFormat.getTimeInstance(DateFormat.LONG, US).format(dt.toDate()); 244 assertEquals(expect, f.withLocale(US).print(dt)); 245 expect = DateFormat.getTimeInstance(DateFormat.LONG, FRANCE).format(dt.toDate()); 246 assertEquals(expect, f.withLocale(FRANCE).print(dt)); 247 } 248 249 public void testForStyle_longDateTime() throws Exception { 250 DateTimeFormatter f = DateTimeFormat.longDateTime(); 251 DateTimeFormatter g = DateTimeFormat.forStyle("LL"); 252 assertSame(g, f); 253 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0); 254 String expect = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, UK).format(dt.toDate()); 255 assertEquals(expect, f.print(dt)); 256 expect = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, US).format(dt.toDate()); 257 assertEquals(expect, f.withLocale(US).print(dt)); 258 expect = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, FRANCE).format(dt.toDate()); 259 assertEquals(expect, f.withLocale(FRANCE).print(dt)); 260 } 261 262 public void testForStyle_fullDate() throws Exception { 264 DateTimeFormatter f = DateTimeFormat.fullDate(); 265 DateTimeFormatter g = DateTimeFormat.forStyle("F-"); 266 assertSame(g, f); 267 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0); 268 String expect = DateFormat.getDateInstance(DateFormat.FULL, UK).format(dt.toDate()); 269 assertEquals(expect, f.print(dt)); 270 expect = DateFormat.getDateInstance(DateFormat.FULL, US).format(dt.toDate()); 271 assertEquals(expect, f.withLocale(US).print(dt)); 272 expect = DateFormat.getDateInstance(DateFormat.FULL, FRANCE).format(dt.toDate()); 273 assertEquals(expect, f.withLocale(FRANCE).print(dt)); 274 } 275 276 public void testForStyle_fullTime() throws Exception { 277 DateTimeFormatter f = DateTimeFormat.fullTime(); 278 DateTimeFormatter g = DateTimeFormat.forStyle("-F"); 279 assertSame(g, f); 280 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0); 281 String expect = DateFormat.getTimeInstance(DateFormat.FULL, UK).format(dt.toDate()); 282 assertEquals(expect, f.print(dt)); 283 expect = DateFormat.getTimeInstance(DateFormat.FULL, US).format(dt.toDate()); 284 assertEquals(expect, f.withLocale(US).print(dt)); 285 expect = DateFormat.getTimeInstance(DateFormat.FULL, FRANCE).format(dt.toDate()); 286 assertEquals(expect, f.withLocale(FRANCE).print(dt)); 287 } 288 289 public void testForStyle_fullDateTime() throws Exception { 290 DateTimeFormatter f = DateTimeFormat.fullDateTime(); 291 DateTimeFormatter g = DateTimeFormat.forStyle("FF"); 292 assertSame(g, f); 293 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0); 294 String expect = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, UK).format(dt.toDate()); 295 assertEquals(expect, f.print(dt)); 296 expect = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, US).format(dt.toDate()); 297 assertEquals(expect, f.withLocale(US).print(dt)); 298 expect = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, FRANCE).format(dt.toDate()); 299 assertEquals(expect, f.withLocale(FRANCE).print(dt)); 300 } 301 302 public void testForStyle_shortMediumDateTime() throws Exception { 304 DateTimeFormatter f = DateTimeFormat.forStyle("SM"); 305 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0); 306 String expect = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, UK).format(dt.toDate()); 307 assertEquals(expect, f.print(dt)); 308 expect = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, US).format(dt.toDate()); 309 assertEquals(expect, f.withLocale(US).print(dt)); 310 expect = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, FRANCE).format(dt.toDate()); 311 assertEquals(expect, f.withLocale(FRANCE).print(dt)); 312 } 313 314 public void testForStyle_shortLongDateTime() throws Exception { 315 DateTimeFormatter f = DateTimeFormat.forStyle("SL"); 316 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0); 317 String expect = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG, UK).format(dt.toDate()); 318 assertEquals(expect, f.print(dt)); 319 expect = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG, US).format(dt.toDate()); 320 assertEquals(expect, f.withLocale(US).print(dt)); 321 expect = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG, FRANCE).format(dt.toDate()); 322 assertEquals(expect, f.withLocale(FRANCE).print(dt)); 323 } 324 325 public void testForStyle_shortFullDateTime() throws Exception { 326 DateTimeFormatter f = DateTimeFormat.forStyle("SF"); 327 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0); 328 String expect = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.FULL, UK).format(dt.toDate()); 329 assertEquals(expect, f.print(dt)); 330 expect = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.FULL, US).format(dt.toDate()); 331 assertEquals(expect, f.withLocale(US).print(dt)); 332 expect = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.FULL, FRANCE).format(dt.toDate()); 333 assertEquals(expect, f.withLocale(FRANCE).print(dt)); 334 } 335 336 public void testForStyle_mediumShortDateTime() throws Exception { 338 DateTimeFormatter f = DateTimeFormat.forStyle("MS"); 339 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0); 340 String expect = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, UK).format(dt.toDate()); 341 assertEquals(expect, f.print(dt)); 342 expect = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, US).format(dt.toDate()); 343 assertEquals(expect, f.withLocale(US).print(dt)); 344 expect = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, FRANCE).format(dt.toDate()); 345 assertEquals(expect, f.withLocale(FRANCE).print(dt)); 346 } 347 348 public void testForStyle_mediumLongDateTime() throws Exception { 349 DateTimeFormatter f = DateTimeFormat.forStyle("ML"); 350 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0); 351 String expect = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG, UK).format(dt.toDate()); 352 assertEquals(expect, f.print(dt)); 353 expect = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG, US).format(dt.toDate()); 354 assertEquals(expect, f.withLocale(US).print(dt)); 355 expect = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG, FRANCE).format(dt.toDate()); 356 assertEquals(expect, f.withLocale(FRANCE).print(dt)); 357 } 358 359 public void testForStyle_mediumFullDateTime() throws Exception { 360 DateTimeFormatter f = DateTimeFormat.forStyle("MF"); 361 DateTime dt = new DateTime(2004, 6, 9, 10, 20, 30, 0); 362 String expect = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.FULL, UK).format(dt.toDate()); 363 assertEquals(expect, f.print(dt)); 364 expect = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.FULL, US).format(dt.toDate()); 365 assertEquals(expect, f.withLocale(US).print(dt)); 366 expect = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.FULL, FRANCE).format(dt.toDate()); 367 assertEquals(expect, f.withLocale(FRANCE).print(dt)); 368 } 369 370 } 371 | Popular Tags |