001 /* 002 * Copyright 2001-2010 Stephen Colebourne 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.joda.time; 017 018 import java.io.ByteArrayInputStream; 019 import java.io.ByteArrayOutputStream; 020 import java.io.ObjectInputStream; 021 import java.io.ObjectOutputStream; 022 import java.util.Arrays; 023 import java.util.Calendar; 024 import java.util.Date; 025 import java.util.GregorianCalendar; 026 import java.util.Locale; 027 import java.util.SimpleTimeZone; 028 import java.util.TimeZone; 029 030 import junit.framework.TestCase; 031 import junit.framework.TestSuite; 032 033 import org.joda.time.chrono.BuddhistChronology; 034 import org.joda.time.chrono.CopticChronology; 035 import org.joda.time.chrono.GJChronology; 036 import org.joda.time.chrono.GregorianChronology; 037 import org.joda.time.chrono.ISOChronology; 038 import org.joda.time.format.DateTimeFormat; 039 import org.joda.time.format.DateTimeFormatter; 040 041 /** 042 * This class is a Junit unit test for LocalDate. 043 * 044 * @author Stephen Colebourne 045 */ 046 public class TestLocalDateTime_Basics extends TestCase { 047 048 private static final DateTimeZone PARIS = DateTimeZone.forID("Europe/Paris"); 049 private static final DateTimeZone LONDON = DateTimeZone.forID("Europe/London"); 050 private static final DateTimeZone TOKYO = DateTimeZone.forID("Asia/Tokyo"); 051 private static final int OFFSET = 1; 052 private static final GJChronology GJ_UTC = GJChronology.getInstanceUTC(); 053 private static final Chronology COPTIC_PARIS = CopticChronology.getInstance(PARIS); 054 private static final Chronology COPTIC_LONDON = CopticChronology.getInstance(LONDON); 055 private static final Chronology COPTIC_TOKYO = CopticChronology.getInstance(TOKYO); 056 private static final Chronology COPTIC_UTC = CopticChronology.getInstanceUTC(); 057 private static final Chronology ISO_PARIS = ISOChronology.getInstance(PARIS); 058 private static final Chronology ISO_LONDON = ISOChronology.getInstance(LONDON); 059 private static final Chronology ISO_TOKYO = ISOChronology.getInstance(TOKYO); 060 private static final Chronology ISO_UTC = ISOChronology.getInstanceUTC(); 061 private static final Chronology GREGORIAN_UTC = GregorianChronology.getInstanceUTC(); 062 private static final Chronology BUDDHIST_PARIS = BuddhistChronology.getInstance(PARIS); 063 private static final Chronology BUDDHIST_LONDON = BuddhistChronology.getInstance(LONDON); 064 private static final Chronology BUDDHIST_TOKYO = BuddhistChronology.getInstance(TOKYO); 065 private static final Chronology BUDDHIST_UTC = BuddhistChronology.getInstanceUTC(); 066 067 // private long TEST_TIME1 = 068 // (31L + 28L + 31L + 6L -1L) * DateTimeConstants.MILLIS_PER_DAY 069 // + 12L * DateTimeConstants.MILLIS_PER_HOUR 070 // + 24L * DateTimeConstants.MILLIS_PER_MINUTE; 071 // 072 // private long TEST_TIME2 = 073 // (365L + 31L + 28L + 31L + 30L + 7L -1L) * DateTimeConstants.MILLIS_PER_DAY 074 // + 14L * DateTimeConstants.MILLIS_PER_HOUR 075 // + 28L * DateTimeConstants.MILLIS_PER_MINUTE; 076 077 private int MILLIS_OF_DAY_UTC = 078 (int) (10L * DateTimeConstants.MILLIS_PER_HOUR 079 + 20L * DateTimeConstants.MILLIS_PER_MINUTE 080 + 30L * DateTimeConstants.MILLIS_PER_SECOND 081 + 40L); 082 083 private long TEST_TIME_NOW_UTC = 084 (31L + 28L + 31L + 30L + 31L + 9L -1L) * DateTimeConstants.MILLIS_PER_DAY + MILLIS_OF_DAY_UTC; 085 086 private DateTimeZone zone = null; 087 088 private Locale systemDefaultLocale = null; 089 090 public static void main(String[] args) { 091 junit.textui.TestRunner.run(suite()); 092 } 093 094 public static TestSuite suite() { 095 return new TestSuite(TestLocalDateTime_Basics.class); 096 } 097 098 public TestLocalDateTime_Basics(String name) { 099 super(name); 100 } 101 102 protected void setUp() throws Exception { 103 DateTimeUtils.setCurrentMillisFixed(TEST_TIME_NOW_UTC); 104 zone = DateTimeZone.getDefault(); 105 DateTimeZone.setDefault(LONDON); 106 systemDefaultLocale = Locale.getDefault(); 107 Locale.setDefault(Locale.ENGLISH); 108 } 109 110 protected void tearDown() throws Exception { 111 DateTimeUtils.setCurrentMillisSystem(); 112 DateTimeZone.setDefault(zone); 113 zone = null; 114 Locale.setDefault(systemDefaultLocale); 115 systemDefaultLocale = null; 116 } 117 118 //----------------------------------------------------------------------- 119 public void testGet_DateTimeFieldType() { 120 LocalDateTime test = new LocalDateTime(1970, 6, 9, 10, 20, 30, 40); 121 assertEquals(1970, test.get(DateTimeFieldType.year())); 122 assertEquals(6, test.get(DateTimeFieldType.monthOfYear())); 123 assertEquals(9, test.get(DateTimeFieldType.dayOfMonth())); 124 assertEquals(2, test.get(DateTimeFieldType.dayOfWeek())); 125 assertEquals(160, test.get(DateTimeFieldType.dayOfYear())); 126 assertEquals(24, test.get(DateTimeFieldType.weekOfWeekyear())); 127 assertEquals(1970, test.get(DateTimeFieldType.weekyear())); 128 assertEquals(10, test.get(DateTimeFieldType.hourOfDay())); 129 assertEquals(20, test.get(DateTimeFieldType.minuteOfHour())); 130 assertEquals(30, test.get(DateTimeFieldType.secondOfMinute())); 131 assertEquals(40, test.get(DateTimeFieldType.millisOfSecond())); 132 assertEquals(MILLIS_OF_DAY_UTC / 60000 , test.get(DateTimeFieldType.minuteOfDay())); 133 assertEquals(MILLIS_OF_DAY_UTC / 1000 , test.get(DateTimeFieldType.secondOfDay())); 134 assertEquals(MILLIS_OF_DAY_UTC , test.get(DateTimeFieldType.millisOfDay())); 135 assertEquals(10, test.get(DateTimeFieldType.hourOfHalfday())); 136 assertEquals(DateTimeConstants.AM, test.get(DateTimeFieldType.halfdayOfDay())); 137 138 test = new LocalDateTime(1970, 6, 9, 12, 30); 139 assertEquals(0, test.get(DateTimeFieldType.hourOfHalfday())); 140 assertEquals(12, test.get(DateTimeFieldType.clockhourOfHalfday())); 141 assertEquals(12, test.get(DateTimeFieldType.clockhourOfDay())); 142 assertEquals(DateTimeConstants.PM, test.get(DateTimeFieldType.halfdayOfDay())); 143 test = new LocalDateTime(1970, 6, 9, 14, 30); 144 assertEquals(2, test.get(DateTimeFieldType.hourOfHalfday())); 145 assertEquals(2, test.get(DateTimeFieldType.clockhourOfHalfday())); 146 assertEquals(14, test.get(DateTimeFieldType.clockhourOfDay())); 147 assertEquals(DateTimeConstants.PM, test.get(DateTimeFieldType.halfdayOfDay())); 148 test = new LocalDateTime(1970, 6, 9, 0, 30); 149 assertEquals(0, test.get(DateTimeFieldType.hourOfHalfday())); 150 assertEquals(12, test.get(DateTimeFieldType.clockhourOfHalfday())); 151 assertEquals(24, test.get(DateTimeFieldType.clockhourOfDay())); 152 assertEquals(DateTimeConstants.AM, test.get(DateTimeFieldType.halfdayOfDay())); 153 try { 154 test.get(null); 155 fail(); 156 } catch (IllegalArgumentException ex) {} 157 } 158 159 public void testSize() { 160 LocalDateTime test = new LocalDateTime(); 161 assertEquals(4, test.size()); 162 } 163 164 public void testGetFieldType_int() { 165 LocalDateTime test = new LocalDateTime(COPTIC_PARIS); 166 assertSame(DateTimeFieldType.year(), test.getFieldType(0)); 167 assertSame(DateTimeFieldType.monthOfYear(), test.getFieldType(1)); 168 assertSame(DateTimeFieldType.dayOfMonth(), test.getFieldType(2)); 169 assertSame(DateTimeFieldType.millisOfDay(), test.getFieldType(3)); 170 try { 171 test.getFieldType(-1); 172 } catch (IndexOutOfBoundsException ex) {} 173 try { 174 test.getFieldType(3); 175 } catch (IndexOutOfBoundsException ex) {} 176 } 177 178 public void testGetFieldTypes() { 179 LocalDateTime test = new LocalDateTime(COPTIC_PARIS); 180 DateTimeFieldType[] fields = test.getFieldTypes(); 181 assertSame(DateTimeFieldType.year(), fields[0]); 182 assertSame(DateTimeFieldType.monthOfYear(), fields[1]); 183 assertSame(DateTimeFieldType.dayOfMonth(), fields[2]); 184 assertSame(DateTimeFieldType.millisOfDay(), fields[3]); 185 assertNotSame(test.getFieldTypes(), test.getFieldTypes()); 186 } 187 188 public void testGetField_int() { 189 LocalDateTime test = new LocalDateTime(COPTIC_PARIS); 190 assertSame(COPTIC_UTC.year(), test.getField(0)); 191 assertSame(COPTIC_UTC.monthOfYear(), test.getField(1)); 192 assertSame(COPTIC_UTC.dayOfMonth(), test.getField(2)); 193 assertSame(COPTIC_UTC.millisOfDay(), test.getField(3)); 194 try { 195 test.getField(-1); 196 } catch (IndexOutOfBoundsException ex) {} 197 try { 198 test.getField(3); 199 } catch (IndexOutOfBoundsException ex) {} 200 } 201 202 public void testGetFields() { 203 LocalDateTime test = new LocalDateTime(COPTIC_PARIS); 204 DateTimeField[] fields = test.getFields(); 205 assertSame(COPTIC_UTC.year(), fields[0]); 206 assertSame(COPTIC_UTC.monthOfYear(), fields[1]); 207 assertSame(COPTIC_UTC.dayOfMonth(), fields[2]); 208 assertSame(COPTIC_UTC.millisOfDay(), fields[3]); 209 assertNotSame(test.getFields(), test.getFields()); 210 } 211 212 public void testGetValue_int() { 213 LocalDateTime test = new LocalDateTime(ISO_UTC); 214 assertEquals(1970, test.getValue(0)); 215 assertEquals(6, test.getValue(1)); 216 assertEquals(9, test.getValue(2)); 217 assertEquals(MILLIS_OF_DAY_UTC, test.getValue(3)); 218 try { 219 test.getValue(-1); 220 } catch (IndexOutOfBoundsException ex) {} 221 try { 222 test.getValue(3); 223 } catch (IndexOutOfBoundsException ex) {} 224 } 225 226 public void testGetValues() { 227 LocalDateTime test = new LocalDateTime(ISO_UTC); 228 int[] values = test.getValues(); 229 assertEquals(1970, values[0]); 230 assertEquals(6, values[1]); 231 assertEquals(9, values[2]); 232 assertEquals(MILLIS_OF_DAY_UTC, values[3]); 233 assertNotSame(test.getValues(), test.getValues()); 234 } 235 236 public void testIsSupported_DateTimeFieldType() { 237 LocalDateTime test = new LocalDateTime(); 238 assertEquals(true, test.isSupported(DateTimeFieldType.year())); 239 assertEquals(true, test.isSupported(DateTimeFieldType.monthOfYear())); 240 assertEquals(true, test.isSupported(DateTimeFieldType.dayOfMonth())); 241 assertEquals(true, test.isSupported(DateTimeFieldType.dayOfWeek())); 242 assertEquals(true, test.isSupported(DateTimeFieldType.dayOfYear())); 243 assertEquals(true, test.isSupported(DateTimeFieldType.weekOfWeekyear())); 244 assertEquals(true, test.isSupported(DateTimeFieldType.weekyear())); 245 assertEquals(true, test.isSupported(DateTimeFieldType.yearOfCentury())); 246 assertEquals(true, test.isSupported(DateTimeFieldType.yearOfEra())); 247 assertEquals(true, test.isSupported(DateTimeFieldType.centuryOfEra())); 248 assertEquals(true, test.isSupported(DateTimeFieldType.weekyearOfCentury())); 249 assertEquals(true, test.isSupported(DateTimeFieldType.era())); 250 251 assertEquals(true, test.isSupported(DateTimeFieldType.hourOfDay())); 252 assertEquals(true, test.isSupported(DateTimeFieldType.minuteOfHour())); 253 assertEquals(true, test.isSupported(DateTimeFieldType.secondOfMinute())); 254 assertEquals(true, test.isSupported(DateTimeFieldType.millisOfSecond())); 255 assertEquals(true, test.isSupported(DateTimeFieldType.minuteOfDay())); 256 assertEquals(true, test.isSupported(DateTimeFieldType.secondOfDay())); 257 assertEquals(true, test.isSupported(DateTimeFieldType.millisOfDay())); 258 assertEquals(true, test.isSupported(DateTimeFieldType.hourOfHalfday())); 259 assertEquals(true, test.isSupported(DateTimeFieldType.halfdayOfDay())); 260 assertEquals(true, test.isSupported(DateTimeFieldType.clockhourOfHalfday())); 261 assertEquals(true, test.isSupported(DateTimeFieldType.clockhourOfDay())); 262 263 assertEquals(false, test.isSupported((DateTimeFieldType) null)); 264 } 265 266 public void testIsSupported_DurationFieldType() { 267 LocalDateTime test = new LocalDateTime(); 268 assertEquals(false, test.isSupported(DurationFieldType.eras())); 269 assertEquals(true, test.isSupported(DurationFieldType.centuries())); 270 assertEquals(true, test.isSupported(DurationFieldType.years())); 271 assertEquals(true, test.isSupported(DurationFieldType.months())); 272 assertEquals(true, test.isSupported(DurationFieldType.weekyears())); 273 assertEquals(true, test.isSupported(DurationFieldType.weeks())); 274 assertEquals(true, test.isSupported(DurationFieldType.days())); 275 276 assertEquals(true, test.isSupported(DurationFieldType.hours())); 277 assertEquals(true, test.isSupported(DurationFieldType.minutes())); 278 assertEquals(true, test.isSupported(DurationFieldType.seconds())); 279 assertEquals(true, test.isSupported(DurationFieldType.millis())); 280 assertEquals(true, test.isSupported(DurationFieldType.halfdays())); 281 282 assertEquals(false, test.isSupported((DurationFieldType) null)); 283 } 284 285 public void testEqualsHashCode() { 286 LocalDateTime test1 = new LocalDateTime(1970, 6, 9, 10, 20, 30, 40, COPTIC_PARIS); 287 LocalDateTime test2 = new LocalDateTime(1970, 6, 9, 10, 20, 30, 40, COPTIC_PARIS); 288 assertEquals(true, test1.equals(test2)); 289 assertEquals(true, test2.equals(test1)); 290 assertEquals(true, test1.equals(test1)); 291 assertEquals(true, test2.equals(test2)); 292 assertEquals(true, test1.hashCode() == test2.hashCode()); 293 assertEquals(true, test1.hashCode() == test1.hashCode()); 294 assertEquals(true, test2.hashCode() == test2.hashCode()); 295 296 LocalDateTime test3 = new LocalDateTime(1971, 6, 9, 10, 20, 30, 40); 297 assertEquals(false, test1.equals(test3)); 298 assertEquals(false, test2.equals(test3)); 299 assertEquals(false, test3.equals(test1)); 300 assertEquals(false, test3.equals(test2)); 301 assertEquals(false, test1.hashCode() == test3.hashCode()); 302 assertEquals(false, test2.hashCode() == test3.hashCode()); 303 304 assertEquals(false, test1.equals("Hello")); 305 assertEquals(true, test1.equals(new MockInstant())); 306 Partial partial = new Partial( 307 new DateTimeFieldType[] { 308 DateTimeFieldType.year(), DateTimeFieldType.monthOfYear(), 309 DateTimeFieldType.dayOfMonth(), DateTimeFieldType.millisOfDay()}, 310 new int[] {1970, 6, 9, MILLIS_OF_DAY_UTC}, COPTIC_PARIS); 311 assertEquals(true, test1.equals(partial)); 312 assertEquals(true, test1.hashCode() == partial.hashCode()); 313 assertEquals(false, test1.equals(MockPartial.EMPTY_INSTANCE)); 314 } 315 316 class MockInstant extends MockPartial { 317 public Chronology getChronology() { 318 return COPTIC_UTC; 319 } 320 public DateTimeField[] getFields() { 321 return new DateTimeField[] { 322 COPTIC_UTC.year(), 323 COPTIC_UTC.monthOfYear(), 324 COPTIC_UTC.dayOfMonth(), 325 COPTIC_UTC.millisOfDay(), 326 }; 327 } 328 public int[] getValues() { 329 return new int[] {1970, 6, 9, MILLIS_OF_DAY_UTC}; 330 } 331 } 332 333 //----------------------------------------------------------------------- 334 public void testCompareTo() { 335 LocalDateTime test1 = new LocalDateTime(2005, 6, 2, 10, 20, 30, 40); 336 LocalDateTime test1a = new LocalDateTime(2005, 6, 2, 10, 20, 30, 40); 337 assertEquals(0, test1.compareTo(test1a)); 338 assertEquals(0, test1a.compareTo(test1)); 339 assertEquals(0, test1.compareTo(test1)); 340 assertEquals(0, test1a.compareTo(test1a)); 341 342 LocalDateTime test2 = new LocalDateTime(2005, 7, 2, 10, 20, 30, 40); 343 assertEquals(-1, test1.compareTo(test2)); 344 assertEquals(+1, test2.compareTo(test1)); 345 346 LocalDateTime test3 = new LocalDateTime(2005, 7, 2, 10, 20, 30, 40, GREGORIAN_UTC); 347 assertEquals(-1, test1.compareTo(test3)); 348 assertEquals(+1, test3.compareTo(test1)); 349 assertEquals(0, test3.compareTo(test2)); 350 351 DateTimeFieldType[] types = new DateTimeFieldType[] { 352 DateTimeFieldType.year(), 353 DateTimeFieldType.monthOfYear(), 354 DateTimeFieldType.dayOfMonth(), 355 DateTimeFieldType.millisOfDay(), 356 }; 357 int[] values = new int[] {2005, 6, 2, MILLIS_OF_DAY_UTC}; 358 Partial p = new Partial(types, values); 359 assertEquals(0, test1.compareTo(p)); 360 try { 361 test1.compareTo(null); 362 fail(); 363 } catch (NullPointerException ex) {} 364 // try { 365 // test1.compareTo(new Date()); 366 // fail(); 367 // } catch (ClassCastException ex) {} 368 try { 369 test1.compareTo(new YearMonthDay()); 370 fail(); 371 } catch (ClassCastException ex) {} 372 try { 373 test1.compareTo(new TimeOfDay()); 374 fail(); 375 } catch (ClassCastException ex) {} 376 Partial partial = new Partial() 377 .with(DateTimeFieldType.centuryOfEra(), 1) 378 .with(DateTimeFieldType.halfdayOfDay(), 0) 379 .with(DateTimeFieldType.dayOfMonth(), 9); 380 try { 381 new LocalDateTime(1970, 6, 9, 10, 20, 30, 40).compareTo(partial); 382 fail(); 383 } catch (ClassCastException ex) {} 384 } 385 386 //----------------------------------------------------------------------- 387 public void testIsEqual_LocalDateTime() { 388 LocalDateTime test1 = new LocalDateTime(2005, 6, 2, 10, 20, 30, 40); 389 LocalDateTime test1a = new LocalDateTime(2005, 6, 2, 10, 20, 30, 40); 390 assertEquals(true, test1.isEqual(test1a)); 391 assertEquals(true, test1a.isEqual(test1)); 392 assertEquals(true, test1.isEqual(test1)); 393 assertEquals(true, test1a.isEqual(test1a)); 394 395 LocalDateTime test2 = new LocalDateTime(2005, 7, 2, 10, 20, 30, 40); 396 assertEquals(false, test1.isEqual(test2)); 397 assertEquals(false, test2.isEqual(test1)); 398 399 LocalDateTime test3 = new LocalDateTime(2005, 7, 2, 10, 20, 30, 40, GREGORIAN_UTC); 400 assertEquals(false, test1.isEqual(test3)); 401 assertEquals(false, test3.isEqual(test1)); 402 assertEquals(true, test3.isEqual(test2)); 403 404 try { 405 new LocalDateTime(2005, 7, 2, 10, 20, 30, 40).isEqual(null); 406 fail(); 407 } catch (IllegalArgumentException ex) {} 408 } 409 410 //----------------------------------------------------------------------- 411 public void testIsBefore_LocalDateTime() { 412 LocalDateTime test1 = new LocalDateTime(2005, 6, 2, 10, 20, 30, 40); 413 LocalDateTime test1a = new LocalDateTime(2005, 6, 2, 10, 20, 30, 40); 414 assertEquals(false, test1.isBefore(test1a)); 415 assertEquals(false, test1a.isBefore(test1)); 416 assertEquals(false, test1.isBefore(test1)); 417 assertEquals(false, test1a.isBefore(test1a)); 418 419 LocalDateTime test2 = new LocalDateTime(2005, 7, 2, 10, 20, 30, 40); 420 assertEquals(true, test1.isBefore(test2)); 421 assertEquals(false, test2.isBefore(test1)); 422 423 LocalDateTime test3 = new LocalDateTime(2005, 7, 2, 10, 20, 30, 40, GREGORIAN_UTC); 424 assertEquals(true, test1.isBefore(test3)); 425 assertEquals(false, test3.isBefore(test1)); 426 assertEquals(false, test3.isBefore(test2)); 427 428 try { 429 new LocalDateTime(2005, 7, 2, 10, 20, 30, 40).isBefore(null); 430 fail(); 431 } catch (IllegalArgumentException ex) {} 432 } 433 434 //----------------------------------------------------------------------- 435 public void testIsAfter_LocalDateTime() { 436 LocalDateTime test1 = new LocalDateTime(2005, 6, 2, 10, 20, 30, 40); 437 LocalDateTime test1a = new LocalDateTime(2005, 6, 2, 10, 20, 30, 40); 438 assertEquals(false, test1.isAfter(test1a)); 439 assertEquals(false, test1a.isAfter(test1)); 440 assertEquals(false, test1.isAfter(test1)); 441 assertEquals(false, test1a.isAfter(test1a)); 442 443 LocalDateTime test2 = new LocalDateTime(2005, 7, 2, 10, 20, 30, 40); 444 assertEquals(false, test1.isAfter(test2)); 445 assertEquals(true, test2.isAfter(test1)); 446 447 LocalDateTime test3 = new LocalDateTime(2005, 7, 2, 10, 20, 30, 40, GREGORIAN_UTC); 448 assertEquals(false, test1.isAfter(test3)); 449 assertEquals(true, test3.isAfter(test1)); 450 assertEquals(false, test3.isAfter(test2)); 451 452 try { 453 new LocalDateTime(2005, 7, 2, 10, 20, 30, 40).isAfter(null); 454 fail(); 455 } catch (IllegalArgumentException ex) {} 456 } 457 458 //----------------------------------------------------------------------- 459 public void testWithDate() { 460 LocalDateTime test = new LocalDateTime(2004, 6, 9, 10, 20, 30, 40); 461 LocalDateTime result = test.withDate(2006, 2, 1); 462 463 check(test, 2004, 6, 9, 10, 20, 30, 40); 464 check(result, 2006, 2, 1, 10, 20, 30, 40); 465 } 466 467 //----------------------------------------------------------------------- 468 public void testWithTime() { 469 LocalDateTime test = new LocalDateTime(2004, 6, 9, 10, 20, 30, 40); 470 LocalDateTime result = test.withTime(9, 8, 7, 6); 471 472 check(test, 2004, 6, 9, 10, 20, 30, 40); 473 check(result, 2004, 6, 9, 9, 8, 7, 6); 474 } 475 476 //----------------------------------------------------------------------- 477 public void testWithField_DateTimeFieldType_int_1() { 478 LocalDateTime test = new LocalDateTime(2004, 6, 9, 10, 20, 30, 40); 479 LocalDateTime result = test.withField(DateTimeFieldType.year(), 2006); 480 481 assertEquals(new LocalDateTime(2004, 6, 9, 10, 20, 30, 40), test); 482 assertEquals(new LocalDateTime(2006, 6, 9, 10, 20, 30, 40), result); 483 } 484 485 public void testWithField_DateTimeFieldType_int_2() { 486 LocalDateTime test = new LocalDateTime(2004, 6, 9, 10, 20, 30, 40); 487 try { 488 test.withField(null, 6); 489 fail(); 490 } catch (IllegalArgumentException ex) {} 491 } 492 493 public void testWithField_DateTimeFieldType_int_3() { 494 LocalDateTime test = new LocalDateTime(2004, 6, 9, 10, 20, 30, 40); 495 LocalDateTime result = test.withField(DateTimeFieldType.year(), 2004); 496 assertEquals(new LocalDateTime(2004, 6, 9, 10, 20, 30, 40), test); 497 assertSame(test, result); 498 } 499 500 //----------------------------------------------------------------------- 501 public void testWithFieldAdded_DurationFieldType_int_1() { 502 LocalDateTime test = new LocalDateTime(2004, 6, 9, 10, 20, 30, 40); 503 LocalDateTime result = test.withFieldAdded(DurationFieldType.years(), 6); 504 505 assertEquals(new LocalDateTime(2004, 6, 9, 10, 20, 30, 40), test); 506 assertEquals(new LocalDateTime(2010, 6, 9, 10, 20, 30, 40), result); 507 } 508 509 public void testWithFieldAdded_DurationFieldType_int_2() { 510 LocalDateTime test = new LocalDateTime(2004, 6, 9, 10, 20, 30, 40); 511 try { 512 test.withFieldAdded(null, 0); 513 fail(); 514 } catch (IllegalArgumentException ex) {} 515 } 516 517 public void testWithFieldAdded_DurationFieldType_int_3() { 518 LocalDateTime test = new LocalDateTime(2004, 6, 9, 10, 20, 30, 40); 519 try { 520 test.withFieldAdded(null, 6); 521 fail(); 522 } catch (IllegalArgumentException ex) {} 523 } 524 525 public void testWithFieldAdded_DurationFieldType_int_4() { 526 LocalDateTime test = new LocalDateTime(2004, 6, 9, 10, 20, 30, 40); 527 LocalDateTime result = test.withFieldAdded(DurationFieldType.years(), 0); 528 assertSame(test, result); 529 } 530 531 //----------------------------------------------------------------------- 532 public void testPlus_RP() { 533 LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30, 40, BUDDHIST_LONDON); 534 LocalDateTime result = test.plus(new Period(1, 2, 3, 4, 29, 6, 7, 8)); 535 LocalDateTime expected = new LocalDateTime(2003, 7, 29, 15, 26, 37, 48, BUDDHIST_LONDON); 536 assertEquals(expected, result); 537 538 result = test.plus((ReadablePeriod) null); 539 assertSame(test, result); 540 } 541 542 public void testPlusYears_int() { 543 LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30, 40, BUDDHIST_LONDON); 544 LocalDateTime result = test.plusYears(1); 545 LocalDateTime expected = new LocalDateTime(2003, 5, 3, 10, 20, 30, 40, BUDDHIST_LONDON); 546 assertEquals(expected, result); 547 548 result = test.plusYears(0); 549 assertSame(test, result); 550 } 551 552 public void testPlusMonths_int() { 553 LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30, 40, BUDDHIST_LONDON); 554 LocalDateTime result = test.plusMonths(1); 555 LocalDateTime expected = new LocalDateTime(2002, 6, 3, 10, 20, 30, 40, BUDDHIST_LONDON); 556 assertEquals(expected, result); 557 558 result = test.plusMonths(0); 559 assertSame(test, result); 560 } 561 562 public void testPlusWeeks_int() { 563 LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30, 40, BUDDHIST_LONDON); 564 LocalDateTime result = test.plusWeeks(1); 565 LocalDateTime expected = new LocalDateTime(2002, 5, 10, 10, 20, 30, 40, BUDDHIST_LONDON); 566 assertEquals(expected, result); 567 568 result = test.plusWeeks(0); 569 assertSame(test, result); 570 } 571 572 public void testPlusDays_int() { 573 LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30, 40, BUDDHIST_LONDON); 574 LocalDateTime result = test.plusDays(1); 575 LocalDateTime expected = new LocalDateTime(2002, 5, 4, 10, 20, 30, 40, BUDDHIST_LONDON); 576 assertEquals(expected, result); 577 578 result = test.plusDays(0); 579 assertSame(test, result); 580 } 581 582 public void testPlusHours_int() { 583 LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30, 40, BUDDHIST_LONDON); 584 LocalDateTime result = test.plusHours(1); 585 LocalDateTime expected = new LocalDateTime(2002, 5, 3, 11, 20, 30, 40, BUDDHIST_LONDON); 586 assertEquals(expected, result); 587 588 result = test.plusHours(0); 589 assertSame(test, result); 590 } 591 592 public void testPlusMinutes_int() { 593 LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30, 40, BUDDHIST_LONDON); 594 LocalDateTime result = test.plusMinutes(1); 595 LocalDateTime expected = new LocalDateTime(2002, 5, 3, 10, 21, 30, 40, BUDDHIST_LONDON); 596 assertEquals(expected, result); 597 598 result = test.plusMinutes(0); 599 assertSame(test, result); 600 } 601 602 public void testPlusSeconds_int() { 603 LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30, 40, BUDDHIST_LONDON); 604 LocalDateTime result = test.plusSeconds(1); 605 LocalDateTime expected = new LocalDateTime(2002, 5, 3, 10, 20, 31, 40, BUDDHIST_LONDON); 606 assertEquals(expected, result); 607 608 result = test.plusSeconds(0); 609 assertSame(test, result); 610 } 611 612 public void testPlusMillis_int() { 613 LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30, 40, BUDDHIST_LONDON); 614 LocalDateTime result = test.plusMillis(1); 615 LocalDateTime expected = new LocalDateTime(2002, 5, 3, 10, 20, 30, 41, BUDDHIST_LONDON); 616 assertEquals(expected, result); 617 618 result = test.plusMillis(0); 619 assertSame(test, result); 620 } 621 622 //----------------------------------------------------------------------- 623 public void testMinus_RP() { 624 LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30, 40, BUDDHIST_LONDON); 625 LocalDateTime result = test.minus(new Period(1, 1, 1, 1, 1, 1, 1, 1)); 626 627 LocalDateTime expected = new LocalDateTime(2001, 3, 26, 9, 19, 29, 39, BUDDHIST_LONDON); 628 assertEquals(expected, result); 629 630 result = test.minus((ReadablePeriod) null); 631 assertSame(test, result); 632 } 633 634 public void testMinusYears_int() { 635 LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30, 40, BUDDHIST_LONDON); 636 LocalDateTime result = test.minusYears(1); 637 LocalDateTime expected = new LocalDateTime(2001, 5, 3, 10, 20, 30, 40, BUDDHIST_LONDON); 638 assertEquals(expected, result); 639 640 result = test.minusYears(0); 641 assertSame(test, result); 642 } 643 644 public void testMinusMonths_int() { 645 LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30, 40, BUDDHIST_LONDON); 646 LocalDateTime result = test.minusMonths(1); 647 LocalDateTime expected = new LocalDateTime(2002, 4, 3, 10, 20, 30, 40, BUDDHIST_LONDON); 648 assertEquals(expected, result); 649 650 result = test.minusMonths(0); 651 assertSame(test, result); 652 } 653 654 public void testMinusWeeks_int() { 655 LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30, 40, BUDDHIST_LONDON); 656 LocalDateTime result = test.minusWeeks(1); 657 LocalDateTime expected = new LocalDateTime(2002, 4, 26, 10, 20, 30, 40, BUDDHIST_LONDON); 658 assertEquals(expected, result); 659 660 result = test.minusWeeks(0); 661 assertSame(test, result); 662 } 663 664 public void testMinusDays_int() { 665 LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30, 40, BUDDHIST_LONDON); 666 LocalDateTime result = test.minusDays(1); 667 LocalDateTime expected = new LocalDateTime(2002, 5, 2, 10, 20, 30, 40, BUDDHIST_LONDON); 668 assertEquals(expected, result); 669 670 result = test.minusDays(0); 671 assertSame(test, result); 672 } 673 674 public void testMinusHours_int() { 675 LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30, 40, BUDDHIST_LONDON); 676 LocalDateTime result = test.minusHours(1); 677 LocalDateTime expected = new LocalDateTime(2002, 5, 3, 9, 20, 30, 40, BUDDHIST_LONDON); 678 assertEquals(expected, result); 679 680 result = test.minusHours(0); 681 assertSame(test, result); 682 } 683 684 public void testMinusMinutes_int() { 685 LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30, 40, BUDDHIST_LONDON); 686 LocalDateTime result = test.minusMinutes(1); 687 LocalDateTime expected = new LocalDateTime(2002, 5, 3, 10, 19, 30, 40, BUDDHIST_LONDON); 688 assertEquals(expected, result); 689 690 result = test.minusMinutes(0); 691 assertSame(test, result); 692 } 693 694 public void testMinusSeconds_int() { 695 LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30, 40, BUDDHIST_LONDON); 696 LocalDateTime result = test.minusSeconds(1); 697 LocalDateTime expected = new LocalDateTime(2002, 5, 3, 10, 20, 29, 40, BUDDHIST_LONDON); 698 assertEquals(expected, result); 699 700 result = test.minusSeconds(0); 701 assertSame(test, result); 702 } 703 704 public void testMinusMillis_int() { 705 LocalDateTime test = new LocalDateTime(2002, 5, 3, 10, 20, 30, 40, BUDDHIST_LONDON); 706 LocalDateTime result = test.minusMillis(1); 707 LocalDateTime expected = new LocalDateTime(2002, 5, 3, 10, 20, 30, 39, BUDDHIST_LONDON); 708 assertEquals(expected, result); 709 710 result = test.minusMillis(0); 711 assertSame(test, result); 712 } 713 714 //----------------------------------------------------------------------- 715 public void testGetters() { 716 LocalDateTime test = new LocalDateTime(1970, 6, 9, 10, 20, 30, 40, GJ_UTC); 717 assertEquals(1970, test.getYear()); 718 assertEquals(6, test.getMonthOfYear()); 719 assertEquals(9, test.getDayOfMonth()); 720 assertEquals(160, test.getDayOfYear()); 721 assertEquals(2, test.getDayOfWeek()); 722 assertEquals(24, test.getWeekOfWeekyear()); 723 assertEquals(1970, test.getWeekyear()); 724 assertEquals(70, test.getYearOfCentury()); 725 assertEquals(20, test.getCenturyOfEra()); 726 assertEquals(1970, test.getYearOfEra()); 727 assertEquals(DateTimeConstants.AD, test.getEra()); 728 assertEquals(10, test.getHourOfDay()); 729 assertEquals(20, test.getMinuteOfHour()); 730 assertEquals(30, test.getSecondOfMinute()); 731 assertEquals(40, test.getMillisOfSecond()); 732 assertEquals(MILLIS_OF_DAY_UTC, test.getMillisOfDay()); 733 } 734 735 //----------------------------------------------------------------------- 736 public void testWithers() { 737 LocalDateTime test = new LocalDateTime(1970, 6, 9, 10, 20, 30, 40, GJ_UTC); 738 check(test.withYear(2000), 2000, 6, 9, 10, 20, 30, 40); 739 check(test.withMonthOfYear(2), 1970, 2, 9, 10, 20, 30, 40); 740 check(test.withDayOfMonth(2), 1970, 6, 2, 10, 20, 30, 40); 741 check(test.withDayOfYear(6), 1970, 1, 6, 10, 20, 30, 40); 742 check(test.withDayOfWeek(6), 1970, 6, 13, 10, 20, 30, 40); 743 check(test.withWeekOfWeekyear(6), 1970, 2, 3, 10, 20, 30, 40); 744 check(test.withWeekyear(1971), 1971, 6, 15, 10, 20, 30, 40); 745 check(test.withYearOfCentury(60), 1960, 6, 9, 10, 20, 30, 40); 746 check(test.withCenturyOfEra(21), 2070, 6, 9, 10, 20, 30, 40); 747 check(test.withYearOfEra(1066), 1066, 6, 9, 10, 20, 30, 40); 748 check(test.withEra(DateTimeConstants.BC), -1970, 6, 9, 10, 20, 30, 40); 749 check(test.withHourOfDay(6), 1970, 6, 9, 6, 20, 30, 40); 750 check(test.withMinuteOfHour(6), 1970, 6, 9, 10, 6, 30, 40); 751 check(test.withSecondOfMinute(6), 1970, 6, 9, 10, 20, 6, 40); 752 check(test.withMillisOfSecond(6), 1970, 6, 9, 10, 20, 30, 6); 753 check(test.withMillisOfDay(61234), 1970, 6, 9, 0, 1, 1, 234); 754 try { 755 test.withMonthOfYear(0); 756 fail(); 757 } catch (IllegalArgumentException ex) {} 758 try { 759 test.withMonthOfYear(13); 760 fail(); 761 } catch (IllegalArgumentException ex) {} 762 } 763 764 //----------------------------------------------------------------------- 765 public void testToDateTime() { 766 LocalDateTime base = new LocalDateTime(2005, 6, 9, 6, 7, 8, 9, COPTIC_PARIS); // PARIS irrelevant 767 768 DateTime test = base.toDateTime(); 769 check(base, 2005, 6, 9, 6, 7, 8, 9); 770 DateTime expected = new DateTime(2005, 6, 9, 6, 7, 8, 9, COPTIC_LONDON); 771 assertEquals(expected, test); 772 } 773 774 //----------------------------------------------------------------------- 775 public void testToDateTime_Zone() { 776 LocalDateTime base = new LocalDateTime(2005, 6, 9, 6, 7, 8, 9, COPTIC_PARIS); // PARIS irrelevant 777 778 DateTime test = base.toDateTime(TOKYO); 779 check(base, 2005, 6, 9, 6, 7, 8, 9); 780 DateTime expected = new DateTime(2005, 6, 9, 6, 7, 8, 9, COPTIC_TOKYO); 781 assertEquals(expected, test); 782 } 783 784 public void testToDateTime_nullZone() { 785 LocalDateTime base = new LocalDateTime(2005, 6, 9, 6, 7, 8, 9, COPTIC_PARIS); // PARIS irrelevant 786 787 DateTime test = base.toDateTime((DateTimeZone) null); 788 check(base, 2005, 6, 9, 6, 7, 8, 9); 789 DateTime expected = new DateTime(2005, 6, 9, 6, 7, 8, 9, COPTIC_LONDON); 790 assertEquals(expected, test); 791 } 792 793 //----------------------------------------------------------------------- 794 public void testToLocalDate() { 795 LocalDateTime base = new LocalDateTime(2005, 6, 9, 6, 7, 8, 9, COPTIC_PARIS); // PARIS irrelevant 796 LocalDate expected = new LocalDate(2005, 6, 9, COPTIC_LONDON); 797 assertEquals(expected,base.toLocalDate()); 798 } 799 800 public void testToLocalTime() { 801 LocalDateTime base = new LocalDateTime(2005, 6, 9, 6, 7, 8, 9, COPTIC_PARIS); // PARIS irrelevant 802 LocalTime expected = new LocalTime(6, 7, 8, 9, COPTIC_LONDON); 803 assertEquals(expected,base.toLocalTime()); 804 } 805 806 //----------------------------------------------------------------------- 807 public void testToDateTime_RI() { 808 LocalDateTime base = new LocalDateTime(2005, 6, 9, 10, 20, 30, 40, COPTIC_PARIS); 809 DateTime dt = new DateTime(2002, 1, 3, 4, 5, 6, 7, BUDDHIST_TOKYO); 810 811 DateTime test = base.toDateTime(dt); 812 check(base, 2005, 6, 9, 10, 20, 30, 40); 813 DateTime expected = new DateTime(2005, 6, 9, 10, 20, 30, 40, BUDDHIST_TOKYO); 814 assertEquals(expected, test); 815 } 816 817 public void testToDateTime_nullRI() { 818 LocalDateTime base = new LocalDateTime(2005, 6, 9, 10, 20, 30, 40, COPTIC_PARIS); 819 820 DateTime test = base.toDateTime((ReadableInstant) null); 821 check(base, 2005, 6, 9, 10, 20, 30, 40); 822 DateTime expected = new DateTime(2005, 6, 9, 10, 20, 30, 40, ISO_LONDON); 823 assertEquals(expected, test); 824 } 825 826 //----------------------------------------------------------------------- 827 public void testToDate_summer() { 828 LocalDateTime base = new LocalDateTime(2005, 7, 9, 10, 20, 30, 40, COPTIC_PARIS); 829 830 Date test = base.toDate(); 831 check(base, 2005, 7, 9, 10, 20, 30, 40); 832 833 GregorianCalendar gcal = new GregorianCalendar(); 834 gcal.clear(); 835 gcal.set(Calendar.YEAR, 2005); 836 gcal.set(Calendar.MONTH, Calendar.JULY); 837 gcal.set(Calendar.DAY_OF_MONTH, 9); 838 gcal.set(Calendar.HOUR_OF_DAY, 10); 839 gcal.set(Calendar.MINUTE, 20); 840 gcal.set(Calendar.SECOND, 30); 841 gcal.set(Calendar.MILLISECOND, 40); 842 assertEquals(gcal.getTime(), test); 843 } 844 845 public void testToDate_winter() { 846 LocalDateTime base = new LocalDateTime(2005, 1, 9, 10, 20, 30, 40, COPTIC_PARIS); 847 848 Date test = base.toDate(); 849 check(base, 2005, 1, 9, 10, 20, 30, 40); 850 851 GregorianCalendar gcal = new GregorianCalendar(); 852 gcal.clear(); 853 gcal.set(Calendar.YEAR, 2005); 854 gcal.set(Calendar.MONTH, Calendar.JANUARY); 855 gcal.set(Calendar.DAY_OF_MONTH, 9); 856 gcal.set(Calendar.HOUR_OF_DAY, 10); 857 gcal.set(Calendar.MINUTE, 20); 858 gcal.set(Calendar.SECOND, 30); 859 gcal.set(Calendar.MILLISECOND, 40); 860 assertEquals(gcal.getTime(), test); 861 } 862 863 public void testToDate_springDST() { 864 LocalDateTime base = new LocalDateTime(2007, 4, 2, 0, 20, 0, 0); 865 866 SimpleTimeZone testZone = new SimpleTimeZone(3600000, "NoMidnight", 867 Calendar.APRIL, 2, 0, 0, Calendar.OCTOBER, 2, 0, 3600000); 868 TimeZone currentZone = TimeZone.getDefault(); 869 try { 870 TimeZone.setDefault(testZone); 871 Date test = base.toDate(); 872 check(base, 2007, 4, 2, 0, 20, 0, 0); 873 assertEquals("Mon Apr 02 01:00:00 GMT+02:00 2007", test.toString()); 874 } finally { 875 TimeZone.setDefault(currentZone); 876 } 877 } 878 879 public void testToDate_springDST_2Hour40Savings() { 880 LocalDateTime base = new LocalDateTime(2007, 4, 2, 0, 20, 0, 0); 881 882 SimpleTimeZone testZone = new SimpleTimeZone(3600000, "NoMidnight", 883 Calendar.APRIL, 2, 0, 0, Calendar.OCTOBER, 2, 0, 3600000, (3600000 / 6) * 16); 884 TimeZone currentZone = TimeZone.getDefault(); 885 try { 886 TimeZone.setDefault(testZone); 887 Date test = base.toDate(); 888 check(base, 2007, 4, 2, 0, 20, 0, 0); 889 assertEquals("Mon Apr 02 02:40:00 GMT+03:40 2007", test.toString()); 890 } finally { 891 TimeZone.setDefault(currentZone); 892 } 893 } 894 895 public void testToDate_autumnDST() { 896 LocalDateTime base = new LocalDateTime(2007, 10, 2, 0, 20, 30, 0); 897 898 SimpleTimeZone testZone = new SimpleTimeZone(3600000, "NoMidnight", 899 Calendar.APRIL, 2, 0, 0, Calendar.OCTOBER, 2, 0, 3600000); 900 TimeZone currentZone = TimeZone.getDefault(); 901 try { 902 TimeZone.setDefault(testZone); 903 Date test = base.toDate(); 904 check(base, 2007, 10, 2, 0, 20, 30, 0); 905 assertEquals("Tue Oct 02 00:20:30 GMT+02:00 2007", test.toString()); 906 } finally { 907 TimeZone.setDefault(currentZone); 908 } 909 } 910 911 //----------------------------------------------------------------------- 912 public void testProperty() { 913 LocalDateTime test = new LocalDateTime(2005, 6, 9, 10, 20, 30, 40, GJ_UTC); 914 assertEquals(test.year(), test.property(DateTimeFieldType.year())); 915 assertEquals(test.monthOfYear(), test.property(DateTimeFieldType.monthOfYear())); 916 assertEquals(test.dayOfMonth(), test.property(DateTimeFieldType.dayOfMonth())); 917 assertEquals(test.dayOfWeek(), test.property(DateTimeFieldType.dayOfWeek())); 918 assertEquals(test.dayOfYear(), test.property(DateTimeFieldType.dayOfYear())); 919 assertEquals(test.weekOfWeekyear(), test.property(DateTimeFieldType.weekOfWeekyear())); 920 assertEquals(test.weekyear(), test.property(DateTimeFieldType.weekyear())); 921 assertEquals(test.yearOfCentury(), test.property(DateTimeFieldType.yearOfCentury())); 922 assertEquals(test.yearOfEra(), test.property(DateTimeFieldType.yearOfEra())); 923 assertEquals(test.centuryOfEra(), test.property(DateTimeFieldType.centuryOfEra())); 924 assertEquals(test.era(), test.property(DateTimeFieldType.era())); 925 assertEquals(test.hourOfDay(), test.property(DateTimeFieldType.hourOfDay())); 926 assertEquals(test.minuteOfHour(), test.property(DateTimeFieldType.minuteOfHour())); 927 assertEquals(test.secondOfMinute(), test.property(DateTimeFieldType.secondOfMinute())); 928 assertEquals(test.millisOfSecond(), test.property(DateTimeFieldType.millisOfSecond())); 929 assertEquals(test.millisOfDay(), test.property(DateTimeFieldType.millisOfDay())); 930 931 try { 932 test.property(null); 933 fail(); 934 } catch (IllegalArgumentException ex) {} 935 assertEquals(test, test.property(DateTimeFieldType.minuteOfDay()).getLocalDateTime()); 936 } 937 938 //----------------------------------------------------------------------- 939 public void testSerialization() throws Exception { 940 LocalDateTime test = new LocalDateTime(1972, 6, 9, 10, 20, 30, 40, COPTIC_PARIS); 941 942 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 943 ObjectOutputStream oos = new ObjectOutputStream(baos); 944 oos.writeObject(test); 945 byte[] bytes = baos.toByteArray(); 946 oos.close(); 947 948 ByteArrayInputStream bais = new ByteArrayInputStream(bytes); 949 ObjectInputStream ois = new ObjectInputStream(bais); 950 LocalDateTime result = (LocalDateTime) ois.readObject(); 951 ois.close(); 952 953 assertEquals(test, result); 954 assertTrue(Arrays.equals(test.getValues(), result.getValues())); 955 assertTrue(Arrays.equals(test.getFields(), result.getFields())); 956 assertEquals(test.getChronology(), result.getChronology()); 957 assertTrue(result.isSupported(DateTimeFieldType.dayOfMonth())); // check deserialization 958 } 959 960 //----------------------------------------------------------------------- 961 public void testToString() { 962 LocalDateTime test = new LocalDateTime(2002, 6, 9, 10, 20, 30, 40); 963 assertEquals("2002-06-09T10:20:30.040", test.toString()); 964 } 965 966 //----------------------------------------------------------------------- 967 public void testToString_String() { 968 LocalDateTime test = new LocalDateTime(2002, 6, 9, 10, 20, 30, 40); 969 assertEquals("2002 10", test.toString("yyyy HH")); 970 assertEquals("2002-06-09T10:20:30.040", test.toString((String) null)); 971 } 972 973 //----------------------------------------------------------------------- 974 public void testToString_String_Locale() { 975 LocalDateTime test = new LocalDateTime(1970, 6, 9, 10, 20, 30, 40); 976 assertEquals("Tue 9/6", test.toString("EEE d/M", Locale.ENGLISH)); 977 assertEquals("mar. 9/6", test.toString("EEE d/M", Locale.FRENCH)); 978 assertEquals("1970-06-09T10:20:30.040", test.toString(null, Locale.ENGLISH)); 979 assertEquals("Tue 9/6", test.toString("EEE d/M", null)); 980 assertEquals("1970-06-09T10:20:30.040", test.toString(null, null)); 981 } 982 983 //----------------------------------------------------------------------- 984 public void testToString_DTFormatter() { 985 LocalDateTime test = new LocalDateTime(2002, 6, 9, 10, 20, 30, 40); 986 assertEquals("2002 10", test.toString(DateTimeFormat.forPattern("yyyy HH"))); 987 assertEquals("2002-06-09T10:20:30.040", test.toString((DateTimeFormatter) null)); 988 } 989 990 //----------------------------------------------------------------------- 991 private void check(LocalDateTime test, int year, int month, int day, int hour, int min, int sec, int mil) { 992 assertEquals(year, test.getYear()); 993 assertEquals(month, test.getMonthOfYear()); 994 assertEquals(day, test.getDayOfMonth()); 995 assertEquals(hour, test.getHourOfDay()); 996 assertEquals(min, test.getMinuteOfHour()); 997 assertEquals(sec, test.getSecondOfMinute()); 998 assertEquals(mil, test.getMillisOfSecond()); 999 } 1000 }