1 16 package org.apache.commons.lang.time; 17 18 import java.lang.reflect.Constructor ; 19 import java.lang.reflect.Modifier ; 20 import java.text.DateFormat ; 21 import java.text.ParseException ; 22 import java.text.SimpleDateFormat ; 23 import java.util.Calendar ; 24 import java.util.Date ; 25 import java.util.GregorianCalendar ; 26 import java.util.Iterator ; 27 import java.util.Locale ; 28 import java.util.NoSuchElementException ; 29 import java.util.TimeZone ; 30 31 import junit.framework.AssertionFailedError; 32 import junit.framework.Test; 33 import junit.framework.TestCase; 34 import junit.framework.TestSuite; 35 import junit.textui.TestRunner; 36 37 import org.apache.commons.lang.SystemUtils; 38 39 45 public class DateUtilsTest extends TestCase { 46 DateFormat dateParser = null; 47 DateFormat dateTimeParser = null; 48 DateFormat timeZoneDateParser = null; 49 Date dateAmPm1 = null; 50 Date dateAmPm2 = null; 51 Date dateAmPm3 = null; 52 Date dateAmPm4 = null; 53 Date date0 = null; 54 Date date1 = null; 55 Date date2 = null; 56 Date date3 = null; 57 Date date4 = null; 58 Date date5 = null; 59 Date date6 = null; 60 Date date7 = null; 61 Date date8 = null; 62 Calendar calAmPm1 = null; 63 Calendar calAmPm2 = null; 64 Calendar calAmPm3 = null; 65 Calendar calAmPm4 = null; 66 Calendar cal1 = null; 67 Calendar cal2 = null; 68 Calendar cal3 = null; 69 Calendar cal4 = null; 70 Calendar cal5 = null; 71 Calendar cal6 = null; 72 Calendar cal7 = null; 73 Calendar cal8 = null; 74 TimeZone zone = null; 75 TimeZone defaultZone = null; 76 77 public DateUtilsTest(String name) { 78 super(name); 79 } 80 81 public static void main(String [] args) { 82 TestRunner.run(suite()); 83 } 84 85 public static Test suite() { 86 TestSuite suite = new TestSuite(DateUtilsTest.class); 87 suite.setName("DateUtils Tests"); 88 return suite; 89 } 90 91 protected void setUp() throws Exception { 92 super.setUp(); 93 94 dateParser = new SimpleDateFormat ("MMM dd, yyyy", Locale.ENGLISH); 95 dateTimeParser = new SimpleDateFormat ("MMM dd, yyyy H:mm:ss.SSS", Locale.ENGLISH); 96 97 dateAmPm1 = dateTimeParser.parse("February 3, 2002 01:10:00.000"); 98 dateAmPm2 = dateTimeParser.parse("February 3, 2002 11:10:00.000"); 99 dateAmPm3 = dateTimeParser.parse("February 3, 2002 13:10:00.000"); 100 dateAmPm4 = dateTimeParser.parse("February 3, 2002 19:10:00.000"); 101 date0 = dateTimeParser.parse("February 3, 2002 12:34:56.789"); 102 date1 = dateTimeParser.parse("February 12, 2002 12:34:56.789"); 103 date2 = dateTimeParser.parse("November 18, 2001 1:23:11.321"); 104 defaultZone = TimeZone.getDefault(); 105 zone = TimeZone.getTimeZone("MET"); 106 TimeZone.setDefault(zone); 107 dateTimeParser.setTimeZone(zone); 108 date3 = dateTimeParser.parse("March 30, 2003 05:30:45.000"); 109 date4 = dateTimeParser.parse("March 30, 2003 01:10:00.000"); 110 date5 = dateTimeParser.parse("March 30, 2003 01:40:00.000"); 111 date6 = dateTimeParser.parse("March 30, 2003 02:10:00.000"); 112 date7 = dateTimeParser.parse("March 30, 2003 02:40:00.000"); 113 date8 = dateTimeParser.parse("October 26, 2003 05:30:45.000"); 114 dateTimeParser.setTimeZone(defaultZone); 115 TimeZone.setDefault(defaultZone); 116 calAmPm1 = Calendar.getInstance(); 117 calAmPm1.setTime(dateAmPm1); 118 calAmPm2 = Calendar.getInstance(); 119 calAmPm2.setTime(dateAmPm2); 120 calAmPm3 = Calendar.getInstance(); 121 calAmPm3.setTime(dateAmPm3); 122 calAmPm4 = Calendar.getInstance(); 123 calAmPm4.setTime(dateAmPm4); 124 cal1 = Calendar.getInstance(); 125 cal1.setTime(date1); 126 cal2 = Calendar.getInstance(); 127 cal2.setTime(date2); 128 TimeZone.setDefault(zone); 129 cal3 = Calendar.getInstance(); 130 cal3.setTime(date3); 131 cal4 = Calendar.getInstance(); 132 cal4.setTime(date4); 133 cal5 = Calendar.getInstance(); 134 cal5.setTime(date5); 135 cal6 = Calendar.getInstance(); 136 cal6.setTime(date6); 137 cal7 = Calendar.getInstance(); 138 cal7.setTime(date7); 139 cal8 = Calendar.getInstance(); 140 cal8.setTime(date8); 141 TimeZone.setDefault(defaultZone); 142 } 143 144 protected void tearDown() throws Exception { 145 super.tearDown(); 146 } 147 148 public void testConstructor() { 150 assertNotNull(new DateUtils()); 151 Constructor [] cons = DateUtils.class.getDeclaredConstructors(); 152 assertEquals(1, cons.length); 153 assertEquals(true, Modifier.isPublic(cons[0].getModifiers())); 154 assertEquals(true, Modifier.isPublic(DateUtils.class.getModifiers())); 155 assertEquals(false, Modifier.isFinal(DateUtils.class.getModifiers())); 156 } 157 158 public void testIsSameDay_Date() { 160 Date date1 = new GregorianCalendar (2004, 6, 9, 13, 45).getTime(); 161 Date date2 = new GregorianCalendar (2004, 6, 9, 13, 45).getTime(); 162 assertEquals(true, DateUtils.isSameDay(date1, date2)); 163 date2 = new GregorianCalendar (2004, 6, 10, 13, 45).getTime(); 164 assertEquals(false, DateUtils.isSameDay(date1, date2)); 165 date1 = new GregorianCalendar (2004, 6, 10, 13, 45).getTime(); 166 assertEquals(true, DateUtils.isSameDay(date1, date2)); 167 date2 = new GregorianCalendar (2005, 6, 10, 13, 45).getTime(); 168 assertEquals(false, DateUtils.isSameDay(date1, date2)); 169 try { 170 DateUtils.isSameDay((Date ) null, (Date ) null); 171 fail(); 172 } catch (IllegalArgumentException ex) {} 173 } 174 175 public void testIsSameDay_Cal() { 177 GregorianCalendar cal1 = new GregorianCalendar (2004, 6, 9, 13, 45); 178 GregorianCalendar cal2 = new GregorianCalendar (2004, 6, 9, 13, 45); 179 assertEquals(true, DateUtils.isSameDay(cal1, cal2)); 180 cal2.add(Calendar.DAY_OF_YEAR, 1); 181 assertEquals(false, DateUtils.isSameDay(cal1, cal2)); 182 cal1.add(Calendar.DAY_OF_YEAR, 1); 183 assertEquals(true, DateUtils.isSameDay(cal1, cal2)); 184 cal2.add(Calendar.YEAR, 1); 185 assertEquals(false, DateUtils.isSameDay(cal1, cal2)); 186 try { 187 DateUtils.isSameDay((Calendar ) null, (Calendar ) null); 188 fail(); 189 } catch (IllegalArgumentException ex) {} 190 } 191 192 public void testIsSameInstant_Date() { 194 Date date1 = new GregorianCalendar (2004, 6, 9, 13, 45).getTime(); 195 Date date2 = new GregorianCalendar (2004, 6, 9, 13, 45).getTime(); 196 assertEquals(true, DateUtils.isSameInstant(date1, date2)); 197 date2 = new GregorianCalendar (2004, 6, 10, 13, 45).getTime(); 198 assertEquals(false, DateUtils.isSameInstant(date1, date2)); 199 date1 = new GregorianCalendar (2004, 6, 10, 13, 45).getTime(); 200 assertEquals(true, DateUtils.isSameInstant(date1, date2)); 201 date2 = new GregorianCalendar (2005, 6, 10, 13, 45).getTime(); 202 assertEquals(false, DateUtils.isSameInstant(date1, date2)); 203 try { 204 DateUtils.isSameInstant((Date ) null, (Date ) null); 205 fail(); 206 } catch (IllegalArgumentException ex) {} 207 } 208 209 public void testIsSameInstant_Cal() { 211 GregorianCalendar cal1 = new GregorianCalendar (TimeZone.getTimeZone("GMT+1")); 212 GregorianCalendar cal2 = new GregorianCalendar (TimeZone.getTimeZone("GMT-1")); 213 cal1.set(2004, 6, 9, 13, 45, 0); 214 cal1.set(Calendar.MILLISECOND, 0); 215 cal2.set(2004, 6, 9, 13, 45, 0); 216 cal2.set(Calendar.MILLISECOND, 0); 217 assertEquals(false, DateUtils.isSameInstant(cal1, cal2)); 218 219 cal2.set(2004, 6, 9, 11, 45, 0); 220 assertEquals(true, DateUtils.isSameInstant(cal1, cal2)); 221 try { 222 DateUtils.isSameInstant((Calendar ) null, (Calendar ) null); 223 fail(); 224 } catch (IllegalArgumentException ex) {} 225 } 226 227 public void testIsSameLocalTime_Cal() { 229 GregorianCalendar cal1 = new GregorianCalendar (TimeZone.getTimeZone("GMT+1")); 230 GregorianCalendar cal2 = new GregorianCalendar (TimeZone.getTimeZone("GMT-1")); 231 cal1.set(2004, 6, 9, 13, 45, 0); 232 cal1.set(Calendar.MILLISECOND, 0); 233 cal2.set(2004, 6, 9, 13, 45, 0); 234 cal2.set(Calendar.MILLISECOND, 0); 235 assertEquals(true, DateUtils.isSameLocalTime(cal1, cal2)); 236 237 cal2.set(2004, 6, 9, 11, 45, 0); 238 assertEquals(false, DateUtils.isSameLocalTime(cal1, cal2)); 239 try { 240 DateUtils.isSameLocalTime((Calendar ) null, (Calendar ) null); 241 fail(); 242 } catch (IllegalArgumentException ex) {} 243 } 244 245 public void testParseDate() throws Exception { 247 GregorianCalendar cal = new GregorianCalendar (1972, 11, 3); 248 String dateStr = "1972-12-03"; 249 String [] parsers = new String [] {"yyyy'-'DDD", "yyyy'-'MM'-'dd", "yyyyMMdd"}; 250 Date date = DateUtils.parseDate(dateStr, parsers); 251 assertEquals(cal.getTime(), date); 252 253 dateStr = "1972-338"; 254 date = DateUtils.parseDate(dateStr, parsers); 255 assertEquals(cal.getTime(), date); 256 257 dateStr = "19721203"; 258 date = DateUtils.parseDate(dateStr, parsers); 259 assertEquals(cal.getTime(), date); 260 261 try { 262 DateUtils.parseDate("PURPLE", parsers); 263 fail(); 264 } catch (ParseException ex) {} 265 try { 266 DateUtils.parseDate("197212AB", parsers); 267 fail(); 268 } catch (ParseException ex) {} 269 try { 270 DateUtils.parseDate(null, parsers); 271 fail(); 272 } catch (IllegalArgumentException ex) {} 273 try { 274 DateUtils.parseDate(dateStr, null); 275 fail(); 276 } catch (IllegalArgumentException ex) {} 277 } 278 279 283 public void testRound() throws Exception { 284 assertEquals("round year-1 failed", 286 dateParser.parse("January 1, 2002"), 287 DateUtils.round(date1, Calendar.YEAR)); 288 assertEquals("round year-2 failed", 289 dateParser.parse("January 1, 2002"), 290 DateUtils.round(date2, Calendar.YEAR)); 291 assertEquals("round month-1 failed", 292 dateParser.parse("February 1, 2002"), 293 DateUtils.round(date1, Calendar.MONTH)); 294 assertEquals("round month-2 failed", 295 dateParser.parse("December 1, 2001"), 296 DateUtils.round(date2, Calendar.MONTH)); 297 assertEquals("round semimonth-0 failed", 298 dateParser.parse("February 1, 2002"), 299 DateUtils.round(date0, DateUtils.SEMI_MONTH)); 300 assertEquals("round semimonth-1 failed", 301 dateParser.parse("February 16, 2002"), 302 DateUtils.round(date1, DateUtils.SEMI_MONTH)); 303 assertEquals("round semimonth-2 failed", 304 dateParser.parse("November 16, 2001"), 305 DateUtils.round(date2, DateUtils.SEMI_MONTH)); 306 assertEquals("round date-1 failed", 307 dateParser.parse("February 13, 2002"), 308 DateUtils.round(date1, Calendar.DATE)); 309 assertEquals("round date-2 failed", 310 dateParser.parse("November 18, 2001"), 311 DateUtils.round(date2, Calendar.DATE)); 312 assertEquals("round hour-1 failed", 313 dateTimeParser.parse("February 12, 2002 13:00:00.000"), 314 DateUtils.round(date1, Calendar.HOUR)); 315 assertEquals("round hour-2 failed", 316 dateTimeParser.parse("November 18, 2001 1:00:00.000"), 317 DateUtils.round(date2, Calendar.HOUR)); 318 assertEquals("round minute-1 failed", 319 dateTimeParser.parse("February 12, 2002 12:35:00.000"), 320 DateUtils.round(date1, Calendar.MINUTE)); 321 assertEquals("round minute-2 failed", 322 dateTimeParser.parse("November 18, 2001 1:23:00.000"), 323 DateUtils.round(date2, Calendar.MINUTE)); 324 assertEquals("round second-1 failed", 325 dateTimeParser.parse("February 12, 2002 12:34:57.000"), 326 DateUtils.round(date1, Calendar.SECOND)); 327 assertEquals("round second-2 failed", 328 dateTimeParser.parse("November 18, 2001 1:23:11.000"), 329 DateUtils.round(date2, Calendar.SECOND)); 330 assertEquals("truncate ampm-1 failed", 331 dateTimeParser.parse("February 3, 2002 00:00:00.000"), 332 DateUtils.round(dateAmPm1, Calendar.AM_PM)); 333 assertEquals("truncate ampm-2 failed", 334 dateTimeParser.parse("February 4, 2002 00:00:00.000"), 335 DateUtils.round(dateAmPm2, Calendar.AM_PM)); 336 assertEquals("truncate ampm-3 failed", 337 dateTimeParser.parse("February 3, 2002 12:00:00.000"), 338 DateUtils.round(dateAmPm3, Calendar.AM_PM)); 339 assertEquals("truncate ampm-4 failed", 340 dateTimeParser.parse("February 4, 2002 12:00:00.000"), 341 DateUtils.round(dateAmPm4, Calendar.AM_PM)); 342 343 assertEquals("round year-1 failed", 345 dateParser.parse("January 1, 2002"), 346 DateUtils.round((Object ) date1, Calendar.YEAR)); 347 assertEquals("round year-2 failed", 348 dateParser.parse("January 1, 2002"), 349 DateUtils.round((Object ) date2, Calendar.YEAR)); 350 assertEquals("round month-1 failed", 351 dateParser.parse("February 1, 2002"), 352 DateUtils.round((Object ) date1, Calendar.MONTH)); 353 assertEquals("round month-2 failed", 354 dateParser.parse("December 1, 2001"), 355 DateUtils.round((Object ) date2, Calendar.MONTH)); 356 assertEquals("round semimonth-1 failed", 357 dateParser.parse("February 16, 2002"), 358 DateUtils.round((Object ) date1, DateUtils.SEMI_MONTH)); 359 assertEquals("round semimonth-2 failed", 360 dateParser.parse("November 16, 2001"), 361 DateUtils.round((Object ) date2, DateUtils.SEMI_MONTH)); 362 assertEquals("round date-1 failed", 363 dateParser.parse("February 13, 2002"), 364 DateUtils.round((Object ) date1, Calendar.DATE)); 365 assertEquals("round date-2 failed", 366 dateParser.parse("November 18, 2001"), 367 DateUtils.round((Object ) date2, Calendar.DATE)); 368 assertEquals("round hour-1 failed", 369 dateTimeParser.parse("February 12, 2002 13:00:00.000"), 370 DateUtils.round((Object ) date1, Calendar.HOUR)); 371 assertEquals("round hour-2 failed", 372 dateTimeParser.parse("November 18, 2001 1:00:00.000"), 373 DateUtils.round((Object ) date2, Calendar.HOUR)); 374 assertEquals("round minute-1 failed", 375 dateTimeParser.parse("February 12, 2002 12:35:00.000"), 376 DateUtils.round((Object ) date1, Calendar.MINUTE)); 377 assertEquals("round minute-2 failed", 378 dateTimeParser.parse("November 18, 2001 1:23:00.000"), 379 DateUtils.round((Object ) date2, Calendar.MINUTE)); 380 assertEquals("round second-1 failed", 381 dateTimeParser.parse("February 12, 2002 12:34:57.000"), 382 DateUtils.round((Object ) date1, Calendar.SECOND)); 383 assertEquals("round second-2 failed", 384 dateTimeParser.parse("November 18, 2001 1:23:11.000"), 385 DateUtils.round((Object ) date2, Calendar.SECOND)); 386 assertEquals("round calendar second-1 failed", 387 dateTimeParser.parse("February 12, 2002 12:34:57.000"), 388 DateUtils.round((Object ) cal1, Calendar.SECOND)); 389 assertEquals("round calendar second-2 failed", 390 dateTimeParser.parse("November 18, 2001 1:23:11.000"), 391 DateUtils.round((Object ) cal2, Calendar.SECOND)); 392 assertEquals("truncate ampm-1 failed", 393 dateTimeParser.parse("February 3, 2002 00:00:00.000"), 394 DateUtils.round((Object ) dateAmPm1, Calendar.AM_PM)); 395 assertEquals("truncate ampm-2 failed", 396 dateTimeParser.parse("February 4, 2002 00:00:00.000"), 397 DateUtils.round((Object ) dateAmPm2, Calendar.AM_PM)); 398 assertEquals("truncate ampm-3 failed", 399 dateTimeParser.parse("February 3, 2002 12:00:00.000"), 400 DateUtils.round((Object ) dateAmPm3, Calendar.AM_PM)); 401 assertEquals("truncate ampm-4 failed", 402 dateTimeParser.parse("February 4, 2002 12:00:00.000"), 403 DateUtils.round((Object ) dateAmPm4, Calendar.AM_PM)); 404 405 try { 406 DateUtils.round((Date ) null, Calendar.SECOND); 407 fail(); 408 } catch (IllegalArgumentException ex) {} 409 try { 410 DateUtils.round((Calendar ) null, Calendar.SECOND); 411 fail(); 412 } catch (IllegalArgumentException ex) {} 413 try { 414 DateUtils.round((Object ) null, Calendar.SECOND); 415 fail(); 416 } catch (IllegalArgumentException ex) {} 417 try { 418 DateUtils.round("", Calendar.SECOND); 419 fail(); 420 } catch (ClassCastException ex) {} 421 try { 422 DateUtils.round(date1, -9999); 423 fail(); 424 } catch(IllegalArgumentException ex) {} 425 426 assertEquals("truncate ampm-1 failed", 427 dateTimeParser.parse("February 3, 2002 00:00:00.000"), 428 DateUtils.round((Object ) calAmPm1, Calendar.AM_PM)); 429 assertEquals("truncate ampm-2 failed", 430 dateTimeParser.parse("February 4, 2002 00:00:00.000"), 431 DateUtils.round((Object ) calAmPm2, Calendar.AM_PM)); 432 assertEquals("truncate ampm-3 failed", 433 dateTimeParser.parse("February 3, 2002 12:00:00.000"), 434 DateUtils.round((Object ) calAmPm3, Calendar.AM_PM)); 435 assertEquals("truncate ampm-4 failed", 436 dateTimeParser.parse("February 4, 2002 12:00:00.000"), 437 DateUtils.round((Object ) calAmPm4, Calendar.AM_PM)); 438 439 TimeZone.setDefault(zone); 442 dateTimeParser.setTimeZone(zone); 443 assertEquals("round MET date across DST change-over", 444 dateTimeParser.parse("March 30, 2003 00:00:00.000"), 445 DateUtils.round(date4, Calendar.DATE)); 446 assertEquals("round MET date across DST change-over", 447 dateTimeParser.parse("March 30, 2003 00:00:00.000"), 448 DateUtils.round((Object ) cal4, Calendar.DATE)); 449 assertEquals("round MET date across DST change-over", 450 dateTimeParser.parse("March 30, 2003 00:00:00.000"), 451 DateUtils.round(date5, Calendar.DATE)); 452 assertEquals("round MET date across DST change-over", 453 dateTimeParser.parse("March 30, 2003 00:00:00.000"), 454 DateUtils.round((Object ) cal5, Calendar.DATE)); 455 assertEquals("round MET date across DST change-over", 456 dateTimeParser.parse("March 30, 2003 00:00:00.000"), 457 DateUtils.round(date6, Calendar.DATE)); 458 assertEquals("round MET date across DST change-over", 459 dateTimeParser.parse("March 30, 2003 00:00:00.000"), 460 DateUtils.round((Object ) cal6, Calendar.DATE)); 461 assertEquals("round MET date across DST change-over", 462 dateTimeParser.parse("March 30, 2003 00:00:00.000"), 463 DateUtils.round(date7, Calendar.DATE)); 464 assertEquals("round MET date across DST change-over", 465 dateTimeParser.parse("March 30, 2003 00:00:00.000"), 466 DateUtils.round((Object ) cal7, Calendar.DATE)); 467 468 assertEquals("round MET date across DST change-over", 469 dateTimeParser.parse("March 30, 2003 01:00:00.000"), 470 DateUtils.round(date4, Calendar.HOUR_OF_DAY)); 471 assertEquals("round MET date across DST change-over", 472 dateTimeParser.parse("March 30, 2003 01:00:00.000"), 473 DateUtils.round((Object ) cal4, Calendar.HOUR_OF_DAY)); 474 if (SystemUtils.isJavaVersionAtLeast(1.4f)) { 475 assertEquals("round MET date across DST change-over", 476 dateTimeParser.parse("March 30, 2003 03:00:00.000"), 477 DateUtils.round(date5, Calendar.HOUR_OF_DAY)); 478 assertEquals("round MET date across DST change-over", 479 dateTimeParser.parse("March 30, 2003 03:00:00.000"), 480 DateUtils.round((Object ) cal5, Calendar.HOUR_OF_DAY)); 481 assertEquals("round MET date across DST change-over", 482 dateTimeParser.parse("March 30, 2003 03:00:00.000"), 483 DateUtils.round(date6, Calendar.HOUR_OF_DAY)); 484 assertEquals("round MET date across DST change-over", 485 dateTimeParser.parse("March 30, 2003 03:00:00.000"), 486 DateUtils.round((Object ) cal6, Calendar.HOUR_OF_DAY)); 487 assertEquals("round MET date across DST change-over", 488 dateTimeParser.parse("March 30, 2003 04:00:00.000"), 489 DateUtils.round(date7, Calendar.HOUR_OF_DAY)); 490 assertEquals("round MET date across DST change-over", 491 dateTimeParser.parse("March 30, 2003 04:00:00.000"), 492 DateUtils.round((Object ) cal7, Calendar.HOUR_OF_DAY)); 493 } else { 494 this.warn("Some date rounding tests not run since the current version is " + SystemUtils.JAVA_VERSION); 495 } 496 TimeZone.setDefault(defaultZone); 497 dateTimeParser.setTimeZone(defaultZone); 498 } 499 500 503 public void testTruncate() throws Exception { 504 assertEquals("truncate year-1 failed", 506 dateParser.parse("January 1, 2002"), 507 DateUtils.truncate(date1, Calendar.YEAR)); 508 assertEquals("truncate year-2 failed", 509 dateParser.parse("January 1, 2001"), 510 DateUtils.truncate(date2, Calendar.YEAR)); 511 assertEquals("truncate month-1 failed", 512 dateParser.parse("February 1, 2002"), 513 DateUtils.truncate(date1, Calendar.MONTH)); 514 assertEquals("truncate month-2 failed", 515 dateParser.parse("November 1, 2001"), 516 DateUtils.truncate(date2, Calendar.MONTH)); 517 assertEquals("truncate semimonth-1 failed", 518 dateParser.parse("February 1, 2002"), 519 DateUtils.truncate(date1, DateUtils.SEMI_MONTH)); 520 assertEquals("truncate semimonth-2 failed", 521 dateParser.parse("November 16, 2001"), 522 DateUtils.truncate(date2, DateUtils.SEMI_MONTH)); 523 assertEquals("truncate date-1 failed", 524 dateParser.parse("February 12, 2002"), 525 DateUtils.truncate(date1, Calendar.DATE)); 526 assertEquals("truncate date-2 failed", 527 dateParser.parse("November 18, 2001"), 528 DateUtils.truncate(date2, Calendar.DATE)); 529 assertEquals("truncate hour-1 failed", 530 dateTimeParser.parse("February 12, 2002 12:00:00.000"), 531 DateUtils.truncate(date1, Calendar.HOUR)); 532 assertEquals("truncate hour-2 failed", 533 dateTimeParser.parse("November 18, 2001 1:00:00.000"), 534 DateUtils.truncate(date2, Calendar.HOUR)); 535 assertEquals("truncate minute-1 failed", 536 dateTimeParser.parse("February 12, 2002 12:34:00.000"), 537 DateUtils.truncate(date1, Calendar.MINUTE)); 538 assertEquals("truncate minute-2 failed", 539 dateTimeParser.parse("November 18, 2001 1:23:00.000"), 540 DateUtils.truncate(date2, Calendar.MINUTE)); 541 assertEquals("truncate second-1 failed", 542 dateTimeParser.parse("February 12, 2002 12:34:56.000"), 543 DateUtils.truncate(date1, Calendar.SECOND)); 544 assertEquals("truncate second-2 failed", 545 dateTimeParser.parse("November 18, 2001 1:23:11.000"), 546 DateUtils.truncate(date2, Calendar.SECOND)); 547 assertEquals("truncate ampm-1 failed", 548 dateTimeParser.parse("February 3, 2002 00:00:00.000"), 549 DateUtils.truncate(dateAmPm1, Calendar.AM_PM)); 550 assertEquals("truncate ampm-2 failed", 551 dateTimeParser.parse("February 3, 2002 00:00:00.000"), 552 DateUtils.truncate(dateAmPm2, Calendar.AM_PM)); 553 assertEquals("truncate ampm-3 failed", 554 dateTimeParser.parse("February 3, 2002 12:00:00.000"), 555 DateUtils.truncate(dateAmPm3, Calendar.AM_PM)); 556 assertEquals("truncate ampm-4 failed", 557 dateTimeParser.parse("February 3, 2002 12:00:00.000"), 558 DateUtils.truncate(dateAmPm4, Calendar.AM_PM)); 559 560 assertEquals("truncate year-1 failed", 562 dateParser.parse("January 1, 2002"), 563 DateUtils.truncate((Object ) date1, Calendar.YEAR)); 564 assertEquals("truncate year-2 failed", 565 dateParser.parse("January 1, 2001"), 566 DateUtils.truncate((Object ) date2, Calendar.YEAR)); 567 assertEquals("truncate month-1 failed", 568 dateParser.parse("February 1, 2002"), 569 DateUtils.truncate((Object ) date1, Calendar.MONTH)); 570 assertEquals("truncate month-2 failed", 571 dateParser.parse("November 1, 2001"), 572 DateUtils.truncate((Object ) date2, Calendar.MONTH)); 573 assertEquals("truncate semimonth-1 failed", 574 dateParser.parse("February 1, 2002"), 575 DateUtils.truncate((Object ) date1, DateUtils.SEMI_MONTH)); 576 assertEquals("truncate semimonth-2 failed", 577 dateParser.parse("November 16, 2001"), 578 DateUtils.truncate((Object ) date2, DateUtils.SEMI_MONTH)); 579 assertEquals("truncate date-1 failed", 580 dateParser.parse("February 12, 2002"), 581 DateUtils.truncate((Object ) date1, Calendar.DATE)); 582 assertEquals("truncate date-2 failed", 583 dateParser.parse("November 18, 2001"), 584 DateUtils.truncate((Object ) date2, Calendar.DATE)); 585 assertEquals("truncate hour-1 failed", 586 dateTimeParser.parse("February 12, 2002 12:00:00.000"), 587 DateUtils.truncate((Object ) date1, Calendar.HOUR)); 588 assertEquals("truncate hour-2 failed", 589 dateTimeParser.parse("November 18, 2001 1:00:00.000"), 590 DateUtils.truncate((Object ) date2, Calendar.HOUR)); 591 assertEquals("truncate minute-1 failed", 592 dateTimeParser.parse("February 12, 2002 12:34:00.000"), 593 DateUtils.truncate((Object ) date1, Calendar.MINUTE)); 594 assertEquals("truncate minute-2 failed", 595 dateTimeParser.parse("November 18, 2001 1:23:00.000"), 596 DateUtils.truncate((Object ) date2, Calendar.MINUTE)); 597 assertEquals("truncate second-1 failed", 598 dateTimeParser.parse("February 12, 2002 12:34:56.000"), 599 DateUtils.truncate((Object ) date1, Calendar.SECOND)); 600 assertEquals("truncate second-2 failed", 601 dateTimeParser.parse("November 18, 2001 1:23:11.000"), 602 DateUtils.truncate((Object ) date2, Calendar.SECOND)); 603 assertEquals("truncate ampm-1 failed", 604 dateTimeParser.parse("February 3, 2002 00:00:00.000"), 605 DateUtils.truncate((Object ) dateAmPm1, Calendar.AM_PM)); 606 assertEquals("truncate ampm-2 failed", 607 dateTimeParser.parse("February 3, 2002 00:00:00.000"), 608 DateUtils.truncate((Object ) dateAmPm2, Calendar.AM_PM)); 609 assertEquals("truncate ampm-3 failed", 610 dateTimeParser.parse("February 3, 2002 12:00:00.000"), 611 DateUtils.truncate((Object ) dateAmPm3, Calendar.AM_PM)); 612 assertEquals("truncate ampm-4 failed", 613 dateTimeParser.parse("February 3, 2002 12:00:00.000"), 614 DateUtils.truncate((Object ) dateAmPm4, Calendar.AM_PM)); 615 616 assertEquals("truncate calendar second-1 failed", 617 dateTimeParser.parse("February 12, 2002 12:34:56.000"), 618 DateUtils.truncate((Object ) cal1, Calendar.SECOND)); 619 assertEquals("truncate calendar second-2 failed", 620 dateTimeParser.parse("November 18, 2001 1:23:11.000"), 621 DateUtils.truncate((Object ) cal2, Calendar.SECOND)); 622 623 assertEquals("truncate ampm-1 failed", 624 dateTimeParser.parse("February 3, 2002 00:00:00.000"), 625 DateUtils.truncate((Object ) calAmPm1, Calendar.AM_PM)); 626 assertEquals("truncate ampm-2 failed", 627 dateTimeParser.parse("February 3, 2002 00:00:00.000"), 628 DateUtils.truncate((Object ) calAmPm2, Calendar.AM_PM)); 629 assertEquals("truncate ampm-3 failed", 630 dateTimeParser.parse("February 3, 2002 12:00:00.000"), 631 DateUtils.truncate((Object ) calAmPm3, Calendar.AM_PM)); 632 assertEquals("truncate ampm-4 failed", 633 dateTimeParser.parse("February 3, 2002 12:00:00.000"), 634 DateUtils.truncate((Object ) calAmPm4, Calendar.AM_PM)); 635 636 try { 637 DateUtils.truncate((Date ) null, Calendar.SECOND); 638 fail(); 639 } catch (IllegalArgumentException ex) {} 640 try { 641 DateUtils.truncate((Calendar ) null, Calendar.SECOND); 642 fail(); 643 } catch (IllegalArgumentException ex) {} 644 try { 645 DateUtils.truncate((Object ) null, Calendar.SECOND); 646 fail(); 647 } catch (IllegalArgumentException ex) {} 648 try { 649 DateUtils.truncate("", Calendar.SECOND); 650 fail(); 651 } catch (ClassCastException ex) {} 652 653 TimeZone.setDefault(zone); 656 dateTimeParser.setTimeZone(zone); 657 assertEquals("truncate MET date across DST change-over", 658 dateTimeParser.parse("March 30, 2003 00:00:00.000"), 659 DateUtils.truncate(date3, Calendar.DATE)); 660 assertEquals("truncate MET date across DST change-over", 661 dateTimeParser.parse("March 30, 2003 00:00:00.000"), 662 DateUtils.truncate((Object ) cal3, Calendar.DATE)); 663 assertEquals("truncate MET date across DST change-over", 665 dateTimeParser.parse("October 26, 2003 00:00:00.000"), 666 DateUtils.truncate(date8, Calendar.DATE)); 667 assertEquals("truncate MET date across DST change-over", 668 dateTimeParser.parse("October 26, 2003 00:00:00.000"), 669 DateUtils.truncate((Object ) cal8, Calendar.DATE)); 670 TimeZone.setDefault(defaultZone); 671 dateTimeParser.setTimeZone(defaultZone); 672 673 Date endOfTime = new Date (Long.MAX_VALUE); GregorianCalendar endCal = new GregorianCalendar (); 676 endCal.setTime(endOfTime); 677 try { 678 DateUtils.truncate(endCal, Calendar.DATE); 679 fail(); 680 } catch (ArithmeticException ex) {} 681 endCal.set(Calendar.YEAR, 280000001); 682 try { 683 DateUtils.truncate(endCal, Calendar.DATE); 684 fail(); 685 } catch (ArithmeticException ex) {} 686 endCal.set(Calendar.YEAR, 280000000); 687 Calendar cal = DateUtils.truncate(endCal, Calendar.DATE); 688 assertEquals(0, cal.get(Calendar.HOUR)); 689 } 690 691 694 public void testIteratorEx() throws Exception { 695 try { 696 DateUtils.iterator(Calendar.getInstance(), -9999); 697 } catch (IllegalArgumentException ex) {} 698 try { 699 DateUtils.iterator((Date ) null, DateUtils.RANGE_WEEK_CENTER); 700 fail(); 701 } catch (IllegalArgumentException ex) {} 702 try { 703 DateUtils.iterator((Calendar ) null, DateUtils.RANGE_WEEK_CENTER); 704 fail(); 705 } catch (IllegalArgumentException ex) {} 706 try { 707 DateUtils.iterator((Object ) null, DateUtils.RANGE_WEEK_CENTER); 708 fail(); 709 } catch (IllegalArgumentException ex) {} 710 try { 711 DateUtils.iterator("", DateUtils.RANGE_WEEK_CENTER); 712 fail(); 713 } catch (ClassCastException ex) {} 714 } 715 716 719 public void testWeekIterator() throws Exception { 720 Calendar now = Calendar.getInstance(); 721 for (int i = 0; i< 7; i++) { 722 Calendar today = DateUtils.truncate(now, Calendar.DATE); 723 Calendar sunday = DateUtils.truncate(now, Calendar.DATE); 724 sunday.add(Calendar.DATE, 1 - sunday.get(Calendar.DAY_OF_WEEK)); 725 Calendar monday = DateUtils.truncate(now, Calendar.DATE); 726 if (monday.get(Calendar.DAY_OF_WEEK) == 1) { 727 monday.add(Calendar.DATE, -6); 729 } else { 730 monday.add(Calendar.DATE, 2 - monday.get(Calendar.DAY_OF_WEEK)); 731 } 732 Calendar centered = DateUtils.truncate(now, Calendar.DATE); 733 centered.add(Calendar.DATE, -3); 734 735 Iterator it = DateUtils.iterator(now, DateUtils.RANGE_WEEK_SUNDAY); 736 assertWeekIterator(it, sunday); 737 it = DateUtils.iterator(now, DateUtils.RANGE_WEEK_MONDAY); 738 assertWeekIterator(it, monday); 739 it = DateUtils.iterator(now, DateUtils.RANGE_WEEK_RELATIVE); 740 assertWeekIterator(it, today); 741 it = DateUtils.iterator(now, DateUtils.RANGE_WEEK_CENTER); 742 assertWeekIterator(it, centered); 743 744 it = DateUtils.iterator((Object ) now, DateUtils.RANGE_WEEK_CENTER); 745 assertWeekIterator(it, centered); 746 it = DateUtils.iterator((Object ) now.getTime(), DateUtils.RANGE_WEEK_CENTER); 747 assertWeekIterator(it, centered); 748 try { 749 it.next(); 750 fail(); 751 } catch (NoSuchElementException ex) {} 752 it = DateUtils.iterator(now, DateUtils.RANGE_WEEK_CENTER); 753 it.next(); 754 try { 755 it.remove(); 756 } catch( UnsupportedOperationException ex) {} 757 758 now.add(Calendar.DATE,1); 759 } 760 } 761 762 765 public void testMonthIterator() throws Exception { 766 Iterator it = DateUtils.iterator(date1, DateUtils.RANGE_MONTH_SUNDAY); 767 assertWeekIterator(it, 768 dateParser.parse("January 27, 2002"), 769 dateParser.parse("March 2, 2002")); 770 771 it = DateUtils.iterator(date1, DateUtils.RANGE_MONTH_MONDAY); 772 assertWeekIterator(it, 773 dateParser.parse("January 28, 2002"), 774 dateParser.parse("March 3, 2002")); 775 776 it = DateUtils.iterator(date2, DateUtils.RANGE_MONTH_SUNDAY); 777 assertWeekIterator(it, 778 dateParser.parse("October 28, 2001"), 779 dateParser.parse("December 1, 2001")); 780 781 it = DateUtils.iterator(date2, DateUtils.RANGE_MONTH_MONDAY); 782 assertWeekIterator(it, 783 dateParser.parse("October 29, 2001"), 784 dateParser.parse("December 2, 2001")); 785 } 786 787 791 private static void assertWeekIterator(Iterator it, Calendar start) { 792 Calendar end = (Calendar ) start.clone(); 793 end.add(Calendar.DATE, 6); 794 795 assertWeekIterator(it, start, end); 796 } 797 798 801 private static void assertWeekIterator(Iterator it, Date start, Date end) { 802 Calendar calStart = Calendar.getInstance(); 803 calStart.setTime(start); 804 Calendar calEnd = Calendar.getInstance(); 805 calEnd.setTime(end); 806 807 assertWeekIterator(it, calStart, calEnd); 808 } 809 810 815 private static void assertWeekIterator(Iterator it, Calendar start, Calendar end) { 816 Calendar cal = (Calendar ) it.next(); 817 assertEquals("", start, cal, 0); 818 Calendar last = null; 819 int count = 1; 820 while (it.hasNext()) { 821 assertEquals("", cal, DateUtils.truncate(cal, Calendar.DATE), 0); 823 824 last = cal; 825 cal = (Calendar ) it.next(); 826 count++; 827 828 last.add(Calendar.DATE, 1); 830 assertEquals("", last, cal, 0); 831 } 832 if (count % 7 != 0) { 833 throw new AssertionFailedError("There were " + count + " days in this iterator"); 834 } 835 assertEquals("", end, cal, 0); 836 } 837 838 842 private static void assertEquals(String message, Calendar cal1, Calendar cal2, long delta) { 843 if (Math.abs(cal1.getTime().getTime() - cal2.getTime().getTime()) > delta) { 844 throw new AssertionFailedError( 845 message + " expected " + cal1.getTime() + " but got " + cal2.getTime()); 846 } 847 } 848 849 void warn(String msg) { 850 System.err.println(msg); 851 } 852 } 853 854 | Popular Tags |