1 16 package org.joda.time; 17 18 import java.io.ByteArrayInputStream ; 19 import java.io.ByteArrayOutputStream ; 20 import java.io.ObjectInputStream ; 21 import java.io.ObjectOutputStream ; 22 import java.util.Arrays ; 23 import java.util.Date ; 24 import java.util.Locale ; 25 26 import junit.framework.TestCase; 27 import junit.framework.TestSuite; 28 29 import org.joda.time.chrono.BuddhistChronology; 30 import org.joda.time.chrono.CopticChronology; 31 import org.joda.time.chrono.GJChronology; 32 import org.joda.time.chrono.GregorianChronology; 33 import org.joda.time.chrono.ISOChronology; 34 import org.joda.time.format.DateTimeFormat; 35 import org.joda.time.format.DateTimeFormatter; 36 37 42 public class TestLocalDateTime_Basics extends TestCase { 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 private static final int OFFSET = 1; 48 private static final GJChronology GJ_UTC = GJChronology.getInstanceUTC(); 49 private static final Chronology COPTIC_PARIS = CopticChronology.getInstance(PARIS); 50 private static final Chronology COPTIC_LONDON = CopticChronology.getInstance(LONDON); 51 private static final Chronology COPTIC_TOKYO = CopticChronology.getInstance(TOKYO); 52 private static final Chronology COPTIC_UTC = CopticChronology.getInstanceUTC(); 53 private static final Chronology ISO_PARIS = ISOChronology.getInstance(PARIS); 54 private static final Chronology ISO_LONDON = ISOChronology.getInstance(LONDON); 55 private static final Chronology ISO_TOKYO = ISOChronology.getInstance(TOKYO); 56 private static final Chronology ISO_UTC = ISOChronology.getInstanceUTC(); 57 private static final Chronology GREGORIAN_UTC = GregorianChronology.getInstanceUTC(); 58 private static final Chronology BUDDHIST_PARIS = BuddhistChronology.getInstance(PARIS); 59 private static final Chronology BUDDHIST_LONDON = BuddhistChronology.getInstance(LONDON); 60 private static final Chronology BUDDHIST_TOKYO = BuddhistChronology.getInstance(TOKYO); 61 private static final Chronology BUDDHIST_UTC = BuddhistChronology.getInstanceUTC(); 62 63 73 private int MILLIS_OF_DAY_UTC = 74 (int) (10L * DateTimeConstants.MILLIS_PER_HOUR 75 + 20L * DateTimeConstants.MILLIS_PER_MINUTE 76 + 30L * DateTimeConstants.MILLIS_PER_SECOND 77 + 40L); 78 79 private long TEST_TIME_NOW_UTC = 80 (31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY + MILLIS_OF_DAY_UTC; 81 82 private DateTimeZone zone = null; 83 84 public static void main(String [] args) { 85 junit.textui.TestRunner.run(suite()); 86 } 87 88 public static TestSuite suite() { 89 return new TestSuite(TestLocalDateTime_Basics.class); 90 } 91 92 public TestLocalDateTime_Basics(String name) { 93 super(name); 94 } 95 96 protected void setUp() throws Exception { 97 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW_UTC); 98 zone = DateTimeZone.getDefault(); 99 DateTimeZone.setDefault(LONDON); 100 } 101 102 protected void tearDown() throws Exception { 103 DateTimeUtils.setCurrentMillisSystem(); 104 DateTimeZone.setDefault(zone); 105 zone = null; 106 } 107 108 public void testGet_DateTimeFieldType() { 110 LocalDateTime test = new LocalDateTime(1970, 6, 9, 10, 20, 30, 40); 111 assertEquals(1970, test.get(DateTimeFieldType.year())); 112 assertEquals(6, test.get(DateTimeFieldType.monthOfYear())); 113 assertEquals(9, test.get(DateTimeFieldType.dayOfMonth())); 114 assertEquals(2, test.get(DateTimeFieldType.dayOfWeek())); 115 assertEquals(160, test.get(DateTimeFieldType.dayOfYear())); 116 assertEquals(24, test.get(DateTimeFieldType.weekOfWeekyear())); 117 assertEquals(1970, test.get(DateTimeFieldType.weekyear())); 118 assertEquals(10, test.get(DateTimeFieldType.hourOfDay())); 119 assertEquals(20, test.get(DateTimeFieldType.minuteOfHour())); 120 assertEquals(30, test.get(DateTimeFieldType.secondOfMinute())); 121 assertEquals(40, test.get(DateTimeFieldType.millisOfSecond())); 122 assertEquals(MILLIS_OF_DAY_UTC / 60000 , test.get(DateTimeFieldType.minuteOfDay())); 123 assertEquals(MILLIS_OF_DAY_UTC / 1000 , test.get(DateTimeFieldType.secondOfDay())); 124 assertEquals(MILLIS_OF_DAY_UTC , test.get(DateTimeFieldType.millisOfDay())); 125 assertEquals(10, test.get(DateTimeFieldType.hourOfHalfday())); 126 assertEquals(DateTimeConstants.AM, test.get(DateTimeFieldType.halfdayOfDay())); 127 128 test = new LocalDateTime(1970, 6, 9, 12, 30); 129 assertEquals(0, test.get(DateTimeFieldType.hourOfHalfday())); 130 assertEquals(12, test.get(DateTimeFieldType.clockhourOfHalfday())); 131 assertEquals(12, test.get(DateTimeFieldType.clockhourOfDay())); 132 assertEquals(DateTimeConstants.PM, test.get(DateTimeFieldType.halfdayOfDay())); 133 test = new LocalDateTime(1970, 6, 9, 14, 30); 134 assertEquals(2, test.get(DateTimeFieldType.hourOfHalfday())); 135 assertEquals(2, test.get(DateTimeFieldType.clockhourOfHalfday())); 136 assertEquals(14, test.get(DateTimeFieldType.clockhourOfDay())); 137 assertEquals(DateTimeConstants.PM, test.get(DateTimeFieldType.halfdayOfDay())); 138 test = new LocalDateTime(1970, 6, 9, 0, 30); 139 assertEquals(0, test.get(DateTimeFieldType.hourOfHalfday())); 140 assertEquals(12, test.get(DateTimeFieldType.clockhourOfHalfday())); 141 assertEquals(24, test.get(DateTimeFieldType.clockhourOfDay())); 142 assertEquals(DateTimeConstants.AM, test.get(DateTimeFieldType.halfdayOfDay())); 143 try { 144 test.get(null); 145 fail(); 146 } catch (IllegalArgumentException ex) {} 147 } 148 149 public void testSize() { 150 LocalDateTime test = new LocalDateTime(); 151 assertEquals(4, test.size()); 152 } 153 154 public void testGetFieldType_int() { 155 LocalDateTime test = new LocalDateTime(COPTIC_PARIS); 156 assertSame(DateTimeFieldType.year(), test.getFieldType(0)); 157 assertSame(DateTimeFieldType.monthOfYear(), test.getFieldType(1)); 158 assertSame(DateTimeFieldType.dayOfMonth(), test.getFieldType(2)); 159 assertSame(DateTimeFieldType.millisOfDay(), test.getFieldType(3)); 160 try { 161 test.getFieldType(-1); 162 } catch (IndexOutOfBoundsException ex) {} 163 try { 164 test.getFieldType(3); 165 } catch (IndexOutOfBoundsException ex) {} 166 } 167 168 public void testGetFieldTypes() { 169 LocalDateTime test = new LocalDateTime(COPTIC_PARIS); 170 DateTimeFieldType[] fields = test.getFieldTypes(); 171 assertSame(DateTimeFieldType.year(), fields[0]); 172 assertSame(DateTimeFieldType.monthOfYear(), fields[1]); 173 assertSame(DateTimeFieldType.dayOfMonth(), fields[2]); 174 assertSame(DateTimeFieldType.millisOfDay(), fields[3]); 175 assertNotSame(test.getFieldTypes(), test.getFieldTypes()); 176 } 177 178 public void testGetField_int() { 179 LocalDateTime test = new LocalDateTime(COPTIC_PARIS); 180 assertSame(COPTIC_UTC.year(), test.getField(0)); 181 assertSame(COPTIC_UTC.monthOfYear(), test.getField(1)); 182 assertSame(COPTIC_UTC.dayOfMonth(), test.getField(2)); 183 assertSame(COPTIC_UTC.millisOfDay(), test.getField(3)); 184 try { 185 test.getField(-1); 186 } catch (IndexOutOfBoundsException ex) {} 187 try { 188 test.getField(3); 189 } catch (IndexOutOfBoundsException ex) {} 190 } 191 192 public void testGetFields() { 193 LocalDateTime test = new LocalDateTime(COPTIC_PARIS); 194 DateTimeField[] fields = test.getFields(); 195 assertSame(COPTIC_UTC.year(), fields[0]); 196 assertSame(COPTIC_UTC.monthOfYear(), fields[1]); 197 assertSame(COPTIC_UTC.dayOfMonth(), fields[2]); 198 assertSame(COPTIC_UTC.millisOfDay(), fields[3]); 199 assertNotSame(test.getFields(), test.getFields()); 200 } 201 202 public void testGetValue_int() { 203 LocalDateTime test = new LocalDateTime(ISO_UTC); 204 assertEquals(1970, test.getValue(0)); 205 assertEquals(6, test.getValue(1)); 206 assertEquals(9, test.getValue(2)); 207 assertEquals(MILLIS_OF_DAY_UTC, test.getValue(3)); 208 try { 209 test.getValue(-1); 210 } catch (IndexOutOfBoundsException ex) {} 211 try { 212 test.getValue(3); 213 } catch (IndexOutOfBoundsException ex) {} 214 } 215 216 public void testGetValues() { 217 LocalDateTime test = new LocalDateTime(ISO_UTC); 218 int[] values = test.getValues(); 219 assertEquals(1970, values[0]); 220 assertEquals(6, values[1]); 221 assertEquals(9, values[2]); 222 assertEquals(MILLIS_OF_DAY_UTC, values[3]); 223 assertNotSame(test.getValues(), test.getValues()); 224 } 225 226 public void testIsSupported_DateTimeFieldType() { 227 LocalDateTime test = new LocalDateTime(); 228 assertEquals(true, test.isSupported(DateTimeFieldType.year())); 229 assertEquals(true, test.isSupported(DateTimeFieldType.monthOfYear())); 230 assertEquals(true, test.isSupported(DateTimeFieldType.dayOfMonth())); 231 assertEquals(true, test.isSupported(DateTimeFieldType.dayOfWeek())); 232 assertEquals(true, test.isSupported(DateTimeFieldType.dayOfYear())); 233 assertEquals(true, test.isSupported(DateTimeFieldType.weekOfWeekyear())); 234 assertEquals(true, test.isSupported(DateTimeFieldType.weekyear())); 235 assertEquals(true, test.isSupported(DateTimeFieldType.yearOfCentury())); 236 assertEquals(true, test.isSupported(DateTimeFieldType.yearOfEra())); 237 assertEquals(true, test.isSupported(DateTimeFieldType.centuryOfEra())); 238 assertEquals(true, test.isSupported(DateTimeFieldType.weekyearOfCentury())); 239 assertEquals(true, test.isSupported(DateTimeFieldType.era())); 240 241 assertEquals(true, test.isSupported(DateTimeFieldType.hourOfDay())); 242 assertEquals(true, test.isSupported(DateTimeFieldType.minuteOfHour())); 243 assertEquals(true, test.isSupported(DateTimeFieldType.secondOfMinute())); 244 assertEquals(true, test.isSupported(DateTimeFieldType.millisOfSecond())); 245 assertEquals(true, test.isSupported(DateTimeFieldType.minuteOfDay())); 246 assertEquals(true, test.isSupported(DateTimeFieldType.secondOfDay())); 247 assertEquals(true, test.isSupported(DateTimeFieldType.millisOfDay())); 248 assertEquals(true, test.isSupported(DateTimeFieldType.hourOfHalfday())); 249 assertEquals(true, test.isSupported(DateTimeFieldType.halfdayOfDay())); 250 assertEquals(true, test.isSupported(DateTimeFieldType.clockhourOfHalfday())); 251 assertEquals(true, test.isSupported(DateTimeFieldType.clockhourOfDay())); 252 253 assertEquals(false, test.isSupported((DateTimeFieldType) null)); 254 } 255 256 public void testIsSupported_DurationFieldType() { 257 LocalDateTime test = new LocalDateTime(); 258 assertEquals(false, test.isSupported(DurationFieldType.eras())); 259 assertEquals(true, test.isSupported(DurationFieldType.centuries())); 260 assertEquals(true, test.isSupported(DurationFieldType.years())); 261 assertEquals(true, test.isSupported(DurationFieldType.months())); 262 assertEquals(true, test.isSupported(DurationFieldType.weekyears())); 263 assertEquals(true, test.isSupported(DurationFieldType.weeks())); 264 assertEquals(true, test.isSupported(DurationFieldType.days())); 265 266 assertEquals(true, test.isSupported(DurationFieldType.hours())); 267 assertEquals(true, test.isSupported(DurationFieldType.minutes())); 268 assertEquals(true, test.isSupported(DurationFieldType.seconds())); 269 assertEquals(true, test.isSupported(DurationFieldType.millis())); 270 assertEquals(true, test.isSupported(DurationFieldType.halfdays())); 271 272 assertEquals(false, test.isSupported((DurationFieldType) null)); 273 } 274 275 public void testEqualsHashCode() { 276 LocalDateTime test1 = new LocalDateTime(1970, 6, 9, 10, 20, 30, 40, COPTIC_PARIS); 277 LocalDateTime test2 = new LocalDateTime(1970, 6, 9, 10, 20, 30, 40, COPTIC_PARIS); 278 assertEquals(true, test1.equals(test2)); 279 assertEquals(true, test2.equals(test1)); 280 assertEquals(true, test1.equals(test1)); 281 assertEquals(true, test2.equals(test2)); 282 assertEquals(true, test1.hashCode() == test2.hashCode()); 283 assertEquals(true, test1.hashCode() == test1.hashCode()); 284 assertEquals(true, test2.hashCode() == test2.hashCode()); 285 286 LocalDateTime test3 = new LocalDateTime(1971, 6, 9, 10, 20, 30, 40); 287 assertEquals(false, test1.equals(test3)); 288 assertEquals(false, test2.equals(test3)); 289 assertEquals(false, test3.equals(test1)); 290 assertEquals(false, test3.equals(test2)); 291 assertEquals(false, test1.hashCode() == test3.hashCode()); 292 assertEquals(false, test2.hashCode() == test3.hashCode()); 293 294 assertEquals(false, test1.equals("Hello")); 295 assertEquals(true, test1.equals(new MockInstant())); 296 assertEquals(false, test1.equals(new YearMonthDay(1970, 6, 9, COPTIC_PARIS))); 297 assertEquals(false, test1.equals(MockPartial.EMPTY_INSTANCE)); 298 } 299 300 class MockInstant extends MockPartial { 301 public Chronology getChronology() { 302 return COPTIC_UTC; 303 } 304 public DateTimeField[] getFields() { 305 return new DateTimeField[] { 306 COPTIC_UTC.year(), 307 COPTIC_UTC.monthOfYear(), 308 COPTIC_UTC.dayOfMonth(), 309 COPTIC_UTC.millisOfDay(), 310 }; 311 } 312 public int[] getValues() { 313 return new int[] {1970, 6, 9, MILLIS_OF_DAY_UTC}; 314 } 315 } 316 317 public void testCompareTo() { 319 LocalDateTime test1 = new LocalDateTime(2005, 6, 2, 10, 20, 30, 40); 320 LocalDateTime test1a = new LocalDateTime(2005, 6, 2, 10, 20, 30, 40); 321 assertEquals(0, test1.compareTo(test1a)); 322 assertEquals(0, test1a.compareTo(test1)); 323 assertEquals(0, test1.compareTo(test1)); 324 assertEquals(0, test1a.compareTo(test1a)); 325 326 LocalDateTime test2 = new LocalDateTime(2005, 7, 2, 10, 20, 30, 40); 327 assertEquals(-1, test1.compareTo(test2)); 328 assertEquals(+1, test2.compareTo(test1)); 329 330 LocalDateTime test3 = new LocalDateTime(2005, 7, 2, 10, 20, 30, 40, GREGORIAN_UTC); 331 assertEquals(-1, test1.compareTo(test3)); 332 assertEquals(+1, test3.compareTo(test1)); 333 assertEquals(0, test3.compareTo(test2)); 334 335 DateTimeFieldType[] types = new DateTimeFieldType[] { 336 DateTimeFieldType.year(), 337 DateTimeFieldType.monthOfYear(), 338 DateTimeFieldType.dayOfMonth(), 339 DateTimeFieldType.millisOfDay(), 340 }; 341 int[] values = new int[] {2005, 6, 2, MILLIS_OF_DAY_UTC}; 342 Partial p = new Partial(types, values); 343 assertEquals(0, test1.compareTo(p)); 344 try { 345 test1.compareTo(null); 346 fail(); 347 } catch (NullPointerException ex) {} 348 try { 349 test1.compareTo(new Date ()); 350 fail(); 351 } catch (ClassCastException ex) {} 352 try { 353 test1.compareTo(new YearMonthDay()); 354 fail(); 355 } catch (ClassCastException ex) {} 356 try { 357 test1.compareTo(new TimeOfDay()); 358 fail(); 359 } catch (ClassCastException ex) {} 360 Partial partial = new Partial() 361 .with(DateTimeFieldType.centuryOfEra(), 1) 362 .with(DateTimeFieldType.halfdayOfDay(), 0) 363 .with(DateTimeFieldType.dayOfMonth(), 9); 364 try { 365 new LocalDateTime(1970, 6, 9, 10, 20, 30, 40).compareTo(partial); 366 fail(); 367 } catch (ClassCastException ex) {} 368 } 369 370 public void testIsEqual_LocalDateTime() { 372 LocalDateTime test1 = new LocalDateTime(2005, 6, 2, 10, 20, 30, 40); 373 LocalDateTime test1a = new LocalDateTime(2005, 6, 2, 10, 20, 30, 40); 374 assertEquals(true, test1.isEqual(test1a)); 375 assertEquals(true, test1a.isEqual(test1)); 376 assertEquals(true, test1.isEqual(test1)); 377 assertEquals(true, test1a.isEqual(test1a)); 378 379 LocalDateTime test2 = new LocalDateTime(2005, 7, 2, 10, 20, 30, 40); 380 assertEquals(false, test1.isEqual(test2)); 381 assertEquals(false, test2.isEqual(test1)); 382 383 LocalDateTime test3 = new LocalDateTime(2005, 7, 2, 10, 20, 30, 40, GREGORIAN_UTC); 384 assertEquals(false, test1.isEqual(test3)); 385 assertEquals(false, test3.isEqual(test1)); 386 assertEquals(true, test3.isEqual(test2)); 387 388 try { 389 new LocalDateTime(2005, 7, 2, 10, 20, 30, 40).isEqual(null); 390 fail(); 391 } catch (IllegalArgumentException ex) {} 392 } 393 394 public void testIsBefore_LocalDateTime() { 396 LocalDateTime test1 = new LocalDateTime(2005, 6, 2, 10, 20, 30, 40); 397 LocalDateTime test1a = new LocalDateTime(2005, 6, 2, 10, 20, 30, 40); 398 assertEquals(false, test1.isBefore(test1a)); 399 assertEquals(false, test1a.isBefore(test1)); 400 assertEquals(false, test1.isBefore(test1)); 401 assertEquals(false, test1a.isBefore(test1a)); 402 403 LocalDateTime test2 = new LocalDateTime(2005, 7, 2, 10, 20, 30, 40); 404 assertEquals(true, test1.isBefore(test2)); 405 assertEquals(false, test2.isBefore(test1)); 406 407 LocalDateTime test3 = new LocalDateTime(2005, 7, 2, 10, 20, 30, 40, GREGORIAN_UTC); 408 assertEquals(true, test1.isBefore(test3)); 409 assertEquals(false, test3.isBefore(test1)); 410 assertEquals(false, test3.isBefore(test2)); 411 412 try { 413 new LocalDateTime(2005, 7, 2, 10, 20, 30, 40).isBefore(null); 414 fail(); 415 } catch (IllegalArgumentException ex) {} 416 } 417 418 public void testIsAfter_LocalDateTime() { 420 LocalDateTime test1 = new LocalDateTime(2005, 6, 2, 10, 20, 30, 40); 421 LocalDateTime test1a = new LocalDateTime(2005, 6, 2, 10, 20, 30, 40); 422 assertEquals(false, test1.isAfter(test1a)); 423 assertEquals(false, test1a.isAfter(test1)); 424 assertEquals(false, test1.isAfter(test1)); 425 assertEquals(false, test1a.isAfter(test1a)); 426 427 LocalDateTime test2 = new LocalDateTime(2005, 7, 2, 10, 20, 30, 40); 428 assertEquals(false, test1.isAfter(test2)); 429 assertEquals(true, test2.isAfter(test1)); 430 431 LocalDateTime test3 = new LocalDateTime(2005, 7, 2, 10, 20, 30, 40, GREGORIAN_UTC); 432 assertEquals(false, test1.isAfter(test3)); 433 assertEquals(true, test3.isAfter(test1)); 434 assertEquals(false, test3.isAfter(test2)); 435 436 try { 437 new LocalDateTime(2005, 7, 2, 10, 20, 30, 40).isAfter(null); 438 fail(); 439 } catch (IllegalArgumentException ex) {} 440 } 441 442 public void testWithDate() { 444 LocalDateTime test = new LocalDateTime(2004, 6, 9, 10, 20, 30, 40); 445 LocalDateTime result = test.withDate(2006, 2, 1); 446 447 check(test, 2004, 6, 9, 10, 20, 30, 40); 448 check(result, 2006, 2, 1, 10, 20, 30, 40); 449 } 450 451 public void testWithTime() { 453 LocalDateTime test = new LocalDateTime(2004, 6, 9, 10, 20, 30, 40); 454 LocalDateTime result = test.withTime(9, 8, 7, 6); 455 456 check(test, 2004, 6, 9, 10, 20, 30, 40); 457 check(result, 2004, 6, 9, 9, 8, 7, 6); 458 } 459 460 public void testWithField_DateTimeFieldType_int_1() { 462 LocalDateTime test = new LocalDateTime(2004, 6, 9, 10, 20, 30, 40); 463 LocalDateTime result = test.withField(DateTimeFieldType.year(), 2006); 464 465 assertEquals(new LocalDateTime(2004, 6, 9, 10, 20, 30, 40), test); 466 assertEquals(new LocalDateTime(2006, 6, 9, 10, 20, 30, 40), result); 467 } 468 469 public void testWithField_DateTimeFieldType_int_2() { 470 LocalDateTime test = new LocalDateTime(2004, 6, 9, 10, 20, 30, 40); 471 try { 472 test.withField(null, 6); 473 fail(); 474 } catch (IllegalArgumentException ex) {} 475 } 476 477 public void testWithField_DateTimeFieldType_int_3() { 478 LocalDateTime test = new LocalDateTime(2004, 6, 9, 10, 20, 30, 40); 479 LocalDateTime result = test.withField(DateTimeFieldType.year(), 2004); 480 assertEquals(new LocalDateTime(2004, 6, 9, 10, 20, 30, 40), test); 481 assertSame(test, result); 482 } 483 484 public void testWithFieldAdded_DurationFieldType_int_1() { 486 LocalDateTime test = new LocalDateTime(2004, 6, 9, 10, 20, 30, 40); 487 LocalDateTime result = test.withFieldAdded(DurationFieldType.years(), 6); 488 489 assertEquals(new LocalDateTime(2004, 6, 9, 10, 20, 30, 40), test); 490 assertEquals(new LocalDateTime(2010, 6, 9, 10, 20, 30, 40), result); 491 } 492 493 public void testWithFieldAdded_DurationFieldType_int_2() { 494 LocalDateTime test = new LocalDateTime(2004, 6, 9, 10, 20, 30, 40); 495 try { 496 test.withFieldAdded(null, 0); 497 fail(); 498 } catch (IllegalArgumentException ex) {} 499 } 500 501 public void testWithFieldAdded_DurationFieldType_int_3() { 502 LocalDateTime test = new LocalDateTime(2004, 6, 9, 10, 20, 30, 40); 503 try { 504 test.withFieldAdded(null, 6); 505 fail(); 506 } catch (IllegalArgumentException ex) {} 507 } 508 509 public void testWithFieldAdded_DurationFieldType_int_4() { 510 LocalDateTime test = new LocalDateTime(2004, 6, 9, 10, 20, 30, 40); 511 LocalDateTime result = test.withFieldAdded(DurationFieldType.years(), 0); 512 assertSame(test, result); 513 } 514 515 public void testPlus_RP() { 517 LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30, 40, BUDDHIST_LONDON); 518 LocalDateTime result = test.plus(new Period(1, 2, 3, 4, 29, 6, 7, 8)); 519 LocalDateTime expected = new LocalDateTime(2003, 7, 29, 15, 26, 37, 48, BUDDHIST_LONDON); 520 assertEquals(expected, result); 521 522 result = test.plus((ReadablePeriod) null); 523 assertSame(test, result); 524 } 525 526 public void testPlusYears_int() { 527 LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30, 40, BUDDHIST_LONDON); 528 LocalDateTime result = test.plusYears(1); 529 LocalDateTime expected = new LocalDateTime(2003, 5, 3, 10, 20, 30, 40, BUDDHIST_LONDON); 530 assertEquals(expected, result); 531 532 result = test.plusYears(0); 533 assertSame(test, result); 534 } 535 536 public void testPlusMonths_int() { 537 LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30, 40, BUDDHIST_LONDON); 538 LocalDateTime result = test.plusMonths(1); 539 LocalDateTime expected = new LocalDateTime(2002, 6, 3, 10, 20, 30, 40, BUDDHIST_LONDON); 540 assertEquals(expected, result); 541 542 result = test.plusMonths(0); 543 assertSame(test, result); 544 } 545 546 public void testPlusWeeks_int() { 547 LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30, 40, BUDDHIST_LONDON); 548 LocalDateTime result = test.plusWeeks(1); 549 LocalDateTime expected = new LocalDateTime(2002, 5, 10, 10, 20, 30, 40, BUDDHIST_LONDON); 550 assertEquals(expected, result); 551 552 result = test.plusWeeks(0); 553 assertSame(test, result); 554 } 555 556 public void testPlusDays_int() { 557 LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30, 40, BUDDHIST_LONDON); 558 LocalDateTime result = test.plusDays(1); 559 LocalDateTime expected = new LocalDateTime(2002, 5, 4, 10, 20, 30, 40, BUDDHIST_LONDON); 560 assertEquals(expected, result); 561 562 result = test.plusDays(0); 563 assertSame(test, result); 564 } 565 566 public void testPlusHours_int() { 567 LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30, 40, BUDDHIST_LONDON); 568 LocalDateTime result = test.plusHours(1); 569 LocalDateTime expected = new LocalDateTime(2002, 5, 3, 11, 20, 30, 40, BUDDHIST_LONDON); 570 assertEquals(expected, result); 571 572 result = test.plusHours(0); 573 assertSame(test, result); 574 } 575 576 public void testPlusMinutes_int() { 577 LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30, 40, BUDDHIST_LONDON); 578 LocalDateTime result = test.plusMinutes(1); 579 LocalDateTime expected = new LocalDateTime(2002, 5, 3, 10, 21, 30, 40, BUDDHIST_LONDON); 580 assertEquals(expected, result); 581 582 result = test.plusMinutes(0); 583 assertSame(test, result); 584 } 585 586 public void testPlusSeconds_int() { 587 LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30, 40, BUDDHIST_LONDON); 588 LocalDateTime result = test.plusSeconds(1); 589 LocalDateTime expected = new LocalDateTime(2002, 5, 3, 10, 20, 31, 40, BUDDHIST_LONDON); 590 assertEquals(expected, result); 591 592 result = test.plusSeconds(0); 593 assertSame(test, result); 594 } 595 596 public void testPlusMillis_int() { 597 LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30, 40, BUDDHIST_LONDON); 598 LocalDateTime result = test.plusMillis(1); 599 LocalDateTime expected = new LocalDateTime(2002, 5, 3, 10, 20, 30, 41, BUDDHIST_LONDON); 600 assertEquals(expected, result); 601 602 result = test.plusMillis(0); 603 assertSame(test, result); 604 } 605 606 public void testMinus_RP() { 608 LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30, 40, BUDDHIST_LONDON); 609 LocalDateTime result = test.minus(new Period(1, 1, 1, 1, 1, 1, 1, 1)); 610 611 LocalDateTime expected = new LocalDateTime(2001, 3, 26, 9, 19, 29, 39, BUDDHIST_LONDON); 612 assertEquals(expected, result); 613 614 result = test.minus((ReadablePeriod) null); 615 assertSame(test, result); 616 } 617 618 public void testMinusYears_int() { 619 LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30, 40, BUDDHIST_LONDON); 620 LocalDateTime result = test.minusYears(1); 621 LocalDateTime expected = new LocalDateTime(2001, 5, 3, 10, 20, 30, 40, BUDDHIST_LONDON); 622 assertEquals(expected, result); 623 624 result = test.minusYears(0); 625 assertSame(test, result); 626 } 627 628 public void testMinusMonths_int() { 629 LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30, 40, BUDDHIST_LONDON); 630 LocalDateTime result = test.minusMonths(1); 631 LocalDateTime expected = new LocalDateTime(2002, 4, 3, 10, 20, 30, 40, BUDDHIST_LONDON); 632 assertEquals(expected, result); 633 634 result = test.minusMonths(0); 635 assertSame(test, result); 636 } 637 638 public void testMinusWeeks_int() { 639 LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30, 40, BUDDHIST_LONDON); 640 LocalDateTime result = test.minusWeeks(1); 641 LocalDateTime expected = new LocalDateTime(2002, 4, 26, 10, 20, 30, 40, BUDDHIST_LONDON); 642 assertEquals(expected, result); 643 644 result = test.minusWeeks(0); 645 assertSame(test, result); 646 } 647 648 public void testMinusDays_int() { 649 LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30, 40, BUDDHIST_LONDON); 650 LocalDateTime result = test.minusDays(1); 651 LocalDateTime expected = new LocalDateTime(2002, 5, 2, 10, 20, 30, 40, BUDDHIST_LONDON); 652 assertEquals(expected, result); 653 654 result = test.minusDays(0); 655 assertSame(test, result); 656 } 657 658 public void testMinusHours_int() { 659 LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30, 40, BUDDHIST_LONDON); 660 LocalDateTime result = test.minusHours(1); 661 LocalDateTime expected = new LocalDateTime(2002, 5, 3, 9, 20, 30, 40, BUDDHIST_LONDON); 662 assertEquals(expected, result); 663 664 result = test.minusHours(0); 665 assertSame(test, result); 666 } 667 668 public void testMinusMinutes_int() { 669 LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30, 40, BUDDHIST_LONDON); 670 LocalDateTime result = test.minusMinutes(1); 671 LocalDateTime expected = new LocalDateTime(2002, 5, 3, 10, 19, 30, 40, BUDDHIST_LONDON); 672 assertEquals(expected, result); 673 674 result = test.minusMinutes(0); 675 assertSame(test, result); 676 } 677 678 public void testMinusSeconds_int() { 679 LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30, 40, BUDDHIST_LONDON); 680 LocalDateTime result = test.minusSeconds(1); 681 LocalDateTime expected = new LocalDateTime(2002, 5, 3, 10, 20, 29, 40, BUDDHIST_LONDON); 682 assertEquals(expected, result); 683 684 result = test.minusSeconds(0); 685 assertSame(test, result); 686 } 687 688 public void testMinusMillis_int() { 689 LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30, 40, BUDDHIST_LONDON); 690 LocalDateTime result = test.minusMillis(1); 691 LocalDateTime expected = new LocalDateTime(2002, 5, 3, 10, 20, 30, 39, BUDDHIST_LONDON); 692 assertEquals(expected, result); 693 694 result = test.minusMillis(0); 695 assertSame(test, result); 696 } 697 698 public void testGetters() { 700 LocalDateTime test = new LocalDateTime(1970, 6, 9, 10, 20, 30, 40, GJ_UTC); 701 assertEquals(1970, test.getYear()); 702 assertEquals(6, test.getMonthOfYear()); 703 assertEquals(9, test.getDayOfMonth()); 704 assertEquals(160, test.getDayOfYear()); 705 assertEquals(2, test.getDayOfWeek()); 706 assertEquals(24, test.getWeekOfWeekyear()); 707 assertEquals(1970, test.getWeekyear()); 708 assertEquals(70, test.getYearOfCentury()); 709 assertEquals(20, test.getCenturyOfEra()); 710 assertEquals(1970, test.getYearOfEra()); 711 assertEquals(DateTimeConstants.AD, test.getEra()); 712 assertEquals(10, test.getHourOfDay()); 713 assertEquals(20, test.getMinuteOfHour()); 714 assertEquals(30, test.getSecondOfMinute()); 715 assertEquals(40, test.getMillisOfSecond()); 716 assertEquals(MILLIS_OF_DAY_UTC, test.getMillisOfDay()); 717 } 718 719 public void testWithers() { 721 LocalDateTime test = new LocalDateTime(1970, 6, 9, 10, 20, 30, 40, GJ_UTC); 722 check(test.withYear(2000), 2000, 6, 9, 10, 20, 30, 40); 723 check(test.withMonthOfYear(2), 1970, 2, 9, 10, 20, 30, 40); 724 check(test.withDayOfMonth(2), 1970, 6, 2, 10, 20, 30, 40); 725 check(test.withDayOfYear(6), 1970, 1, 6, 10, 20, 30, 40); 726 check(test.withDayOfWeek(6), 1970, 6, 13, 10, 20, 30, 40); 727 check(test.withWeekOfWeekyear(6), 1970, 2, 3, 10, 20, 30, 40); 728 check(test.withWeekyear(1971), 1971, 6, 15, 10, 20, 30, 40); 729 check(test.withYearOfCentury(60), 1960, 6, 9, 10, 20, 30, 40); 730 check(test.withCenturyOfEra(21), 2070, 6, 9, 10, 20, 30, 40); 731 check(test.withYearOfEra(1066), 1066, 6, 9, 10, 20, 30, 40); 732 check(test.withEra(DateTimeConstants.BC), -1970, 6, 9, 10, 20, 30, 40); 733 check(test.withHourOfDay(6), 1970, 6, 9, 6, 20, 30, 40); 734 check(test.withMinuteOfHour(6), 1970, 6, 9, 10, 6, 30, 40); 735 check(test.withSecondOfMinute(6), 1970, 6, 9, 10, 20, 6, 40); 736 check(test.withMillisOfSecond(6), 1970, 6, 9, 10, 20, 30, 6); 737 check(test.withMillisOfDay(61234), 1970, 6, 9, 0, 1, 1, 234); 738 try { 739 test.withMonthOfYear(0); 740 fail(); 741 } catch (IllegalArgumentException ex) {} 742 try { 743 test.withMonthOfYear(13); 744 fail(); 745 } catch (IllegalArgumentException ex) {} 746 } 747 748 public void testToDateTime() { 750 LocalDateTime base = new LocalDateTime(2005, 6, 9, 6, 7, 8, 9, COPTIC_PARIS); 752 DateTime test = base.toDateTime(); 753 check(base, 2005, 6, 9, 6, 7, 8, 9); 754 DateTime expected = new DateTime(2005, 6, 9, 6, 7, 8, 9, COPTIC_LONDON); 755 assertEquals(expected, test); 756 } 757 758 public void testToDateTime_Zone() { 760 LocalDateTime base = new LocalDateTime(2005, 6, 9, 6, 7, 8, 9, COPTIC_PARIS); 762 DateTime test = base.toDateTime(TOKYO); 763 check(base, 2005, 6, 9, 6, 7, 8, 9); 764 DateTime expected = new DateTime(2005, 6, 9, 6, 7, 8, 9, COPTIC_TOKYO); 765 assertEquals(expected, test); 766 } 767 768 public void testToDateTime_nullZone() { 769 LocalDateTime base = new LocalDateTime(2005, 6, 9, 6, 7, 8, 9, COPTIC_PARIS); 771 DateTime test = base.toDateTime((DateTimeZone) null); 772 check(base, 2005, 6, 9, 6, 7, 8, 9); 773 DateTime expected = new DateTime(2005, 6, 9, 6, 7, 8, 9, COPTIC_LONDON); 774 assertEquals(expected, test); 775 } 776 777 public void testToLocalDate() { 779 LocalDateTime base = new LocalDateTime(2005, 6, 9, 6, 7, 8, 9, COPTIC_PARIS); LocalDate expected = new LocalDate(2005, 6, 9, COPTIC_LONDON); 781 assertEquals(expected,base.toLocalDate()); 782 } 783 784 public void testToLocalTime() { 785 LocalDateTime base = new LocalDateTime(2005, 6, 9, 6, 7, 8, 9, COPTIC_PARIS); LocalTime expected = new LocalTime(6, 7, 8, 9, COPTIC_LONDON); 787 assertEquals(expected,base.toLocalTime()); 788 } 789 790 public void testToDateTime_RI() { 792 LocalDateTime base = new LocalDateTime(2005, 6, 9, 10, 20, 30, 40, COPTIC_PARIS); 793 DateTime dt = new DateTime(2002, 1, 3, 4, 5, 6, 7, BUDDHIST_TOKYO); 794 795 DateTime test = base.toDateTime(dt); 796 check(base, 2005, 6, 9, 10, 20, 30, 40); 797 DateTime expected = new DateTime(2005, 6, 9, 10, 20, 30, 40, BUDDHIST_TOKYO); 798 assertEquals(expected, test); 799 } 800 801 public void testToDateTime_nullRI() { 802 LocalDateTime base = new LocalDateTime(2005, 6, 9, 10, 20, 30, 40, COPTIC_PARIS); 803 804 DateTime test = base.toDateTime((ReadableInstant) null); 805 check(base, 2005, 6, 9, 10, 20, 30, 40); 806 DateTime expected = new DateTime(2005, 6, 9, 10, 20, 30, 40, ISO_LONDON); 807 assertEquals(expected, test); 808 } 809 810 public void testProperty() { 812 LocalDateTime test = new LocalDateTime(2005, 6, 9, 10, 20, 30, 40, GJ_UTC); 813 assertEquals(test.year(), test.property(DateTimeFieldType.year())); 814 assertEquals(test.monthOfYear(), test.property(DateTimeFieldType.monthOfYear())); 815 assertEquals(test.dayOfMonth(), test.property(DateTimeFieldType.dayOfMonth())); 816 assertEquals(test.dayOfWeek(), test.property(DateTimeFieldType.dayOfWeek())); 817 assertEquals(test.dayOfYear(), test.property(DateTimeFieldType.dayOfYear())); 818 assertEquals(test.weekOfWeekyear(), test.property(DateTimeFieldType.weekOfWeekyear())); 819 assertEquals(test.weekyear(), test.property(DateTimeFieldType.weekyear())); 820 assertEquals(test.yearOfCentury(), test.property(DateTimeFieldType.yearOfCentury())); 821 assertEquals(test.yearOfEra(), test.property(DateTimeFieldType.yearOfEra())); 822 assertEquals(test.centuryOfEra(), test.property(DateTimeFieldType.centuryOfEra())); 823 assertEquals(test.era(), test.property(DateTimeFieldType.era())); 824 assertEquals(test.hourOfDay(), test.property(DateTimeFieldType.hourOfDay())); 825 assertEquals(test.minuteOfHour(), test.property(DateTimeFieldType.minuteOfHour())); 826 assertEquals(test.secondOfMinute(), test.property(DateTimeFieldType.secondOfMinute())); 827 assertEquals(test.millisOfSecond(), test.property(DateTimeFieldType.millisOfSecond())); 828 assertEquals(test.millisOfDay(), test.property(DateTimeFieldType.millisOfDay())); 829 830 try { 831 test.property(null); 832 fail(); 833 } catch (IllegalArgumentException ex) {} 834 assertEquals(test, test.property(DateTimeFieldType.minuteOfDay()).getLocalDateTime()); 835 } 836 837 public void testSerialization() throws Exception { 839 LocalDateTime test = new LocalDateTime(1972, 6, 9, 10, 20, 30, 40, COPTIC_PARIS); 840 841 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 842 ObjectOutputStream oos = new ObjectOutputStream (baos); 843 oos.writeObject(test); 844 byte[] bytes = baos.toByteArray(); 845 oos.close(); 846 847 ByteArrayInputStream bais = new ByteArrayInputStream (bytes); 848 ObjectInputStream ois = new ObjectInputStream (bais); 849 LocalDateTime result = (LocalDateTime) ois.readObject(); 850 ois.close(); 851 852 assertEquals(test, result); 853 assertTrue(Arrays.equals(test.getValues(), result.getValues())); 854 assertTrue(Arrays.equals(test.getFields(), result.getFields())); 855 assertEquals(test.getChronology(), result.getChronology()); 856 } 857 858 public void testToString() { 860 LocalDateTime test = new LocalDateTime(2002, 6, 9, 10, 20, 30, 40); 861 assertEquals("2002-06-09T10:20:30.040", test.toString()); 862 } 863 864 public void testToString_String() { 866 LocalDateTime test = new LocalDateTime(2002, 6, 9, 10, 20, 30, 40); 867 assertEquals("2002 10", test.toString("yyyy HH")); 868 assertEquals("2002-06-09T10:20:30.040", test.toString((String ) null)); 869 } 870 871 public void testToString_String_Locale() { 873 LocalDateTime test = new LocalDateTime(1970, 6, 9, 10, 20, 30, 40); 874 assertEquals("Tue 9/6", test.toString("EEE d/M", Locale.ENGLISH)); 875 assertEquals("mar. 9/6", test.toString("EEE d/M", Locale.FRENCH)); 876 assertEquals("1970-06-09T10:20:30.040", test.toString(null, Locale.ENGLISH)); 877 assertEquals("Tue 9/6", test.toString("EEE d/M", null)); 878 assertEquals("1970-06-09T10:20:30.040", test.toString(null, null)); 879 } 880 881 public void testToString_DTFormatter() { 883 LocalDateTime test = new LocalDateTime(2002, 6, 9, 10, 20, 30, 40); 884 assertEquals("2002 10", test.toString(DateTimeFormat.forPattern("yyyy HH"))); 885 assertEquals("2002-06-09T10:20:30.040", test.toString((DateTimeFormatter) null)); 886 } 887 888 private void check(LocalDateTime test, int year, int month, int day, int hour, int min, int sec, int mil) { 890 assertEquals(year, test.getYear()); 891 assertEquals(month, test.getMonthOfYear()); 892 assertEquals(day, test.getDayOfMonth()); 893 assertEquals(hour, test.getHourOfDay()); 894 assertEquals(min, test.getMinuteOfHour()); 895 assertEquals(sec, test.getSecondOfMinute()); 896 assertEquals(mil, test.getMillisOfSecond()); 897 } 898 } 899 | Popular Tags |