54 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 55 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 56 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 57 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 58 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 59 */ 60 package tck.java.time; 61 62 import static java.time.Month.DECEMBER; 63 import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH; 64 import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR; 65 import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_MONTH; 66 import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR; 67 import static java.time.temporal.ChronoField.AMPM_OF_DAY; 68 import static java.time.temporal.ChronoField.CLOCK_HOUR_OF_AMPM; 69 import static java.time.temporal.ChronoField.CLOCK_HOUR_OF_DAY; 70 import static java.time.temporal.ChronoField.DAY_OF_MONTH; 71 import static java.time.temporal.ChronoField.DAY_OF_WEEK; 72 import static java.time.temporal.ChronoField.DAY_OF_YEAR; 73 import static java.time.temporal.ChronoField.EPOCH_DAY; 74 import static java.time.temporal.ChronoField.EPOCH_MONTH; 75 import static java.time.temporal.ChronoField.ERA; 76 import static java.time.temporal.ChronoField.HOUR_OF_AMPM; 77 import static java.time.temporal.ChronoField.HOUR_OF_DAY; 78 import static java.time.temporal.ChronoField.INSTANT_SECONDS; 79 import static java.time.temporal.ChronoField.MICRO_OF_DAY; 80 import static java.time.temporal.ChronoField.MICRO_OF_SECOND; 81 import static java.time.temporal.ChronoField.MILLI_OF_DAY; 82 import static java.time.temporal.ChronoField.MILLI_OF_SECOND; 83 import static java.time.temporal.ChronoField.MINUTE_OF_DAY; 84 import static java.time.temporal.ChronoField.MINUTE_OF_HOUR; 85 import static java.time.temporal.ChronoField.MONTH_OF_YEAR; 86 import static java.time.temporal.ChronoField.NANO_OF_DAY; 87 import static java.time.temporal.ChronoField.NANO_OF_SECOND; 88 import static java.time.temporal.ChronoField.OFFSET_SECONDS; 89 import static java.time.temporal.ChronoField.SECOND_OF_DAY; 90 import static java.time.temporal.ChronoField.SECOND_OF_MINUTE; 91 import static java.time.temporal.ChronoField.YEAR; 92 import static java.time.temporal.ChronoField.YEAR_OF_ERA; 93 import static java.time.temporal.ChronoUnit.DAYS; 94 import static java.time.temporal.ChronoUnit.NANOS; 95 import static java.time.temporal.ChronoUnit.SECONDS; 96 import static org.testng.Assert.assertEquals; 97 import static org.testng.Assert.assertTrue; 98 99 import java.io.ByteArrayOutputStream; 100 import java.io.DataOutputStream; 101 import java.lang.reflect.Constructor; 102 import java.lang.reflect.InvocationTargetException; 103 import java.time.Clock; 104 import java.time.DateTimeException; 105 import java.time.Duration; 106 import java.time.Instant; 107 import java.time.LocalDate; 108 import java.time.LocalDateTime; 109 import java.time.LocalTime; 110 import java.time.Month; 111 import java.time.OffsetDateTime; 112 import java.time.OffsetTime; 113 import java.time.Year; 114 import java.time.ZoneId; 115 import java.time.ZoneOffset; 116 import java.time.ZonedDateTime; 117 import java.time.chrono.IsoChronology; 118 import java.time.format.DateTimeFormatter; 119 import java.time.format.DateTimeParseException; 120 import java.time.temporal.ChronoField; 121 import java.time.temporal.ChronoUnit; 122 import java.time.temporal.JulianFields; 123 import java.time.temporal.Queries; 124 import java.time.temporal.Temporal; 125 import java.time.temporal.TemporalAccessor; 126 import java.time.temporal.TemporalAdjuster; 127 import java.time.temporal.TemporalField; 128 import java.time.temporal.TemporalQuery; 129 import java.util.ArrayList; 130 import java.util.Arrays; 131 import java.util.List; 132 133 import org.testng.annotations.BeforeMethod; 134 import org.testng.annotations.DataProvider; 135 import org.testng.annotations.Test; 136 import test.java.time.MockSimplePeriod; 137 138 /** 139 * Test OffsetDateTime. 140 */ 141 @Test 142 public class TCKOffsetDateTime extends AbstractDateTimeTest { 143 144 private static final ZoneId ZONE_PARIS = ZoneId.of("Europe/Paris"); 145 private static final ZoneId ZONE_GAZA = ZoneId.of("Asia/Gaza"); 146 private static final ZoneOffset OFFSET_PONE = ZoneOffset.ofHours(1); 147 private static final ZoneOffset OFFSET_PTWO = ZoneOffset.ofHours(2); 148 private static final ZoneOffset OFFSET_MONE = ZoneOffset.ofHours(-1); 149 private static final ZoneOffset OFFSET_MTWO = ZoneOffset.ofHours(-2); 150 private OffsetDateTime TEST_2008_6_30_11_30_59_000000500; 151 152 @BeforeMethod(groups={"tck","implementation"}) 153 public void setUp() { 154 TEST_2008_6_30_11_30_59_000000500 = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59, 500), OFFSET_PONE); 155 } 156 157 //----------------------------------------------------------------------- 158 @Override 159 protected List<TemporalAccessor> samples() { 160 TemporalAccessor[] array = {TEST_2008_6_30_11_30_59_000000500, OffsetDateTime.MIN, OffsetDateTime.MAX}; 161 return Arrays.asList(array); 162 } 163 164 @Override 165 protected List<TemporalField> validFields() { 166 TemporalField[] array = { 167 NANO_OF_SECOND, 168 NANO_OF_DAY, 169 MICRO_OF_SECOND, 170 MICRO_OF_DAY, 171 MILLI_OF_SECOND, 172 MILLI_OF_DAY, 173 SECOND_OF_MINUTE, 174 SECOND_OF_DAY, 175 MINUTE_OF_HOUR, 176 MINUTE_OF_DAY, 177 CLOCK_HOUR_OF_AMPM, 178 HOUR_OF_AMPM, 179 CLOCK_HOUR_OF_DAY, 180 HOUR_OF_DAY, 181 AMPM_OF_DAY, 182 DAY_OF_WEEK, 183 ALIGNED_DAY_OF_WEEK_IN_MONTH, 184 ALIGNED_DAY_OF_WEEK_IN_YEAR, 185 DAY_OF_MONTH, 186 DAY_OF_YEAR, 187 EPOCH_DAY, 188 ALIGNED_WEEK_OF_MONTH, 189 ALIGNED_WEEK_OF_YEAR, 190 MONTH_OF_YEAR, 191 EPOCH_MONTH, 192 YEAR_OF_ERA, 193 YEAR, 194 ERA, 195 OFFSET_SECONDS, 196 INSTANT_SECONDS, 197 JulianFields.JULIAN_DAY, 198 JulianFields.MODIFIED_JULIAN_DAY, 199 JulianFields.RATA_DIE, 200 }; 201 return Arrays.asList(array); 202 } 203 204 @Override 205 protected List<TemporalField> invalidFields() { 206 List<TemporalField> list = new ArrayList<>(Arrays.<TemporalField>asList(ChronoField.values())); 207 list.removeAll(validFields()); 208 return list; 209 } 210 211 //----------------------------------------------------------------------- 244 LocalDateTime ldt = LocalDateTime.of(2012, 9, 16, 22, 17, 59, 464_000_000); 245 assertSerializedBySer(OffsetDateTime.of(ldt, ZoneOffset.ofHours(1)), bytes, bytesDateTime, bytesOffset); 246 } 247 248 //----------------------------------------------------------------------- 249 // constants 250 //----------------------------------------------------------------------- 251 @Test 252 public void constant_MIN() { 253 check(OffsetDateTime.MIN, Year.MIN_VALUE, 1, 1, 0, 0, 0, 0, ZoneOffset.MAX); 254 } 255 256 @Test 257 public void constant_MAX() { 258 check(OffsetDateTime.MAX, Year.MAX_VALUE, 12, 31, 23, 59, 59, 999999999, ZoneOffset.MIN); 259 } 260 261 //----------------------------------------------------------------------- 262 // now() 263 //----------------------------------------------------------------------- 264 @Test(groups={"tck"}) 265 public void now() { 266 OffsetDateTime expected = OffsetDateTime.now(Clock.systemDefaultZone()); 267 OffsetDateTime test = OffsetDateTime.now(); 268 long diff = Math.abs(test.toLocalTime().toNanoOfDay() - expected.toLocalTime().toNanoOfDay()); 269 if (diff >= 100000000) { 270 // may be date change 271 expected = OffsetDateTime.now(Clock.systemDefaultZone()); 272 test = OffsetDateTime.now(); 273 diff = Math.abs(test.toLocalTime().toNanoOfDay() - expected.toLocalTime().toNanoOfDay()); 274 } 275 assertTrue(diff < 100000000); // less than 0.1 secs 276 } 277 278 //----------------------------------------------------------------------- 279 // now(Clock) 280 //----------------------------------------------------------------------- 281 @Test(groups={"tck"}) 282 public void now_Clock_allSecsInDay_utc() { 283 for (int i = 0; i < (2 * 24 * 60 * 60); i++) { 284 Instant instant = Instant.ofEpochSecond(i).plusNanos(123456789L); 285 Clock clock = Clock.fixed(instant, ZoneOffset.UTC); 286 OffsetDateTime test = OffsetDateTime.now(clock); 287 assertEquals(test.getYear(), 1970); 288 assertEquals(test.getMonth(), Month.JANUARY); 289 assertEquals(test.getDayOfMonth(), (i < 24 * 60 * 60 ? 1 : 2)); 290 assertEquals(test.getHour(), (i / (60 * 60)) % 24); 291 assertEquals(test.getMinute(), (i / 60) % 60); 292 assertEquals(test.getSecond(), i % 60); 293 assertEquals(test.getNano(), 123456789); 294 assertEquals(test.getOffset(), ZoneOffset.UTC); 295 } 296 } 297 298 @Test(groups={"tck"}) 299 public void now_Clock_allSecsInDay_offset() { 300 for (int i = 0; i < (2 * 24 * 60 * 60); i++) { 301 Instant instant = Instant.ofEpochSecond(i).plusNanos(123456789L); 302 Clock clock = Clock.fixed(instant.minusSeconds(OFFSET_PONE.getTotalSeconds()), OFFSET_PONE); 303 OffsetDateTime test = OffsetDateTime.now(clock); 304 assertEquals(test.getYear(), 1970); 305 assertEquals(test.getMonth(), Month.JANUARY); 306 assertEquals(test.getDayOfMonth(), (i < 24 * 60 * 60) ? 1 : 2); 307 assertEquals(test.getHour(), (i / (60 * 60)) % 24); 308 assertEquals(test.getMinute(), (i / 60) % 60); 309 assertEquals(test.getSecond(), i % 60); 310 assertEquals(test.getNano(), 123456789); 311 assertEquals(test.getOffset(), OFFSET_PONE); 312 } 313 } 314 315 @Test(groups={"tck"}) 316 public void now_Clock_allSecsInDay_beforeEpoch() { 317 LocalTime expected = LocalTime.MIDNIGHT.plusNanos(123456789L); 318 for (int i =-1; i >= -(24 * 60 * 60); i--) { 319 Instant instant = Instant.ofEpochSecond(i).plusNanos(123456789L); 320 Clock clock = Clock.fixed(instant, ZoneOffset.UTC); 321 OffsetDateTime test = OffsetDateTime.now(clock); 322 assertEquals(test.getYear(), 1969); 323 assertEquals(test.getMonth(), Month.DECEMBER); 324 assertEquals(test.getDayOfMonth(), 31); 325 expected = expected.minusSeconds(1); 326 assertEquals(test.toLocalTime(), expected); 327 assertEquals(test.getOffset(), ZoneOffset.UTC); 328 } 329 } 330 331 @Test(groups={"tck"}) 332 public void now_Clock_offsets() { 333 OffsetDateTime base = OffsetDateTime.of(1970, 1, 1, 12, 0, 0, 0, ZoneOffset.UTC); 334 for (int i = -9; i < 15; i++) { 335 ZoneOffset offset = ZoneOffset.ofHours(i); 336 Clock clock = Clock.fixed(base.toInstant(), offset); 337 OffsetDateTime test = OffsetDateTime.now(clock); 338 assertEquals(test.getHour(), (12 + i) % 24); 339 assertEquals(test.getMinute(), 0); 340 assertEquals(test.getSecond(), 0); 341 assertEquals(test.getNano(), 0); 342 assertEquals(test.getOffset(), offset); 343 } 344 } 345 346 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 347 public void now_Clock_nullZoneId() { 348 OffsetDateTime.now((ZoneId) null); 349 } 350 351 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 352 public void now_Clock_nullClock() { 353 OffsetDateTime.now((Clock) null); 354 } 355 356 //----------------------------------------------------------------------- 357 private void check(OffsetDateTime test, int y, int mo, int d, int h, int m, int s, int n, ZoneOffset offset) { 358 assertEquals(test.getYear(), y); 359 assertEquals(test.getMonth().getValue(), mo); 360 assertEquals(test.getDayOfMonth(), d); 361 assertEquals(test.getHour(), h); 362 assertEquals(test.getMinute(), m); 363 assertEquals(test.getSecond(), s); 364 assertEquals(test.getNano(), n); 365 assertEquals(test.getOffset(), offset); 366 assertEquals(test, test); 367 assertEquals(test.hashCode(), test.hashCode()); 368 assertEquals(OffsetDateTime.of(LocalDateTime.of(y, mo, d, h, m, s, n), offset), test); 369 } 370 371 //----------------------------------------------------------------------- 372 // factories 373 //----------------------------------------------------------------------- 374 @Test(groups={"tck"}) 375 public void factory_of_intsHMSN() { 376 OffsetDateTime test = OffsetDateTime.of(2008, 6, 30, 11, 30, 10, 500, OFFSET_PONE); 377 check(test, 2008, 6, 30, 11, 30, 10, 500, OFFSET_PONE); 378 } 379 380 //----------------------------------------------------------------------- 381 @Test(groups={"tck"}) 382 public void factory_of_LocalDateLocalTimeZoneOffset() { 383 LocalDate date = LocalDate.of(2008, 6, 30); 384 LocalTime time = LocalTime.of(11, 30, 10, 500); 385 OffsetDateTime test = OffsetDateTime.of(date, time, OFFSET_PONE); 386 check(test, 2008, 6, 30, 11, 30, 10, 500, OFFSET_PONE); 387 } 388 389 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 390 public void factory_of_LocalDateLocalTimeZoneOffset_nullLocalDate() { 391 LocalTime time = LocalTime.of(11, 30, 10, 500); 392 OffsetDateTime.of((LocalDate) null, time, OFFSET_PONE); 393 } 394 395 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 396 public void factory_of_LocalDateLocalTimeZoneOffset_nullLocalTime() { 397 LocalDate date = LocalDate.of(2008, 6, 30); 398 OffsetDateTime.of(date, (LocalTime) null, OFFSET_PONE); 399 } 400 401 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 402 public void factory_of_LocalDateLocalTimeZoneOffset_nullOffset() { 403 LocalDate date = LocalDate.of(2008, 6, 30); 404 LocalTime time = LocalTime.of(11, 30, 10, 500); 405 OffsetDateTime.of(date, time, (ZoneOffset) null); 406 } 407 408 //----------------------------------------------------------------------- 409 @Test(groups={"tck"}) 410 public void factory_of_LocalDateTimeZoneOffset() { 411 LocalDateTime dt = LocalDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 10, 500)); 412 OffsetDateTime test = OffsetDateTime.of(dt, OFFSET_PONE); 413 check(test, 2008, 6, 30, 11, 30, 10, 500, OFFSET_PONE); 414 } 415 416 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 417 public void factory_of_LocalDateTimeZoneOffset_nullProvider() { 418 OffsetDateTime.of((LocalDateTime) null, OFFSET_PONE); 419 } 420 421 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 422 public void factory_of_LocalDateTimeZoneOffset_nullOffset() { 423 LocalDateTime dt = LocalDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 10, 500)); 424 OffsetDateTime.of(dt, (ZoneOffset) null); 425 } 426 427 //----------------------------------------------------------------------- 428 // from() 429 //----------------------------------------------------------------------- 430 @Test(groups={"tck"}) 431 public void test_factory_CalendricalObject() { 432 assertEquals(OffsetDateTime.from( 433 OffsetDateTime.of(LocalDate.of(2007, 7, 15), LocalTime.of(17, 30), OFFSET_PONE)), 434 OffsetDateTime.of(LocalDate.of(2007, 7, 15), LocalTime.of(17, 30), OFFSET_PONE)); 435 } 436 437 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 438 public void test_factory_CalendricalObject_invalid_noDerive() { 439 OffsetDateTime.from(LocalTime.of(12, 30)); 440 } 441 442 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 443 public void test_factory_Calendricals_null() { 444 OffsetDateTime.from((TemporalAccessor) null); 445 } 446 447 //----------------------------------------------------------------------- 448 // parse() 449 //----------------------------------------------------------------------- 450 @Test(dataProvider="sampleToString", groups={"tck"}) 451 public void test_parse(int y, int month, int d, int h, int m, int s, int n, String offsetId, String text) { 452 OffsetDateTime t = OffsetDateTime.parse(text); 453 assertEquals(t.getYear(), y); 454 assertEquals(t.getMonth().getValue(), month); 455 assertEquals(t.getDayOfMonth(), d); 456 assertEquals(t.getHour(), h); 457 assertEquals(t.getMinute(), m); 458 assertEquals(t.getSecond(), s); 459 assertEquals(t.getNano(), n); 460 assertEquals(t.getOffset().getId(), offsetId); 461 } 462 463 @Test(expectedExceptions=DateTimeParseException.class, groups={"tck"}) 464 public void factory_parse_illegalValue() { 465 OffsetDateTime.parse("2008-06-32T11:15+01:00"); 466 } 467 468 @Test(expectedExceptions=DateTimeParseException.class, groups={"tck"}) 469 public void factory_parse_invalidValue() { 470 OffsetDateTime.parse("2008-06-31T11:15+01:00"); 471 } 472 473 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 474 public void factory_parse_nullText() { 475 OffsetDateTime.parse((String) null); 476 } 477 478 //----------------------------------------------------------------------- 479 // parse(DateTimeFormatter) 480 //----------------------------------------------------------------------- 481 @Test(groups={"tck"}) 482 public void factory_parse_formatter() { 483 DateTimeFormatter f = DateTimeFormatter.ofPattern("y M d H m s XXX"); 484 OffsetDateTime test = OffsetDateTime.parse("2010 12 3 11 30 0 +01:00", f); 485 assertEquals(test, OffsetDateTime.of(LocalDate.of(2010, 12, 3), LocalTime.of(11, 30), ZoneOffset.ofHours(1))); 486 } 487 488 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 489 public void factory_parse_formatter_nullText() { 490 DateTimeFormatter f = DateTimeFormatter.ofPattern("y M d H m s"); 491 OffsetDateTime.parse((String) null, f); 492 } 493 494 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 495 public void factory_parse_formatter_nullFormatter() { 496 OffsetDateTime.parse("ANY", null); 497 } 498 499 //----------------------------------------------------------------------- 500 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 501 public void constructor_nullTime() throws Throwable { 502 Constructor<OffsetDateTime> con = OffsetDateTime.class.getDeclaredConstructor(LocalDateTime.class, ZoneOffset.class); 503 con.setAccessible(true); 504 try { 505 con.newInstance(null, OFFSET_PONE); 506 } catch (InvocationTargetException ex) { 507 throw ex.getCause(); 508 } 509 } 510 511 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 512 public void constructor_nullOffset() throws Throwable { 513 Constructor<OffsetDateTime> con = OffsetDateTime.class.getDeclaredConstructor(LocalDateTime.class, ZoneOffset.class); 514 con.setAccessible(true); 515 try { 516 con.newInstance(LocalDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30)), null); 517 } catch (InvocationTargetException ex) { 518 throw ex.getCause(); 519 } 520 } 521 522 //----------------------------------------------------------------------- 523 // basics 524 //----------------------------------------------------------------------- 525 @DataProvider(name="sampleTimes") 526 Object[][] provider_sampleTimes() { 527 return new Object[][] { 528 {2008, 6, 30, 11, 30, 20, 500, OFFSET_PONE}, 529 {2008, 6, 30, 11, 0, 0, 0, OFFSET_PONE}, 530 {2008, 6, 30, 23, 59, 59, 999999999, OFFSET_PONE}, 531 {-1, 1, 1, 0, 0, 0, 0, OFFSET_PONE}, 532 }; 533 } 534 535 @Test(dataProvider="sampleTimes", groups={"tck"}) 536 public void test_get(int y, int o, int d, int h, int m, int s, int n, ZoneOffset offset) { 537 LocalDate localDate = LocalDate.of(y, o, d); 538 LocalTime localTime = LocalTime.of(h, m, s, n); 539 LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime); 540 OffsetDateTime a = OffsetDateTime.of(localDateTime, offset); 541 542 assertEquals(a.getYear(), localDate.getYear()); 543 assertEquals(a.getMonth(), localDate.getMonth()); 544 assertEquals(a.getDayOfMonth(), localDate.getDayOfMonth()); 545 assertEquals(a.getDayOfYear(), localDate.getDayOfYear()); 546 assertEquals(a.getDayOfWeek(), localDate.getDayOfWeek()); 547 548 assertEquals(a.getHour(), localDateTime.getHour()); 549 assertEquals(a.getMinute(), localDateTime.getMinute()); 550 assertEquals(a.getSecond(), localDateTime.getSecond()); 551 assertEquals(a.getNano(), localDateTime.getNano()); 552 553 assertEquals(a.toOffsetTime(), OffsetTime.of(localTime, offset)); 554 assertEquals(a.toString(), localDateTime.toString() + offset.toString()); 555 } 585 assertEquals(test.getLong(ChronoField.DAY_OF_WEEK), 1); 586 assertEquals(test.getLong(ChronoField.DAY_OF_YEAR), 182); 587 588 assertEquals(test.getLong(ChronoField.HOUR_OF_DAY), 12); 589 assertEquals(test.getLong(ChronoField.MINUTE_OF_HOUR), 30); 590 assertEquals(test.getLong(ChronoField.SECOND_OF_MINUTE), 40); 591 assertEquals(test.getLong(ChronoField.NANO_OF_SECOND), 987654321); 592 assertEquals(test.getLong(ChronoField.HOUR_OF_AMPM), 0); 593 assertEquals(test.getLong(ChronoField.AMPM_OF_DAY), 1); 594 595 assertEquals(test.getLong(ChronoField.INSTANT_SECONDS), test.toEpochSecond()); 596 assertEquals(test.getLong(ChronoField.OFFSET_SECONDS), 3600); 597 } 598 599 //----------------------------------------------------------------------- 600 // query(TemporalQuery) 601 //----------------------------------------------------------------------- 602 @DataProvider(name="query") 603 Object[][] data_query() { 604 return new Object[][] { 605 {TEST_2008_6_30_11_30_59_000000500, Queries.chronology(), IsoChronology.INSTANCE}, 606 {TEST_2008_6_30_11_30_59_000000500, Queries.zoneId(), null}, 607 {TEST_2008_6_30_11_30_59_000000500, Queries.precision(), ChronoUnit.NANOS}, 608 {TEST_2008_6_30_11_30_59_000000500, Queries.zone(), OFFSET_PONE}, 609 {TEST_2008_6_30_11_30_59_000000500, Queries.offset(), OFFSET_PONE}, 610 {TEST_2008_6_30_11_30_59_000000500, Queries.localDate(), LocalDate.of(2008, 6, 30)}, 611 {TEST_2008_6_30_11_30_59_000000500, Queries.localTime(), LocalTime.of(11, 30, 59, 500)}, 612 }; 613 } 614 615 @Test(dataProvider="query") 616 public <T> void test_query(TemporalAccessor temporal, TemporalQuery<T> query, T expected) { 617 assertEquals(temporal.query(query), expected); 618 } 619 620 @Test(dataProvider="query") 621 public <T> void test_queryFrom(TemporalAccessor temporal, TemporalQuery<T> query, T expected) { 622 assertEquals(query.queryFrom(temporal), expected); 623 } 624 625 @Test(expectedExceptions=NullPointerException.class) 626 public void test_query_null() { 627 TEST_2008_6_30_11_30_59_000000500.query(null); 628 } 629 630 //----------------------------------------------------------------------- 631 // with(WithAdjuster) 632 //----------------------------------------------------------------------- 633 @Test(groups={"tck"}) 634 public void test_with_adjustment() { 635 final OffsetDateTime sample = OffsetDateTime.of(LocalDate.of(2012, 3, 4), LocalTime.of(23, 5), OFFSET_PONE); 636 TemporalAdjuster adjuster = new TemporalAdjuster() { 637 @Override 638 public Temporal adjustInto(Temporal dateTime) { 639 return sample; 640 } 641 }; 642 assertEquals(TEST_2008_6_30_11_30_59_000000500.with(adjuster), sample); 643 } 644 645 @Test(groups={"tck"}) 646 public void test_with_adjustment_LocalDate() { 647 OffsetDateTime test = TEST_2008_6_30_11_30_59_000000500.with(LocalDate.of(2012, 9, 3)); 648 assertEquals(test, OffsetDateTime.of(LocalDate.of(2012, 9, 3), LocalTime.of(11, 30, 59, 500), OFFSET_PONE)); 649 } 650 651 @Test(groups={"tck"}) 652 public void test_with_adjustment_LocalTime() { 653 OffsetDateTime test = TEST_2008_6_30_11_30_59_000000500.with(LocalTime.of(19, 15)); 654 assertEquals(test, OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(19, 15), OFFSET_PONE)); 655 } 656 657 @Test(groups={"tck"}) 658 public void test_with_adjustment_LocalDateTime() { 659 OffsetDateTime test = TEST_2008_6_30_11_30_59_000000500.with(LocalDateTime.of(LocalDate.of(2012, 9, 3), LocalTime.of(19, 15))); 660 assertEquals(test, OffsetDateTime.of(LocalDate.of(2012, 9, 3), LocalTime.of(19, 15), OFFSET_PONE)); 661 } 662 663 @Test(groups={"tck"}) 664 public void test_with_adjustment_OffsetTime() { 665 OffsetDateTime test = TEST_2008_6_30_11_30_59_000000500.with(OffsetTime.of(LocalTime.of(19, 15), OFFSET_PTWO)); 666 assertEquals(test, OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(19, 15), OFFSET_PTWO)); 667 } 668 669 @Test(groups={"tck"}) 670 public void test_with_adjustment_OffsetDateTime() { 671 OffsetDateTime test = TEST_2008_6_30_11_30_59_000000500.with(OffsetDateTime.of(LocalDate.of(2012, 9, 3), LocalTime.of(19, 15), OFFSET_PTWO)); 672 assertEquals(test, OffsetDateTime.of(LocalDate.of(2012, 9, 3), LocalTime.of(19, 15), OFFSET_PTWO)); 673 } 674 675 @Test(groups={"tck"}) 676 public void test_with_adjustment_Month() { 677 OffsetDateTime test = TEST_2008_6_30_11_30_59_000000500.with(DECEMBER); 678 assertEquals(test, OffsetDateTime.of(LocalDate.of(2008, 12, 30),LocalTime.of(11, 30, 59, 500), OFFSET_PONE)); 679 } 680 681 @Test(groups={"tck"}) 682 public void test_with_adjustment_ZoneOffset() { 683 OffsetDateTime test = TEST_2008_6_30_11_30_59_000000500.with(OFFSET_PTWO); 684 assertEquals(test, OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59, 500), OFFSET_PTWO)); 685 } 686 687 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 688 public void test_with_adjustment_null() { 689 TEST_2008_6_30_11_30_59_000000500.with((TemporalAdjuster) null); 690 } 691 692 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 693 public void test_withOffsetSameLocal_null() { 694 OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE); 695 base.withOffsetSameLocal(null); 696 } 697 698 //----------------------------------------------------------------------- 699 // withOffsetSameInstant() 700 //----------------------------------------------------------------------- 701 @Test(groups={"tck"}) 702 public void test_withOffsetSameInstant() { 703 OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE); 704 OffsetDateTime test = base.withOffsetSameInstant(OFFSET_PTWO); 705 OffsetDateTime expected = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(12, 30, 59), OFFSET_PTWO); 706 assertEquals(test, expected); 707 } 708 709 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 710 public void test_withOffsetSameInstant_null() { 711 OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE); 712 base.withOffsetSameInstant(null); 713 } 714 715 //----------------------------------------------------------------------- 716 // withYear() 717 //----------------------------------------------------------------------- 718 @Test(groups={"tck"}) 719 public void test_withYear_normal() { 720 OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE); 721 OffsetDateTime test = base.withYear(2007); 722 assertEquals(test, OffsetDateTime.of(LocalDate.of(2007, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE)); 723 } 724 725 //----------------------------------------------------------------------- 726 // withMonth() 727 //----------------------------------------------------------------------- 728 @Test(groups={"tck"}) 729 public void test_withMonth_normal() { 730 OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE); 731 OffsetDateTime test = base.withMonth(1); 732 assertEquals(test, OffsetDateTime.of(LocalDate.of(2008, 1, 30), LocalTime.of(11, 30, 59), OFFSET_PONE)); 733 } 734 735 //----------------------------------------------------------------------- 736 // withDayOfMonth() 737 //----------------------------------------------------------------------- 738 @Test(groups={"tck"}) 739 public void test_withDayOfMonth_normal() { 740 OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE); 741 OffsetDateTime test = base.withDayOfMonth(15); 742 assertEquals(test, OffsetDateTime.of(LocalDate.of(2008, 6, 15), LocalTime.of(11, 30, 59), OFFSET_PONE)); 743 } 744 745 //----------------------------------------------------------------------- 746 // withDayOfYear(int) 747 //----------------------------------------------------------------------- 748 @Test(groups={"tck"}) 749 public void test_withDayOfYear_normal() { 750 OffsetDateTime t = TEST_2008_6_30_11_30_59_000000500.withDayOfYear(33); 751 assertEquals(t, OffsetDateTime.of(LocalDate.of(2008, 2, 2), LocalTime.of(11, 30, 59, 500), OFFSET_PONE)); 752 } 753 754 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 755 public void test_withDayOfYear_illegal() { 756 TEST_2008_6_30_11_30_59_000000500.withDayOfYear(367); 757 } 758 759 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 760 public void test_withDayOfYear_invalid() { 761 OffsetDateTime.of(LocalDate.of(2007, 2, 2), LocalTime.of(11, 30), OFFSET_PONE).withDayOfYear(366); 762 } 763 764 //----------------------------------------------------------------------- 765 // withHour() 766 //----------------------------------------------------------------------- 767 @Test(groups={"tck"}) 768 public void test_withHour_normal() { 769 OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE); 770 OffsetDateTime test = base.withHour(15); 771 assertEquals(test, OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(15, 30, 59), OFFSET_PONE)); 772 } 773 774 //----------------------------------------------------------------------- 775 // withMinute() 776 //----------------------------------------------------------------------- 777 @Test(groups={"tck"}) 778 public void test_withMinute_normal() { 779 OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE); 780 OffsetDateTime test = base.withMinute(15); 781 assertEquals(test, OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 15, 59), OFFSET_PONE)); 782 } 783 784 //----------------------------------------------------------------------- 785 // withSecond() 786 //----------------------------------------------------------------------- 787 @Test(groups={"tck"}) 788 public void test_withSecond_normal() { 789 OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE); 790 OffsetDateTime test = base.withSecond(15); 791 assertEquals(test, OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 15), OFFSET_PONE)); 792 } 793 794 //----------------------------------------------------------------------- 795 // withNano() 796 //----------------------------------------------------------------------- 797 @Test(groups={"tck"}) 798 public void test_withNanoOfSecond_normal() { 799 OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59, 1), OFFSET_PONE); 800 OffsetDateTime test = base.withNano(15); 801 assertEquals(test, OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59, 15), OFFSET_PONE)); 802 } 803 804 //----------------------------------------------------------------------- 805 // truncatedTo(TemporalUnit) 806 //----------------------------------------------------------------------- 807 @Test(groups={"tck"}) 808 public void test_truncatedTo_normal() { 809 assertEquals(TEST_2008_6_30_11_30_59_000000500.truncatedTo(NANOS), TEST_2008_6_30_11_30_59_000000500); 810 assertEquals(TEST_2008_6_30_11_30_59_000000500.truncatedTo(SECONDS), TEST_2008_6_30_11_30_59_000000500.withNano(0)); 811 assertEquals(TEST_2008_6_30_11_30_59_000000500.truncatedTo(DAYS), TEST_2008_6_30_11_30_59_000000500.with(LocalTime.MIDNIGHT)); 812 } 813 814 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 815 public void test_truncatedTo_null() { 816 TEST_2008_6_30_11_30_59_000000500.truncatedTo(null); 817 } 818 819 //----------------------------------------------------------------------- 820 // plus(Period) 821 //----------------------------------------------------------------------- 822 @Test(groups={"tck"}) 823 public void test_plus_Period() { 824 MockSimplePeriod period = MockSimplePeriod.of(7, ChronoUnit.MONTHS); 825 OffsetDateTime t = TEST_2008_6_30_11_30_59_000000500.plus(period); 826 assertEquals(t, OffsetDateTime.of(2009, 1, 30, 11, 30, 59, 500, OFFSET_PONE)); 827 } 828 829 //----------------------------------------------------------------------- 830 // plus(Duration) 831 //----------------------------------------------------------------------- 832 @Test(groups={"tck"}) 833 public void test_plus_Duration() { 834 Duration dur = Duration.ofSeconds(62, 3); 835 OffsetDateTime t = TEST_2008_6_30_11_30_59_000000500.plus(dur); 836 assertEquals(t, OffsetDateTime.of(2008, 6, 30, 11, 32, 1, 503, OFFSET_PONE)); 837 } 838 839 @Test(groups={"tck"}) 840 public void test_plus_Duration_zero() { 841 OffsetDateTime t = TEST_2008_6_30_11_30_59_000000500.plus(Duration.ZERO); 842 assertEquals(t, TEST_2008_6_30_11_30_59_000000500); 843 } 844 845 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 846 public void test_plus_Duration_null() { 847 TEST_2008_6_30_11_30_59_000000500.plus((Duration) null); 848 } 849 850 //----------------------------------------------------------------------- 851 // plusYears() 852 //----------------------------------------------------------------------- 853 @Test(groups={"tck"}) 854 public void test_plusYears() { 855 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 856 OffsetDateTime test = base.plusYears(1); 857 assertEquals(test, OffsetDateTime.of(2009, 6, 30, 11, 30, 59, 0, OFFSET_PONE)); 858 } 859 860 //----------------------------------------------------------------------- 861 // plusMonths() 862 //----------------------------------------------------------------------- 863 @Test(groups={"tck"}) 864 public void test_plusMonths() { 865 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 866 OffsetDateTime test = base.plusMonths(1); 867 assertEquals(test, OffsetDateTime.of(2008, 7, 30, 11, 30, 59, 0, OFFSET_PONE)); 868 } 869 870 //----------------------------------------------------------------------- 871 // plusWeeks() 872 //----------------------------------------------------------------------- 873 @Test(groups={"tck"}) 874 public void test_plusWeeks() { 875 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 876 OffsetDateTime test = base.plusWeeks(1); 877 assertEquals(test, OffsetDateTime.of(2008, 7, 7, 11, 30, 59, 0, OFFSET_PONE)); 878 } 879 880 //----------------------------------------------------------------------- 881 // plusDays() 882 //----------------------------------------------------------------------- 883 @Test(groups={"tck"}) 884 public void test_plusDays() { 885 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 886 OffsetDateTime test = base.plusDays(1); 887 assertEquals(test, OffsetDateTime.of(2008, 7, 1, 11, 30, 59, 0, OFFSET_PONE)); 888 } 889 890 //----------------------------------------------------------------------- 891 // plusHours() 892 //----------------------------------------------------------------------- 893 @Test(groups={"tck"}) 894 public void test_plusHours() { 895 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 896 OffsetDateTime test = base.plusHours(13); 897 assertEquals(test, OffsetDateTime.of(2008, 7, 1, 0, 30, 59, 0, OFFSET_PONE)); 898 } 899 900 //----------------------------------------------------------------------- 901 // plusMinutes() 902 //----------------------------------------------------------------------- 903 @Test(groups={"tck"}) 904 public void test_plusMinutes() { 905 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 906 OffsetDateTime test = base.plusMinutes(30); 907 assertEquals(test, OffsetDateTime.of(2008, 6, 30, 12, 0, 59, 0, OFFSET_PONE)); 908 } 909 910 //----------------------------------------------------------------------- 911 // plusSeconds() 912 //----------------------------------------------------------------------- 913 @Test(groups={"tck"}) 914 public void test_plusSeconds() { 915 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 916 OffsetDateTime test = base.plusSeconds(1); 917 assertEquals(test, OffsetDateTime.of(2008, 6, 30, 11, 31, 0, 0, OFFSET_PONE)); 918 } 919 920 //----------------------------------------------------------------------- 921 // plusNanos() 922 //----------------------------------------------------------------------- 923 @Test(groups={"tck"}) 924 public void test_plusNanos() { 925 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 926 OffsetDateTime test = base.plusNanos(1); 927 assertEquals(test, OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 1, OFFSET_PONE)); 928 } 929 930 //----------------------------------------------------------------------- 931 // minus(Period) 932 //----------------------------------------------------------------------- 933 @Test(groups={"tck"}) 934 public void test_minus_Period() { 935 MockSimplePeriod period = MockSimplePeriod.of(7, ChronoUnit.MONTHS); 936 OffsetDateTime t = TEST_2008_6_30_11_30_59_000000500.minus(period); 937 assertEquals(t, OffsetDateTime.of(2007, 11, 30, 11, 30, 59, 500, OFFSET_PONE)); 938 } 939 940 //----------------------------------------------------------------------- 941 // minus(Duration) 942 //----------------------------------------------------------------------- 943 @Test(groups={"tck"}) 944 public void test_minus_Duration() { 945 Duration dur = Duration.ofSeconds(62, 3); 946 OffsetDateTime t = TEST_2008_6_30_11_30_59_000000500.minus(dur); 947 assertEquals(t, OffsetDateTime.of(2008, 6, 30, 11, 29, 57, 497, OFFSET_PONE)); 948 } 949 950 @Test(groups={"tck"}) 951 public void test_minus_Duration_zero() { 952 OffsetDateTime t = TEST_2008_6_30_11_30_59_000000500.minus(Duration.ZERO); 953 assertEquals(t, TEST_2008_6_30_11_30_59_000000500); 954 } 955 956 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 957 public void test_minus_Duration_null() { 958 TEST_2008_6_30_11_30_59_000000500.minus((Duration) null); 959 } 960 961 //----------------------------------------------------------------------- 962 // minusYears() 963 //----------------------------------------------------------------------- 964 @Test(groups={"tck"}) 965 public void test_minusYears() { 966 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 967 OffsetDateTime test = base.minusYears(1); 968 assertEquals(test, OffsetDateTime.of(2007, 6, 30, 11, 30, 59, 0, OFFSET_PONE)); 969 } 970 971 //----------------------------------------------------------------------- 972 // minusMonths() 973 //----------------------------------------------------------------------- 974 @Test(groups={"tck"}) 975 public void test_minusMonths() { 976 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 977 OffsetDateTime test = base.minusMonths(1); 978 assertEquals(test, OffsetDateTime.of(2008, 5, 30, 11, 30, 59, 0, OFFSET_PONE)); 979 } 980 981 //----------------------------------------------------------------------- 982 // minusWeeks() 983 //----------------------------------------------------------------------- 984 @Test(groups={"tck"}) 985 public void test_minusWeeks() { 986 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 987 OffsetDateTime test = base.minusWeeks(1); 988 assertEquals(test, OffsetDateTime.of(2008, 6, 23, 11, 30, 59, 0, OFFSET_PONE)); 989 } 990 991 //----------------------------------------------------------------------- 992 // minusDays() 993 //----------------------------------------------------------------------- 994 @Test(groups={"tck"}) 995 public void test_minusDays() { 996 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 997 OffsetDateTime test = base.minusDays(1); 998 assertEquals(test, OffsetDateTime.of(2008, 6, 29, 11, 30, 59, 0, OFFSET_PONE)); 999 } 1000 1001 //----------------------------------------------------------------------- 1002 // minusHours() 1003 //----------------------------------------------------------------------- 1004 @Test(groups={"tck"}) 1005 public void test_minusHours() { 1006 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 1007 OffsetDateTime test = base.minusHours(13); 1008 assertEquals(test, OffsetDateTime.of(2008, 6, 29, 22, 30, 59, 0, OFFSET_PONE)); 1009 } 1010 1011 //----------------------------------------------------------------------- 1012 // minusMinutes() 1013 //----------------------------------------------------------------------- 1014 @Test(groups={"tck"}) 1015 public void test_minusMinutes() { 1016 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 1017 OffsetDateTime test = base.minusMinutes(30); 1018 assertEquals(test, OffsetDateTime.of(2008, 6, 30, 11, 0, 59, 0, OFFSET_PONE)); 1019 } 1020 1021 //----------------------------------------------------------------------- 1022 // minusSeconds() 1023 //----------------------------------------------------------------------- 1024 @Test(groups={"tck"}) 1025 public void test_minusSeconds() { 1026 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 1027 OffsetDateTime test = base.minusSeconds(1); 1028 assertEquals(test, OffsetDateTime.of(2008, 6, 30, 11, 30, 58, 0, OFFSET_PONE)); 1029 } 1030 1031 //----------------------------------------------------------------------- 1032 // minusNanos() 1033 //----------------------------------------------------------------------- 1034 @Test(groups={"tck"}) 1035 public void test_minusNanos() { 1036 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 1037 OffsetDateTime test = base.minusNanos(1); 1038 assertEquals(test, OffsetDateTime.of(2008, 6, 30, 11, 30, 58, 999999999, OFFSET_PONE)); 1039 } 1040 1041 //----------------------------------------------------------------------- 1042 // atZoneSameInstant() 1043 //----------------------------------------------------------------------- 1044 @Test(groups={"tck"}) 1045 public void test_atZone() { 1046 OffsetDateTime t = OffsetDateTime.of(2008, 6, 30, 11, 30, 0, 0, OFFSET_MTWO); 1047 assertEquals(t.atZoneSameInstant(ZONE_PARIS), 1048 ZonedDateTime.of(2008, 6, 30, 15, 30, 0, 0, ZONE_PARIS)); 1049 } 1050 1051 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 1052 public void test_atZone_nullTimeZone() { 1053 OffsetDateTime t = OffsetDateTime.of(2008, 6, 30, 11, 30, 0, 0, OFFSET_PTWO); 1054 t.atZoneSameInstant((ZoneId) null); 1055 } 1056 1057 //----------------------------------------------------------------------- 1058 // atZoneSimilarLocal() 1059 //----------------------------------------------------------------------- 1060 @Test(groups={"tck"}) 1061 public void test_atZoneSimilarLocal() { 1062 OffsetDateTime t = OffsetDateTime.of(2008, 6, 30, 11, 30, 0, 0, OFFSET_MTWO); 1063 assertEquals(t.atZoneSimilarLocal(ZONE_PARIS), 1064 ZonedDateTime.of(2008, 6, 30, 11, 30, 0, 0, ZONE_PARIS)); 1065 } 1066 1067 @Test(groups={"tck"}) 1068 public void test_atZoneSimilarLocal_dstGap() { 1069 OffsetDateTime t = OffsetDateTime.of(2007, 4, 1, 0, 0, 0, 0, OFFSET_MTWO); 1070 assertEquals(t.atZoneSimilarLocal(ZONE_GAZA), 1071 ZonedDateTime.of(2007, 4, 1, 1, 0, 0, 0, ZONE_GAZA)); 1072 } 1073 1074 @Test(groups={"tck"}) 1075 public void test_atZone_dstOverlapSummer() { 1076 OffsetDateTime t = OffsetDateTime.of(2007, 10, 28, 2, 30, 0, 0, OFFSET_PTWO); 1077 assertEquals(t.atZoneSimilarLocal(ZONE_PARIS).toLocalDateTime(), t.toLocalDateTime()); 1078 assertEquals(t.atZoneSimilarLocal(ZONE_PARIS).getOffset(), OFFSET_PTWO); 1079 assertEquals(t.atZoneSimilarLocal(ZONE_PARIS).getZone(), ZONE_PARIS); 1080 } 1081 1082 @Test(groups={"tck"}) 1083 public void test_atZone_dstOverlapWinter() { 1084 OffsetDateTime t = OffsetDateTime.of(2007, 10, 28, 2, 30, 0, 0, OFFSET_PONE); 1085 assertEquals(t.atZoneSimilarLocal(ZONE_PARIS).toLocalDateTime(), t.toLocalDateTime()); 1086 assertEquals(t.atZoneSimilarLocal(ZONE_PARIS).getOffset(), OFFSET_PONE); 1087 assertEquals(t.atZoneSimilarLocal(ZONE_PARIS).getZone(), ZONE_PARIS); 1088 } 1089 1090 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 1091 public void test_atZoneSimilarLocal_nullTimeZone() { 1092 OffsetDateTime t = OffsetDateTime.of(2008, 6, 30, 11, 30, 0, 0, OFFSET_PTWO); 1093 t.atZoneSimilarLocal((ZoneId) null); 1094 } 1095 1096 //----------------------------------------------------------------------- 1097 // toEpochSecond() 1098 //----------------------------------------------------------------------- 1099 @Test(groups={"tck"}) 1100 public void test_toEpochSecond_afterEpoch() { 1101 for (int i = 0; i < 100000; i++) { 1102 OffsetDateTime a = OffsetDateTime.of(1970, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC).plusSeconds(i); 1103 assertEquals(a.toEpochSecond(), i); 1104 } 1105 } 1106 1107 @Test(groups={"tck"}) 1108 public void test_toEpochSecond_beforeEpoch() { 1109 for (int i = 0; i < 100000; i++) { 1110 OffsetDateTime a = OffsetDateTime.of(1970, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC).minusSeconds(i); 1111 assertEquals(a.toEpochSecond(), -i); 1112 } 1113 } 1114 1115 //----------------------------------------------------------------------- 1116 // compareTo() 1117 //----------------------------------------------------------------------- 1118 @Test(groups={"tck"}) 1119 public void test_compareTo_timeMins() { 1120 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 11, 29, 3, 0, OFFSET_PONE); 1121 OffsetDateTime b = OffsetDateTime.of(2008, 6, 30, 11, 30, 2, 0, OFFSET_PONE); // a is before b due to time 1122 assertEquals(a.compareTo(b) < 0, true); 1123 assertEquals(b.compareTo(a) > 0, true); 1124 assertEquals(a.compareTo(a) == 0, true); 1125 assertEquals(b.compareTo(b) == 0, true); 1126 assertEquals(a.toInstant().compareTo(b.toInstant()) < 0, true); 1127 } 1128 1129 @Test(groups={"tck"}) 1130 public void test_compareTo_timeSecs() { 1131 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 11, 29, 2, 0, OFFSET_PONE); 1132 OffsetDateTime b = OffsetDateTime.of(2008, 6, 30, 11, 29, 3, 0, OFFSET_PONE); // a is before b due to time 1133 assertEquals(a.compareTo(b) < 0, true); 1134 assertEquals(b.compareTo(a) > 0, true); 1135 assertEquals(a.compareTo(a) == 0, true); 1136 assertEquals(b.compareTo(b) == 0, true); 1137 assertEquals(a.toInstant().compareTo(b.toInstant()) < 0, true); 1138 } 1139 1140 @Test(groups={"tck"}) 1141 public void test_compareTo_timeNanos() { 1142 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 11, 29, 40, 4, OFFSET_PONE); 1143 OffsetDateTime b = OffsetDateTime.of(2008, 6, 30, 11, 29, 40, 5, OFFSET_PONE); // a is before b due to time 1144 assertEquals(a.compareTo(b) < 0, true); 1145 assertEquals(b.compareTo(a) > 0, true); 1146 assertEquals(a.compareTo(a) == 0, true); 1147 assertEquals(b.compareTo(b) == 0, true); 1148 assertEquals(a.toInstant().compareTo(b.toInstant()) < 0, true); 1149 } 1150 1151 @Test(groups={"tck"}) 1152 public void test_compareTo_offset() { 1153 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 11, 30, 0, 0, OFFSET_PTWO); 1154 OffsetDateTime b = OffsetDateTime.of(2008, 6, 30, 11, 30, 0, 0, OFFSET_PONE); // a is before b due to offset 1155 assertEquals(a.compareTo(b) < 0, true); 1156 assertEquals(b.compareTo(a) > 0, true); 1157 assertEquals(a.compareTo(a) == 0, true); 1158 assertEquals(b.compareTo(b) == 0, true); 1159 assertEquals(a.toInstant().compareTo(b.toInstant()) < 0, true); 1160 } 1161 1162 @Test(groups={"tck"}) 1163 public void test_compareTo_offsetNanos() { 1164 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 11, 30, 40, 6, OFFSET_PTWO); 1165 OffsetDateTime b = OffsetDateTime.of(2008, 6, 30, 11, 30, 40, 5, OFFSET_PONE); // a is before b due to offset 1166 assertEquals(a.compareTo(b) < 0, true); 1167 assertEquals(b.compareTo(a) > 0, true); 1168 assertEquals(a.compareTo(a) == 0, true); 1169 assertEquals(b.compareTo(b) == 0, true); 1170 assertEquals(a.toInstant().compareTo(b.toInstant()) < 0, true); 1171 } 1172 1173 @Test(groups={"tck"}) 1174 public void test_compareTo_both() { 1175 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 11, 50, 0, 0, OFFSET_PTWO); 1176 OffsetDateTime b = OffsetDateTime.of(2008, 6, 30, 11, 20, 0, 0, OFFSET_PONE); // a is before b on instant scale 1177 assertEquals(a.compareTo(b) < 0, true); 1178 assertEquals(b.compareTo(a) > 0, true); 1179 assertEquals(a.compareTo(a) == 0, true); 1180 assertEquals(b.compareTo(b) == 0, true); 1181 assertEquals(a.toInstant().compareTo(b.toInstant()) < 0, true); 1182 } 1183 1184 @Test(groups={"tck"}) 1185 public void test_compareTo_bothNanos() { 1186 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 11, 20, 40, 4, OFFSET_PTWO); 1187 OffsetDateTime b = OffsetDateTime.of(2008, 6, 30, 10, 20, 40, 5, OFFSET_PONE); // a is before b on instant scale 1188 assertEquals(a.compareTo(b) < 0, true); 1189 assertEquals(b.compareTo(a) > 0, true); 1190 assertEquals(a.compareTo(a) == 0, true); 1191 assertEquals(b.compareTo(b) == 0, true); 1192 assertEquals(a.toInstant().compareTo(b.toInstant()) < 0, true); 1193 } 1194 1195 @Test(groups={"tck"}) 1196 public void test_compareTo_hourDifference() { 1197 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 10, 0, 0, 0, OFFSET_PONE); 1198 OffsetDateTime b = OffsetDateTime.of(2008, 6, 30, 11, 0, 0, 0, OFFSET_PTWO); // a is before b despite being same time-line time 1199 assertEquals(a.compareTo(b) < 0, true); 1200 assertEquals(b.compareTo(a) > 0, true); 1201 assertEquals(a.compareTo(a) == 0, true); 1202 assertEquals(b.compareTo(b) == 0, true); 1203 assertEquals(a.toInstant().compareTo(b.toInstant()) == 0, true); 1204 } 1205 1206 @Test(groups={"tck"}) 1207 public void test_compareTo_max() { 1208 OffsetDateTime a = OffsetDateTime.of(Year.MAX_VALUE, 12, 31, 23, 59, 0, 0, OFFSET_MONE); 1209 OffsetDateTime b = OffsetDateTime.of(Year.MAX_VALUE, 12, 31, 23, 59, 0, 0, OFFSET_MTWO); // a is before b due to offset 1210 assertEquals(a.compareTo(b) < 0, true); 1211 assertEquals(b.compareTo(a) > 0, true); 1212 assertEquals(a.compareTo(a) == 0, true); 1213 assertEquals(b.compareTo(b) == 0, true); 1214 } 1215 1216 @Test(groups={"tck"}) 1217 public void test_compareTo_min() { 1218 OffsetDateTime a = OffsetDateTime.of(Year.MIN_VALUE, 1, 1, 0, 0, 0, 0, OFFSET_PTWO); 1219 OffsetDateTime b = OffsetDateTime.of(Year.MIN_VALUE, 1, 1, 0, 0, 0, 0, OFFSET_PONE); // a is before b due to offset 1220 assertEquals(a.compareTo(b) < 0, true); 1221 assertEquals(b.compareTo(a) > 0, true); 1222 assertEquals(a.compareTo(a) == 0, true); 1223 assertEquals(b.compareTo(b) == 0, true); 1224 } 1225 1226 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 1227 public void test_compareTo_null() { 1228 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 1229 a.compareTo(null); 1230 } 1231 1232 @Test(expectedExceptions=ClassCastException.class, groups={"tck"}) 1233 @SuppressWarnings({"unchecked", "rawtypes"}) 1234 public void compareToNonOffsetDateTime() { 1235 Comparable c = TEST_2008_6_30_11_30_59_000000500; 1236 c.compareTo(new Object()); 1237 } 1238 1239 //----------------------------------------------------------------------- 1240 // isAfter() / isBefore() / isEqual() 1241 //----------------------------------------------------------------------- 1242 @Test(groups={"tck"}) 1243 public void test_isBeforeIsAfterIsEqual1() { 1244 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 11, 30, 58, 3, OFFSET_PONE); 1245 OffsetDateTime b = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 2, OFFSET_PONE); // a is before b due to time 1246 assertEquals(a.isBefore(b), true); 1247 assertEquals(a.isEqual(b), false); 1248 assertEquals(a.isAfter(b), false); 1249 1250 assertEquals(b.isBefore(a), false); 1251 assertEquals(b.isEqual(a), false); 1252 assertEquals(b.isAfter(a), true); 1253 1254 assertEquals(a.isBefore(a), false); 1255 assertEquals(b.isBefore(b), false); 1256 1257 assertEquals(a.isEqual(a), true); 1258 assertEquals(b.isEqual(b), true); 1259 1260 assertEquals(a.isAfter(a), false); 1261 assertEquals(b.isAfter(b), false); 1262 } 1263 1264 @Test(groups={"tck"}) 1265 public void test_isBeforeIsAfterIsEqual2() { 1266 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 2, OFFSET_PONE); 1267 OffsetDateTime b = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 3, OFFSET_PONE); // a is before b due to time 1268 assertEquals(a.isBefore(b), true); 1269 assertEquals(a.isEqual(b), false); 1270 assertEquals(a.isAfter(b), false); 1271 1272 assertEquals(b.isBefore(a), false); 1273 assertEquals(b.isEqual(a), false); 1274 assertEquals(b.isAfter(a), true); 1275 1276 assertEquals(a.isBefore(a), false); 1277 assertEquals(b.isBefore(b), false); 1278 1279 assertEquals(a.isEqual(a), true); 1280 assertEquals(b.isEqual(b), true); 1281 1282 assertEquals(a.isAfter(a), false); 1283 assertEquals(b.isAfter(b), false); 1284 } 1285 1286 @Test(groups={"tck"}) 1287 public void test_isBeforeIsAfterIsEqual_instantComparison() { 1288 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 10, 0, 0, 0, OFFSET_PONE); 1289 OffsetDateTime b = OffsetDateTime.of(2008, 6, 30, 11, 0, 0, 0, OFFSET_PTWO); // a is same instant as b 1290 assertEquals(a.isBefore(b), false); 1291 assertEquals(a.isEqual(b), true); 1292 assertEquals(a.isAfter(b), false); 1293 1294 assertEquals(b.isBefore(a), false); 1295 assertEquals(b.isEqual(a), true); 1296 assertEquals(b.isAfter(a), false); 1297 1298 assertEquals(a.isBefore(a), false); 1299 assertEquals(b.isBefore(b), false); 1300 1301 assertEquals(a.isEqual(a), true); 1302 assertEquals(b.isEqual(b), true); 1303 1304 assertEquals(a.isAfter(a), false); 1305 assertEquals(b.isAfter(b), false); 1306 } 1307 1308 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 1309 public void test_isBefore_null() { 1310 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 1311 a.isBefore(null); 1312 } 1313 1314 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 1315 public void test_isEqual_null() { 1316 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 1317 a.isEqual(null); 1318 } 1319 1320 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 1321 public void test_isAfter_null() { 1322 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 1323 a.isAfter(null); 1324 } 1325 1326 //----------------------------------------------------------------------- 1327 // equals() / hashCode() 1328 //----------------------------------------------------------------------- 1329 @Test(dataProvider="sampleTimes", groups={"tck"}) 1330 public void test_equals_true(int y, int o, int d, int h, int m, int s, int n, ZoneOffset ignored) { 1331 OffsetDateTime a = OffsetDateTime.of(y, o, d, h, m, s, n, OFFSET_PONE); 1332 OffsetDateTime b = OffsetDateTime.of(y, o, d, h, m, s, n, OFFSET_PONE); 1333 assertEquals(a.equals(b), true); 1334 assertEquals(a.hashCode() == b.hashCode(), true); 1335 } 1336 @Test(dataProvider="sampleTimes", groups={"tck"}) 1337 public void test_equals_false_year_differs(int y, int o, int d, int h, int m, int s, int n, ZoneOffset ignored) { 1338 OffsetDateTime a = OffsetDateTime.of(y, o, d, h, m, s, n, OFFSET_PONE); 1339 OffsetDateTime b = OffsetDateTime.of(y + 1, o, d, h, m, s, n, OFFSET_PONE); 1340 assertEquals(a.equals(b), false); 1341 } 1342 @Test(dataProvider="sampleTimes", groups={"tck"}) 1343 public void test_equals_false_hour_differs(int y, int o, int d, int h, int m, int s, int n, ZoneOffset ignored) { 1344 h = (h == 23 ? 22 : h); 1345 OffsetDateTime a = OffsetDateTime.of(y, o, d, h, m, s, n, OFFSET_PONE); 1346 OffsetDateTime b = OffsetDateTime.of(y, o, d, h + 1, m, s, n, OFFSET_PONE); 1347 assertEquals(a.equals(b), false); 1348 } 1349 @Test(dataProvider="sampleTimes", groups={"tck"}) 1350 public void test_equals_false_minute_differs(int y, int o, int d, int h, int m, int s, int n, ZoneOffset ignored) { 1351 m = (m == 59 ? 58 : m); 1352 OffsetDateTime a = OffsetDateTime.of(y, o, d, h, m, s, n, OFFSET_PONE); 1353 OffsetDateTime b = OffsetDateTime.of(y, o, d, h, m + 1, s, n, OFFSET_PONE); 1354 assertEquals(a.equals(b), false); 1355 } 1356 @Test(dataProvider="sampleTimes", groups={"tck"}) 1357 public void test_equals_false_second_differs(int y, int o, int d, int h, int m, int s, int n, ZoneOffset ignored) { 1358 s = (s == 59 ? 58 : s); 1359 OffsetDateTime a = OffsetDateTime.of(y, o, d, h, m, s, n, OFFSET_PONE); 1360 OffsetDateTime b = OffsetDateTime.of(y, o, d, h, m, s + 1, n, OFFSET_PONE); 1361 assertEquals(a.equals(b), false); 1362 } 1363 @Test(dataProvider="sampleTimes", groups={"tck"}) 1364 public void test_equals_false_nano_differs(int y, int o, int d, int h, int m, int s, int n, ZoneOffset ignored) { 1365 n = (n == 999999999 ? 999999998 : n); 1366 OffsetDateTime a = OffsetDateTime.of(y, o, d, h, m, s, n, OFFSET_PONE); 1367 OffsetDateTime b = OffsetDateTime.of(y, o, d, h, m, s, n + 1, OFFSET_PONE); 1368 assertEquals(a.equals(b), false); 1369 } 1370 @Test(dataProvider="sampleTimes", groups={"tck"}) 1371 public void test_equals_false_offset_differs(int y, int o, int d, int h, int m, int s, int n, ZoneOffset ignored) { 1372 OffsetDateTime a = OffsetDateTime.of(y, o, d, h, m, s, n, OFFSET_PONE); 1373 OffsetDateTime b = OffsetDateTime.of(y, o, d, h, m, s, n, OFFSET_PTWO); 1374 assertEquals(a.equals(b), false); 1375 } 1376 1377 @Test(groups={"tck"}) 1378 public void test_equals_itself_true() { 1379 assertEquals(TEST_2008_6_30_11_30_59_000000500.equals(TEST_2008_6_30_11_30_59_000000500), true); 1380 } 1381 1382 @Test(groups={"tck"}) 1383 public void test_equals_string_false() { 1384 assertEquals(TEST_2008_6_30_11_30_59_000000500.equals("2007-07-15"), false); 1385 } 1386 1387 @Test(groups={"tck"}) 1388 public void test_equals_null_false() { 1389 assertEquals(TEST_2008_6_30_11_30_59_000000500.equals(null), false); 1390 } 1391 1392 //----------------------------------------------------------------------- 1393 // toString() 1394 //----------------------------------------------------------------------- 1395 @DataProvider(name="sampleToString") 1396 Object[][] provider_sampleToString() { 1397 return new Object[][] { 1398 {2008, 6, 30, 11, 30, 59, 0, "Z", "2008-06-30T11:30:59Z"}, 1399 {2008, 6, 30, 11, 30, 59, 0, "+01:00", "2008-06-30T11:30:59+01:00"}, 1400 {2008, 6, 30, 11, 30, 59, 999000000, "Z", "2008-06-30T11:30:59.999Z"}, 1401 {2008, 6, 30, 11, 30, 59, 999000000, "+01:00", "2008-06-30T11:30:59.999+01:00"}, 1402 {2008, 6, 30, 11, 30, 59, 999000, "Z", "2008-06-30T11:30:59.000999Z"}, 1403 {2008, 6, 30, 11, 30, 59, 999000, "+01:00", "2008-06-30T11:30:59.000999+01:00"}, 1404 {2008, 6, 30, 11, 30, 59, 999, "Z", "2008-06-30T11:30:59.000000999Z"}, 1405 {2008, 6, 30, 11, 30, 59, 999, "+01:00", "2008-06-30T11:30:59.000000999+01:00"}, 1406 }; 1407 } 1408 1409 @Test(dataProvider="sampleToString", groups={"tck"}) 1410 public void test_toString(int y, int o, int d, int h, int m, int s, int n, String offsetId, String expected) { 1411 OffsetDateTime t = OffsetDateTime.of(y, o, d, h, m, s, n, ZoneOffset.of(offsetId)); 1412 String str = t.toString(); 1413 assertEquals(str, expected); 1414 } 1415 1416 //----------------------------------------------------------------------- 1417 // toString(DateTimeFormatter) 1418 //----------------------------------------------------------------------- 1419 @Test(groups={"tck"}) 1420 public void test_toString_formatter() { 1421 DateTimeFormatter f = DateTimeFormatter.ofPattern("y M d H m s"); 1422 String t = OffsetDateTime.of(2010, 12, 3, 11, 30, 0, 0, OFFSET_PONE).toString(f); 1423 assertEquals(t, "2010 12 3 11 30 0"); 1424 } 1425 1426 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 1427 public void test_toString_formatter_null() { 1428 OffsetDateTime.of(2010, 12, 3, 11, 30, 0, 0, OFFSET_PONE).toString(null); 1429 } 1430 1431 } | 54 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 55 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 56 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 57 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 58 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 59 */ 60 package tck.java.time; 61 62 import static java.time.Month.DECEMBER; 63 import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH; 64 import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR; 65 import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_MONTH; 66 import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR; 67 import static java.time.temporal.ChronoField.AMPM_OF_DAY; 68 import static java.time.temporal.ChronoField.CLOCK_HOUR_OF_AMPM; 69 import static java.time.temporal.ChronoField.CLOCK_HOUR_OF_DAY; 70 import static java.time.temporal.ChronoField.DAY_OF_MONTH; 71 import static java.time.temporal.ChronoField.DAY_OF_WEEK; 72 import static java.time.temporal.ChronoField.DAY_OF_YEAR; 73 import static java.time.temporal.ChronoField.EPOCH_DAY; 74 import static java.time.temporal.ChronoField.ERA; 75 import static java.time.temporal.ChronoField.HOUR_OF_AMPM; 76 import static java.time.temporal.ChronoField.HOUR_OF_DAY; 77 import static java.time.temporal.ChronoField.INSTANT_SECONDS; 78 import static java.time.temporal.ChronoField.MICRO_OF_DAY; 79 import static java.time.temporal.ChronoField.MICRO_OF_SECOND; 80 import static java.time.temporal.ChronoField.MILLI_OF_DAY; 81 import static java.time.temporal.ChronoField.MILLI_OF_SECOND; 82 import static java.time.temporal.ChronoField.MINUTE_OF_DAY; 83 import static java.time.temporal.ChronoField.MINUTE_OF_HOUR; 84 import static java.time.temporal.ChronoField.MONTH_OF_YEAR; 85 import static java.time.temporal.ChronoField.NANO_OF_DAY; 86 import static java.time.temporal.ChronoField.NANO_OF_SECOND; 87 import static java.time.temporal.ChronoField.OFFSET_SECONDS; 88 import static java.time.temporal.ChronoField.PROLEPTIC_MONTH; 89 import static java.time.temporal.ChronoField.SECOND_OF_DAY; 90 import static java.time.temporal.ChronoField.SECOND_OF_MINUTE; 91 import static java.time.temporal.ChronoField.YEAR; 92 import static java.time.temporal.ChronoField.YEAR_OF_ERA; 93 import static java.time.temporal.ChronoUnit.DAYS; 94 import static java.time.temporal.ChronoUnit.NANOS; 95 import static java.time.temporal.ChronoUnit.SECONDS; 96 import static org.testng.Assert.assertEquals; 97 import static org.testng.Assert.assertTrue; 98 import static org.testng.Assert.fail; 99 100 import java.io.ByteArrayOutputStream; 101 import java.io.DataOutputStream; 102 import java.lang.reflect.Constructor; 103 import java.lang.reflect.InvocationTargetException; 104 import java.time.Clock; 105 import java.time.DateTimeException; 106 import java.time.Duration; 107 import java.time.Instant; 108 import java.time.LocalDate; 109 import java.time.LocalDateTime; 110 import java.time.LocalTime; 111 import java.time.Month; 112 import java.time.OffsetDateTime; 113 import java.time.OffsetTime; 114 import java.time.Year; 115 import java.time.ZoneId; 116 import java.time.ZoneOffset; 117 import java.time.ZonedDateTime; 118 import java.time.chrono.IsoChronology; 119 import java.time.format.DateTimeFormatter; 120 import java.time.format.DateTimeParseException; 121 import java.time.temporal.ChronoField; 122 import java.time.temporal.ChronoUnit; 123 import java.time.temporal.JulianFields; 124 import java.time.temporal.Temporal; 125 import java.time.temporal.TemporalAccessor; 126 import java.time.temporal.TemporalAdjuster; 127 import java.time.temporal.TemporalField; 128 import java.time.temporal.TemporalQuery; 129 import java.util.ArrayList; 130 import java.util.Arrays; 131 import java.util.List; 132 133 import org.testng.annotations.BeforeMethod; 134 import org.testng.annotations.DataProvider; 135 import org.testng.annotations.Test; 136 import test.java.time.MockSimplePeriod; 137 138 /** 139 * Test OffsetDateTime. 140 */ 141 @Test 142 public class TCKOffsetDateTime extends AbstractDateTimeTest { 143 144 private static final ZoneId ZONE_PARIS = ZoneId.of("Europe/Paris"); 145 private static final ZoneId ZONE_GAZA = ZoneId.of("Asia/Gaza"); 146 private static final ZoneOffset OFFSET_PONE = ZoneOffset.ofHours(1); 147 private static final ZoneOffset OFFSET_PTWO = ZoneOffset.ofHours(2); 148 private static final ZoneOffset OFFSET_MONE = ZoneOffset.ofHours(-1); 149 private static final ZoneOffset OFFSET_MTWO = ZoneOffset.ofHours(-2); 150 private OffsetDateTime TEST_2008_6_30_11_30_59_000000500; 151 152 @BeforeMethod 153 public void setUp() { 154 TEST_2008_6_30_11_30_59_000000500 = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 500, OFFSET_PONE); 155 } 156 157 //----------------------------------------------------------------------- 158 @Override 159 protected List<TemporalAccessor> samples() { 160 TemporalAccessor[] array = {TEST_2008_6_30_11_30_59_000000500, OffsetDateTime.MIN, OffsetDateTime.MAX}; 161 return Arrays.asList(array); 162 } 163 164 @Override 165 protected List<TemporalField> validFields() { 166 TemporalField[] array = { 167 NANO_OF_SECOND, 168 NANO_OF_DAY, 169 MICRO_OF_SECOND, 170 MICRO_OF_DAY, 171 MILLI_OF_SECOND, 172 MILLI_OF_DAY, 173 SECOND_OF_MINUTE, 174 SECOND_OF_DAY, 175 MINUTE_OF_HOUR, 176 MINUTE_OF_DAY, 177 CLOCK_HOUR_OF_AMPM, 178 HOUR_OF_AMPM, 179 CLOCK_HOUR_OF_DAY, 180 HOUR_OF_DAY, 181 AMPM_OF_DAY, 182 DAY_OF_WEEK, 183 ALIGNED_DAY_OF_WEEK_IN_MONTH, 184 ALIGNED_DAY_OF_WEEK_IN_YEAR, 185 DAY_OF_MONTH, 186 DAY_OF_YEAR, 187 EPOCH_DAY, 188 ALIGNED_WEEK_OF_MONTH, 189 ALIGNED_WEEK_OF_YEAR, 190 MONTH_OF_YEAR, 191 PROLEPTIC_MONTH, 192 YEAR_OF_ERA, 193 YEAR, 194 ERA, 195 OFFSET_SECONDS, 196 INSTANT_SECONDS, 197 JulianFields.JULIAN_DAY, 198 JulianFields.MODIFIED_JULIAN_DAY, 199 JulianFields.RATA_DIE, 200 }; 201 return Arrays.asList(array); 202 } 203 204 @Override 205 protected List<TemporalField> invalidFields() { 206 List<TemporalField> list = new ArrayList<>(Arrays.<TemporalField>asList(ChronoField.values())); 207 list.removeAll(validFields()); 208 return list; 209 } 210 211 //----------------------------------------------------------------------- 244 LocalDateTime ldt = LocalDateTime.of(2012, 9, 16, 22, 17, 59, 464_000_000); 245 assertSerializedBySer(OffsetDateTime.of(ldt, ZoneOffset.ofHours(1)), bytes, bytesDateTime, bytesOffset); 246 } 247 248 //----------------------------------------------------------------------- 249 // constants 250 //----------------------------------------------------------------------- 251 @Test 252 public void constant_MIN() { 253 check(OffsetDateTime.MIN, Year.MIN_VALUE, 1, 1, 0, 0, 0, 0, ZoneOffset.MAX); 254 } 255 256 @Test 257 public void constant_MAX() { 258 check(OffsetDateTime.MAX, Year.MAX_VALUE, 12, 31, 23, 59, 59, 999999999, ZoneOffset.MIN); 259 } 260 261 //----------------------------------------------------------------------- 262 // now() 263 //----------------------------------------------------------------------- 264 @Test 265 public void now() { 266 OffsetDateTime expected = OffsetDateTime.now(Clock.systemDefaultZone()); 267 OffsetDateTime test = OffsetDateTime.now(); 268 long diff = Math.abs(test.toLocalTime().toNanoOfDay() - expected.toLocalTime().toNanoOfDay()); 269 if (diff >= 100000000) { 270 // may be date change 271 expected = OffsetDateTime.now(Clock.systemDefaultZone()); 272 test = OffsetDateTime.now(); 273 diff = Math.abs(test.toLocalTime().toNanoOfDay() - expected.toLocalTime().toNanoOfDay()); 274 } 275 assertTrue(diff < 100000000); // less than 0.1 secs 276 } 277 278 //----------------------------------------------------------------------- 279 // now(Clock) 280 //----------------------------------------------------------------------- 281 @Test 282 public void now_Clock_allSecsInDay_utc() { 283 for (int i = 0; i < (2 * 24 * 60 * 60); i++) { 284 Instant instant = Instant.ofEpochSecond(i).plusNanos(123456789L); 285 Clock clock = Clock.fixed(instant, ZoneOffset.UTC); 286 OffsetDateTime test = OffsetDateTime.now(clock); 287 assertEquals(test.getYear(), 1970); 288 assertEquals(test.getMonth(), Month.JANUARY); 289 assertEquals(test.getDayOfMonth(), (i < 24 * 60 * 60 ? 1 : 2)); 290 assertEquals(test.getHour(), (i / (60 * 60)) % 24); 291 assertEquals(test.getMinute(), (i / 60) % 60); 292 assertEquals(test.getSecond(), i % 60); 293 assertEquals(test.getNano(), 123456789); 294 assertEquals(test.getOffset(), ZoneOffset.UTC); 295 } 296 } 297 298 @Test 299 public void now_Clock_allSecsInDay_offset() { 300 for (int i = 0; i < (2 * 24 * 60 * 60); i++) { 301 Instant instant = Instant.ofEpochSecond(i).plusNanos(123456789L); 302 Clock clock = Clock.fixed(instant.minusSeconds(OFFSET_PONE.getTotalSeconds()), OFFSET_PONE); 303 OffsetDateTime test = OffsetDateTime.now(clock); 304 assertEquals(test.getYear(), 1970); 305 assertEquals(test.getMonth(), Month.JANUARY); 306 assertEquals(test.getDayOfMonth(), (i < 24 * 60 * 60) ? 1 : 2); 307 assertEquals(test.getHour(), (i / (60 * 60)) % 24); 308 assertEquals(test.getMinute(), (i / 60) % 60); 309 assertEquals(test.getSecond(), i % 60); 310 assertEquals(test.getNano(), 123456789); 311 assertEquals(test.getOffset(), OFFSET_PONE); 312 } 313 } 314 315 @Test 316 public void now_Clock_allSecsInDay_beforeEpoch() { 317 LocalTime expected = LocalTime.MIDNIGHT.plusNanos(123456789L); 318 for (int i =-1; i >= -(24 * 60 * 60); i--) { 319 Instant instant = Instant.ofEpochSecond(i).plusNanos(123456789L); 320 Clock clock = Clock.fixed(instant, ZoneOffset.UTC); 321 OffsetDateTime test = OffsetDateTime.now(clock); 322 assertEquals(test.getYear(), 1969); 323 assertEquals(test.getMonth(), Month.DECEMBER); 324 assertEquals(test.getDayOfMonth(), 31); 325 expected = expected.minusSeconds(1); 326 assertEquals(test.toLocalTime(), expected); 327 assertEquals(test.getOffset(), ZoneOffset.UTC); 328 } 329 } 330 331 @Test 332 public void now_Clock_offsets() { 333 OffsetDateTime base = OffsetDateTime.of(1970, 1, 1, 12, 0, 0, 0, ZoneOffset.UTC); 334 for (int i = -9; i < 15; i++) { 335 ZoneOffset offset = ZoneOffset.ofHours(i); 336 Clock clock = Clock.fixed(base.toInstant(), offset); 337 OffsetDateTime test = OffsetDateTime.now(clock); 338 assertEquals(test.getHour(), (12 + i) % 24); 339 assertEquals(test.getMinute(), 0); 340 assertEquals(test.getSecond(), 0); 341 assertEquals(test.getNano(), 0); 342 assertEquals(test.getOffset(), offset); 343 } 344 } 345 346 @Test(expectedExceptions=NullPointerException.class) 347 public void now_Clock_nullZoneId() { 348 OffsetDateTime.now((ZoneId) null); 349 } 350 351 @Test(expectedExceptions=NullPointerException.class) 352 public void now_Clock_nullClock() { 353 OffsetDateTime.now((Clock) null); 354 } 355 356 //----------------------------------------------------------------------- 357 private void check(OffsetDateTime test, int y, int mo, int d, int h, int m, int s, int n, ZoneOffset offset) { 358 assertEquals(test.getYear(), y); 359 assertEquals(test.getMonth().getValue(), mo); 360 assertEquals(test.getDayOfMonth(), d); 361 assertEquals(test.getHour(), h); 362 assertEquals(test.getMinute(), m); 363 assertEquals(test.getSecond(), s); 364 assertEquals(test.getNano(), n); 365 assertEquals(test.getOffset(), offset); 366 assertEquals(test, test); 367 assertEquals(test.hashCode(), test.hashCode()); 368 assertEquals(OffsetDateTime.of(LocalDateTime.of(y, mo, d, h, m, s, n), offset), test); 369 } 370 371 //----------------------------------------------------------------------- 372 // factories 373 //----------------------------------------------------------------------- 374 @Test 375 public void factory_of_intsHMSN() { 376 OffsetDateTime test = OffsetDateTime.of(2008, 6, 30, 11, 30, 10, 500, OFFSET_PONE); 377 check(test, 2008, 6, 30, 11, 30, 10, 500, OFFSET_PONE); 378 } 379 380 //----------------------------------------------------------------------- 381 @Test 382 public void factory_of_LocalDateLocalTimeZoneOffset() { 383 LocalDate date = LocalDate.of(2008, 6, 30); 384 LocalTime time = LocalTime.of(11, 30, 10, 500); 385 OffsetDateTime test = OffsetDateTime.of(date, time, OFFSET_PONE); 386 check(test, 2008, 6, 30, 11, 30, 10, 500, OFFSET_PONE); 387 } 388 389 @Test(expectedExceptions=NullPointerException.class) 390 public void factory_of_LocalDateLocalTimeZoneOffset_nullLocalDate() { 391 LocalTime time = LocalTime.of(11, 30, 10, 500); 392 OffsetDateTime.of((LocalDate) null, time, OFFSET_PONE); 393 } 394 395 @Test(expectedExceptions=NullPointerException.class) 396 public void factory_of_LocalDateLocalTimeZoneOffset_nullLocalTime() { 397 LocalDate date = LocalDate.of(2008, 6, 30); 398 OffsetDateTime.of(date, (LocalTime) null, OFFSET_PONE); 399 } 400 401 @Test(expectedExceptions=NullPointerException.class) 402 public void factory_of_LocalDateLocalTimeZoneOffset_nullOffset() { 403 LocalDate date = LocalDate.of(2008, 6, 30); 404 LocalTime time = LocalTime.of(11, 30, 10, 500); 405 OffsetDateTime.of(date, time, (ZoneOffset) null); 406 } 407 408 //----------------------------------------------------------------------- 409 @Test 410 public void factory_of_LocalDateTimeZoneOffset() { 411 LocalDateTime dt = LocalDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 10, 500)); 412 OffsetDateTime test = OffsetDateTime.of(dt, OFFSET_PONE); 413 check(test, 2008, 6, 30, 11, 30, 10, 500, OFFSET_PONE); 414 } 415 416 @Test(expectedExceptions=NullPointerException.class) 417 public void factory_of_LocalDateTimeZoneOffset_nullProvider() { 418 OffsetDateTime.of((LocalDateTime) null, OFFSET_PONE); 419 } 420 421 @Test(expectedExceptions=NullPointerException.class) 422 public void factory_of_LocalDateTimeZoneOffset_nullOffset() { 423 LocalDateTime dt = LocalDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 10, 500)); 424 OffsetDateTime.of(dt, (ZoneOffset) null); 425 } 426 427 //----------------------------------------------------------------------- 428 // from() 429 //----------------------------------------------------------------------- 430 @Test 431 public void test_factory_CalendricalObject() { 432 assertEquals(OffsetDateTime.from( 433 OffsetDateTime.of(LocalDate.of(2007, 7, 15), LocalTime.of(17, 30), OFFSET_PONE)), 434 OffsetDateTime.of(LocalDate.of(2007, 7, 15), LocalTime.of(17, 30), OFFSET_PONE)); 435 } 436 437 @Test(expectedExceptions=DateTimeException.class) 438 public void test_factory_CalendricalObject_invalid_noDerive() { 439 OffsetDateTime.from(LocalTime.of(12, 30)); 440 } 441 442 @Test(expectedExceptions=NullPointerException.class) 443 public void test_factory_Calendricals_null() { 444 OffsetDateTime.from((TemporalAccessor) null); 445 } 446 447 //----------------------------------------------------------------------- 448 // parse() 449 //----------------------------------------------------------------------- 450 @Test(dataProvider="sampleToString") 451 public void test_parse(int y, int month, int d, int h, int m, int s, int n, String offsetId, String text) { 452 OffsetDateTime t = OffsetDateTime.parse(text); 453 assertEquals(t.getYear(), y); 454 assertEquals(t.getMonth().getValue(), month); 455 assertEquals(t.getDayOfMonth(), d); 456 assertEquals(t.getHour(), h); 457 assertEquals(t.getMinute(), m); 458 assertEquals(t.getSecond(), s); 459 assertEquals(t.getNano(), n); 460 assertEquals(t.getOffset().getId(), offsetId); 461 } 462 463 @Test(expectedExceptions=DateTimeParseException.class) 464 public void factory_parse_illegalValue() { 465 OffsetDateTime.parse("2008-06-32T11:15+01:00"); 466 } 467 468 @Test(expectedExceptions=DateTimeParseException.class) 469 public void factory_parse_invalidValue() { 470 OffsetDateTime.parse("2008-06-31T11:15+01:00"); 471 } 472 473 @Test(expectedExceptions=NullPointerException.class) 474 public void factory_parse_nullText() { 475 OffsetDateTime.parse((String) null); 476 } 477 478 //----------------------------------------------------------------------- 479 // parse(DateTimeFormatter) 480 //----------------------------------------------------------------------- 481 @Test 482 public void factory_parse_formatter() { 483 DateTimeFormatter f = DateTimeFormatter.ofPattern("y M d H m s XXX"); 484 OffsetDateTime test = OffsetDateTime.parse("2010 12 3 11 30 0 +01:00", f); 485 assertEquals(test, OffsetDateTime.of(LocalDate.of(2010, 12, 3), LocalTime.of(11, 30), ZoneOffset.ofHours(1))); 486 } 487 488 @Test(expectedExceptions=NullPointerException.class) 489 public void factory_parse_formatter_nullText() { 490 DateTimeFormatter f = DateTimeFormatter.ofPattern("y M d H m s"); 491 OffsetDateTime.parse((String) null, f); 492 } 493 494 @Test(expectedExceptions=NullPointerException.class) 495 public void factory_parse_formatter_nullFormatter() { 496 OffsetDateTime.parse("ANY", null); 497 } 498 499 //----------------------------------------------------------------------- 500 @Test(expectedExceptions=NullPointerException.class) 501 public void constructor_nullTime() throws Throwable { 502 Constructor<OffsetDateTime> con = OffsetDateTime.class.getDeclaredConstructor(LocalDateTime.class, ZoneOffset.class); 503 con.setAccessible(true); 504 try { 505 con.newInstance(null, OFFSET_PONE); 506 } catch (InvocationTargetException ex) { 507 throw ex.getCause(); 508 } 509 } 510 511 @Test(expectedExceptions=NullPointerException.class) 512 public void constructor_nullOffset() throws Throwable { 513 Constructor<OffsetDateTime> con = OffsetDateTime.class.getDeclaredConstructor(LocalDateTime.class, ZoneOffset.class); 514 con.setAccessible(true); 515 try { 516 con.newInstance(LocalDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30)), null); 517 } catch (InvocationTargetException ex) { 518 throw ex.getCause(); 519 } 520 } 521 522 //----------------------------------------------------------------------- 523 // basics 524 //----------------------------------------------------------------------- 525 @DataProvider(name="sampleTimes") 526 Object[][] provider_sampleTimes() { 527 return new Object[][] { 528 {2008, 6, 30, 11, 30, 20, 500, OFFSET_PONE}, 529 {2008, 6, 30, 11, 0, 0, 0, OFFSET_PONE}, 530 {2008, 6, 30, 23, 59, 59, 999999999, OFFSET_PONE}, 531 {-1, 1, 1, 0, 0, 0, 0, OFFSET_PONE}, 532 }; 533 } 534 535 @Test(dataProvider="sampleTimes") 536 public void test_get(int y, int o, int d, int h, int m, int s, int n, ZoneOffset offset) { 537 LocalDate localDate = LocalDate.of(y, o, d); 538 LocalTime localTime = LocalTime.of(h, m, s, n); 539 LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime); 540 OffsetDateTime a = OffsetDateTime.of(localDateTime, offset); 541 542 assertEquals(a.getYear(), localDate.getYear()); 543 assertEquals(a.getMonth(), localDate.getMonth()); 544 assertEquals(a.getDayOfMonth(), localDate.getDayOfMonth()); 545 assertEquals(a.getDayOfYear(), localDate.getDayOfYear()); 546 assertEquals(a.getDayOfWeek(), localDate.getDayOfWeek()); 547 548 assertEquals(a.getHour(), localDateTime.getHour()); 549 assertEquals(a.getMinute(), localDateTime.getMinute()); 550 assertEquals(a.getSecond(), localDateTime.getSecond()); 551 assertEquals(a.getNano(), localDateTime.getNano()); 552 553 assertEquals(a.toOffsetTime(), OffsetTime.of(localTime, offset)); 554 assertEquals(a.toString(), localDateTime.toString() + offset.toString()); 555 } 585 assertEquals(test.getLong(ChronoField.DAY_OF_WEEK), 1); 586 assertEquals(test.getLong(ChronoField.DAY_OF_YEAR), 182); 587 588 assertEquals(test.getLong(ChronoField.HOUR_OF_DAY), 12); 589 assertEquals(test.getLong(ChronoField.MINUTE_OF_HOUR), 30); 590 assertEquals(test.getLong(ChronoField.SECOND_OF_MINUTE), 40); 591 assertEquals(test.getLong(ChronoField.NANO_OF_SECOND), 987654321); 592 assertEquals(test.getLong(ChronoField.HOUR_OF_AMPM), 0); 593 assertEquals(test.getLong(ChronoField.AMPM_OF_DAY), 1); 594 595 assertEquals(test.getLong(ChronoField.INSTANT_SECONDS), test.toEpochSecond()); 596 assertEquals(test.getLong(ChronoField.OFFSET_SECONDS), 3600); 597 } 598 599 //----------------------------------------------------------------------- 600 // query(TemporalQuery) 601 //----------------------------------------------------------------------- 602 @DataProvider(name="query") 603 Object[][] data_query() { 604 return new Object[][] { 605 {TEST_2008_6_30_11_30_59_000000500, TemporalQuery.chronology(), IsoChronology.INSTANCE}, 606 {TEST_2008_6_30_11_30_59_000000500, TemporalQuery.zoneId(), null}, 607 {TEST_2008_6_30_11_30_59_000000500, TemporalQuery.precision(), ChronoUnit.NANOS}, 608 {TEST_2008_6_30_11_30_59_000000500, TemporalQuery.zone(), OFFSET_PONE}, 609 {TEST_2008_6_30_11_30_59_000000500, TemporalQuery.offset(), OFFSET_PONE}, 610 {TEST_2008_6_30_11_30_59_000000500, TemporalQuery.localDate(), LocalDate.of(2008, 6, 30)}, 611 {TEST_2008_6_30_11_30_59_000000500, TemporalQuery.localTime(), LocalTime.of(11, 30, 59, 500)}, 612 }; 613 } 614 615 @Test(dataProvider="query") 616 public <T> void test_query(TemporalAccessor temporal, TemporalQuery<T> query, T expected) { 617 assertEquals(temporal.query(query), expected); 618 } 619 620 @Test(dataProvider="query") 621 public <T> void test_queryFrom(TemporalAccessor temporal, TemporalQuery<T> query, T expected) { 622 assertEquals(query.queryFrom(temporal), expected); 623 } 624 625 @Test(expectedExceptions=NullPointerException.class) 626 public void test_query_null() { 627 TEST_2008_6_30_11_30_59_000000500.query(null); 628 } 629 630 //----------------------------------------------------------------------- 631 // adjustInto(Temporal) 632 //----------------------------------------------------------------------- 633 @DataProvider(name="adjustInto") 634 Object[][] data_adjustInto() { 635 return new Object[][]{ 636 {OffsetDateTime.of(2012, 3, 4, 23, 5, 0, 0, OFFSET_PONE), OffsetDateTime.of(2012, 3, 4, 1, 1, 1, 100, ZoneOffset.UTC), OffsetDateTime.of(2012, 3, 4, 23, 5, 0, 0, OFFSET_PONE), null}, 637 {OffsetDateTime.of(2012, 3, 4, 23, 5, 0, 0, OFFSET_PONE), OffsetDateTime.MAX, OffsetDateTime.of(2012, 3, 4, 23, 5, 0, 0, OFFSET_PONE), null}, 638 {OffsetDateTime.of(2012, 3, 4, 23, 5, 0, 0, OFFSET_PONE), OffsetDateTime.MIN, OffsetDateTime.of(2012, 3, 4, 23, 5, 0, 0, OFFSET_PONE), null}, 639 {OffsetDateTime.MAX, OffsetDateTime.of(2012, 3, 4, 23, 5, 0, 0, OFFSET_PONE), OffsetDateTime.of(OffsetDateTime.MAX.toLocalDateTime(), ZoneOffset.ofHours(-18)), null}, 640 {OffsetDateTime.MIN, OffsetDateTime.of(2012, 3, 4, 23, 5, 0, 0, OFFSET_PONE), OffsetDateTime.of(OffsetDateTime.MIN.toLocalDateTime(), ZoneOffset.ofHours(18)), null}, 641 642 643 {OffsetDateTime.of(2012, 3, 4, 23, 5, 0, 0, OFFSET_PONE), 644 ZonedDateTime.of(2012, 3, 4, 1, 1, 1, 100, ZONE_GAZA), ZonedDateTime.of(2012, 3, 4, 23, 5, 0, 0, ZONE_GAZA), null}, 645 646 {OffsetDateTime.of(2012, 3, 4, 23, 5, 0, 0, OFFSET_PONE), LocalDateTime.of(2012, 3, 4, 1, 1, 1, 100), null, DateTimeException.class}, 647 {OffsetDateTime.of(2012, 3, 4, 23, 5, 0, 0, OFFSET_PONE), LocalDate.of(2210, 2, 2), null, DateTimeException.class}, 648 {OffsetDateTime.of(2012, 3, 4, 23, 5, 0, 0, OFFSET_PONE), LocalTime.of(22, 3, 0), null, DateTimeException.class}, 649 {OffsetDateTime.of(2012, 3, 4, 23, 5, 0, 0, OFFSET_PONE), OffsetTime.of(22, 3, 0, 0, ZoneOffset.UTC), null, DateTimeException.class}, 650 {OffsetDateTime.of(2012, 3, 4, 23, 5, 0, 0, OFFSET_PONE), null, null, NullPointerException.class}, 651 652 }; 653 } 654 655 @Test(dataProvider="adjustInto") 656 public void test_adjustInto(OffsetDateTime test, Temporal temporal, Temporal expected, Class<?> expectedEx) { 657 if (expectedEx == null) { 658 Temporal result = test.adjustInto(temporal); 659 assertEquals(result, expected); 660 } else { 661 try { 662 Temporal result = test.adjustInto(temporal); 663 fail(); 664 } catch (Exception ex) { 665 assertTrue(expectedEx.isInstance(ex)); 666 } 667 } 668 } 669 670 //----------------------------------------------------------------------- 671 // with(WithAdjuster) 672 //----------------------------------------------------------------------- 673 @Test 674 public void test_with_adjustment() { 675 final OffsetDateTime sample = OffsetDateTime.of(LocalDate.of(2012, 3, 4), LocalTime.of(23, 5), OFFSET_PONE); 676 TemporalAdjuster adjuster = new TemporalAdjuster() { 677 @Override 678 public Temporal adjustInto(Temporal dateTime) { 679 return sample; 680 } 681 }; 682 assertEquals(TEST_2008_6_30_11_30_59_000000500.with(adjuster), sample); 683 } 684 685 @Test 686 public void test_with_adjustment_LocalDate() { 687 OffsetDateTime test = TEST_2008_6_30_11_30_59_000000500.with(LocalDate.of(2012, 9, 3)); 688 assertEquals(test, OffsetDateTime.of(LocalDate.of(2012, 9, 3), LocalTime.of(11, 30, 59, 500), OFFSET_PONE)); 689 } 690 691 @Test 692 public void test_with_adjustment_LocalTime() { 693 OffsetDateTime test = TEST_2008_6_30_11_30_59_000000500.with(LocalTime.of(19, 15)); 694 assertEquals(test, OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(19, 15), OFFSET_PONE)); 695 } 696 697 @Test 698 public void test_with_adjustment_LocalDateTime() { 699 OffsetDateTime test = TEST_2008_6_30_11_30_59_000000500.with(LocalDateTime.of(LocalDate.of(2012, 9, 3), LocalTime.of(19, 15))); 700 assertEquals(test, OffsetDateTime.of(LocalDate.of(2012, 9, 3), LocalTime.of(19, 15), OFFSET_PONE)); 701 } 702 703 @Test 704 public void test_with_adjustment_OffsetTime() { 705 OffsetDateTime test = TEST_2008_6_30_11_30_59_000000500.with(OffsetTime.of(LocalTime.of(19, 15), OFFSET_PTWO)); 706 assertEquals(test, OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(19, 15), OFFSET_PTWO)); 707 } 708 709 @Test 710 public void test_with_adjustment_OffsetDateTime() { 711 OffsetDateTime test = TEST_2008_6_30_11_30_59_000000500.with(OffsetDateTime.of(LocalDate.of(2012, 9, 3), LocalTime.of(19, 15), OFFSET_PTWO)); 712 assertEquals(test, OffsetDateTime.of(LocalDate.of(2012, 9, 3), LocalTime.of(19, 15), OFFSET_PTWO)); 713 } 714 715 @Test 716 public void test_with_adjustment_Month() { 717 OffsetDateTime test = TEST_2008_6_30_11_30_59_000000500.with(DECEMBER); 718 assertEquals(test, OffsetDateTime.of(LocalDate.of(2008, 12, 30),LocalTime.of(11, 30, 59, 500), OFFSET_PONE)); 719 } 720 721 @Test 722 public void test_with_adjustment_ZoneOffset() { 723 OffsetDateTime test = TEST_2008_6_30_11_30_59_000000500.with(OFFSET_PTWO); 724 assertEquals(test, OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59, 500), OFFSET_PTWO)); 725 } 726 727 @Test(expectedExceptions=NullPointerException.class) 728 public void test_with_adjustment_null() { 729 TEST_2008_6_30_11_30_59_000000500.with((TemporalAdjuster) null); 730 } 731 732 @Test(expectedExceptions=NullPointerException.class) 733 public void test_withOffsetSameLocal_null() { 734 OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE); 735 base.withOffsetSameLocal(null); 736 } 737 738 //----------------------------------------------------------------------- 739 // withOffsetSameInstant() 740 //----------------------------------------------------------------------- 741 @Test 742 public void test_withOffsetSameInstant() { 743 OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE); 744 OffsetDateTime test = base.withOffsetSameInstant(OFFSET_PTWO); 745 OffsetDateTime expected = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(12, 30, 59), OFFSET_PTWO); 746 assertEquals(test, expected); 747 } 748 749 @Test(expectedExceptions=NullPointerException.class) 750 public void test_withOffsetSameInstant_null() { 751 OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE); 752 base.withOffsetSameInstant(null); 753 } 754 755 //----------------------------------------------------------------------- 756 // with(long,TemporalUnit) 757 //----------------------------------------------------------------------- 758 @DataProvider(name = "withFieldLong") 759 Object[][] data_withFieldLong() { 760 return new Object[][] { 761 {TEST_2008_6_30_11_30_59_000000500, YEAR, 2009, 762 OffsetDateTime.of(2009, 6, 30, 11, 30, 59, 500, OFFSET_PONE)}, 763 {TEST_2008_6_30_11_30_59_000000500, MONTH_OF_YEAR, 7, 764 OffsetDateTime.of(2008, 7, 30, 11, 30, 59, 500, OFFSET_PONE)}, 765 {TEST_2008_6_30_11_30_59_000000500, DAY_OF_MONTH, 15, 766 OffsetDateTime.of(2008, 6, 15, 11, 30, 59, 500, OFFSET_PONE)}, 767 {TEST_2008_6_30_11_30_59_000000500, HOUR_OF_DAY, 14, 768 OffsetDateTime.of(2008, 6, 30, 14, 30, 59, 500, OFFSET_PONE)}, 769 {TEST_2008_6_30_11_30_59_000000500, OFFSET_SECONDS, -3600, 770 OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 500, OFFSET_MONE)}, 771 }; 772 }; 773 774 @Test(dataProvider = "withFieldLong") 775 public void test_with_fieldLong(OffsetDateTime base, TemporalField setField, long setValue, OffsetDateTime expected) { 776 assertEquals(base.with(setField, setValue), expected); 777 } 778 779 //----------------------------------------------------------------------- 780 // withYear() 781 //----------------------------------------------------------------------- 782 @Test 783 public void test_withYear_normal() { 784 OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE); 785 OffsetDateTime test = base.withYear(2007); 786 assertEquals(test, OffsetDateTime.of(LocalDate.of(2007, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE)); 787 } 788 789 //----------------------------------------------------------------------- 790 // withMonth() 791 //----------------------------------------------------------------------- 792 @Test 793 public void test_withMonth_normal() { 794 OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE); 795 OffsetDateTime test = base.withMonth(1); 796 assertEquals(test, OffsetDateTime.of(LocalDate.of(2008, 1, 30), LocalTime.of(11, 30, 59), OFFSET_PONE)); 797 } 798 799 //----------------------------------------------------------------------- 800 // withDayOfMonth() 801 //----------------------------------------------------------------------- 802 @Test 803 public void test_withDayOfMonth_normal() { 804 OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE); 805 OffsetDateTime test = base.withDayOfMonth(15); 806 assertEquals(test, OffsetDateTime.of(LocalDate.of(2008, 6, 15), LocalTime.of(11, 30, 59), OFFSET_PONE)); 807 } 808 809 //----------------------------------------------------------------------- 810 // withDayOfYear(int) 811 //----------------------------------------------------------------------- 812 @Test 813 public void test_withDayOfYear_normal() { 814 OffsetDateTime t = TEST_2008_6_30_11_30_59_000000500.withDayOfYear(33); 815 assertEquals(t, OffsetDateTime.of(LocalDate.of(2008, 2, 2), LocalTime.of(11, 30, 59, 500), OFFSET_PONE)); 816 } 817 818 @Test(expectedExceptions=DateTimeException.class) 819 public void test_withDayOfYear_illegal() { 820 TEST_2008_6_30_11_30_59_000000500.withDayOfYear(367); 821 } 822 823 @Test(expectedExceptions=DateTimeException.class) 824 public void test_withDayOfYear_invalid() { 825 OffsetDateTime.of(LocalDate.of(2007, 2, 2), LocalTime.of(11, 30), OFFSET_PONE).withDayOfYear(366); 826 } 827 828 //----------------------------------------------------------------------- 829 // withHour() 830 //----------------------------------------------------------------------- 831 @Test 832 public void test_withHour_normal() { 833 OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE); 834 OffsetDateTime test = base.withHour(15); 835 assertEquals(test, OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(15, 30, 59), OFFSET_PONE)); 836 } 837 838 //----------------------------------------------------------------------- 839 // withMinute() 840 //----------------------------------------------------------------------- 841 @Test 842 public void test_withMinute_normal() { 843 OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE); 844 OffsetDateTime test = base.withMinute(15); 845 assertEquals(test, OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 15, 59), OFFSET_PONE)); 846 } 847 848 //----------------------------------------------------------------------- 849 // withSecond() 850 //----------------------------------------------------------------------- 851 @Test 852 public void test_withSecond_normal() { 853 OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE); 854 OffsetDateTime test = base.withSecond(15); 855 assertEquals(test, OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 15), OFFSET_PONE)); 856 } 857 858 //----------------------------------------------------------------------- 859 // withNano() 860 //----------------------------------------------------------------------- 861 @Test 862 public void test_withNanoOfSecond_normal() { 863 OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59, 1), OFFSET_PONE); 864 OffsetDateTime test = base.withNano(15); 865 assertEquals(test, OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59, 15), OFFSET_PONE)); 866 } 867 868 //----------------------------------------------------------------------- 869 // truncatedTo(TemporalUnit) 870 //----------------------------------------------------------------------- 871 @Test 872 public void test_truncatedTo_normal() { 873 assertEquals(TEST_2008_6_30_11_30_59_000000500.truncatedTo(NANOS), TEST_2008_6_30_11_30_59_000000500); 874 assertEquals(TEST_2008_6_30_11_30_59_000000500.truncatedTo(SECONDS), TEST_2008_6_30_11_30_59_000000500.withNano(0)); 875 assertEquals(TEST_2008_6_30_11_30_59_000000500.truncatedTo(DAYS), TEST_2008_6_30_11_30_59_000000500.with(LocalTime.MIDNIGHT)); 876 } 877 878 @Test(expectedExceptions=NullPointerException.class) 879 public void test_truncatedTo_null() { 880 TEST_2008_6_30_11_30_59_000000500.truncatedTo(null); 881 } 882 883 //----------------------------------------------------------------------- 884 // plus(Period) 885 //----------------------------------------------------------------------- 886 @Test 887 public void test_plus_Period() { 888 MockSimplePeriod period = MockSimplePeriod.of(7, ChronoUnit.MONTHS); 889 OffsetDateTime t = TEST_2008_6_30_11_30_59_000000500.plus(period); 890 assertEquals(t, OffsetDateTime.of(2009, 1, 30, 11, 30, 59, 500, OFFSET_PONE)); 891 } 892 893 //----------------------------------------------------------------------- 894 // plus(Duration) 895 //----------------------------------------------------------------------- 896 @Test 897 public void test_plus_Duration() { 898 Duration dur = Duration.ofSeconds(62, 3); 899 OffsetDateTime t = TEST_2008_6_30_11_30_59_000000500.plus(dur); 900 assertEquals(t, OffsetDateTime.of(2008, 6, 30, 11, 32, 1, 503, OFFSET_PONE)); 901 } 902 903 @Test 904 public void test_plus_Duration_zero() { 905 OffsetDateTime t = TEST_2008_6_30_11_30_59_000000500.plus(Duration.ZERO); 906 assertEquals(t, TEST_2008_6_30_11_30_59_000000500); 907 } 908 909 @Test(expectedExceptions=NullPointerException.class) 910 public void test_plus_Duration_null() { 911 TEST_2008_6_30_11_30_59_000000500.plus((Duration) null); 912 } 913 914 //----------------------------------------------------------------------- 915 // plusYears() 916 //----------------------------------------------------------------------- 917 @Test 918 public void test_plusYears() { 919 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 920 OffsetDateTime test = base.plusYears(1); 921 assertEquals(test, OffsetDateTime.of(2009, 6, 30, 11, 30, 59, 0, OFFSET_PONE)); 922 } 923 924 //----------------------------------------------------------------------- 925 // plusMonths() 926 //----------------------------------------------------------------------- 927 @Test 928 public void test_plusMonths() { 929 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 930 OffsetDateTime test = base.plusMonths(1); 931 assertEquals(test, OffsetDateTime.of(2008, 7, 30, 11, 30, 59, 0, OFFSET_PONE)); 932 } 933 934 //----------------------------------------------------------------------- 935 // plusWeeks() 936 //----------------------------------------------------------------------- 937 @Test 938 public void test_plusWeeks() { 939 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 940 OffsetDateTime test = base.plusWeeks(1); 941 assertEquals(test, OffsetDateTime.of(2008, 7, 7, 11, 30, 59, 0, OFFSET_PONE)); 942 } 943 944 //----------------------------------------------------------------------- 945 // plusDays() 946 //----------------------------------------------------------------------- 947 @Test 948 public void test_plusDays() { 949 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 950 OffsetDateTime test = base.plusDays(1); 951 assertEquals(test, OffsetDateTime.of(2008, 7, 1, 11, 30, 59, 0, OFFSET_PONE)); 952 } 953 954 //----------------------------------------------------------------------- 955 // plusHours() 956 //----------------------------------------------------------------------- 957 @Test 958 public void test_plusHours() { 959 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 960 OffsetDateTime test = base.plusHours(13); 961 assertEquals(test, OffsetDateTime.of(2008, 7, 1, 0, 30, 59, 0, OFFSET_PONE)); 962 } 963 964 //----------------------------------------------------------------------- 965 // plusMinutes() 966 //----------------------------------------------------------------------- 967 @Test 968 public void test_plusMinutes() { 969 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 970 OffsetDateTime test = base.plusMinutes(30); 971 assertEquals(test, OffsetDateTime.of(2008, 6, 30, 12, 0, 59, 0, OFFSET_PONE)); 972 } 973 974 //----------------------------------------------------------------------- 975 // plusSeconds() 976 //----------------------------------------------------------------------- 977 @Test 978 public void test_plusSeconds() { 979 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 980 OffsetDateTime test = base.plusSeconds(1); 981 assertEquals(test, OffsetDateTime.of(2008, 6, 30, 11, 31, 0, 0, OFFSET_PONE)); 982 } 983 984 //----------------------------------------------------------------------- 985 // plusNanos() 986 //----------------------------------------------------------------------- 987 @Test 988 public void test_plusNanos() { 989 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 990 OffsetDateTime test = base.plusNanos(1); 991 assertEquals(test, OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 1, OFFSET_PONE)); 992 } 993 994 //----------------------------------------------------------------------- 995 // minus(Period) 996 //----------------------------------------------------------------------- 997 @Test 998 public void test_minus_Period() { 999 MockSimplePeriod period = MockSimplePeriod.of(7, ChronoUnit.MONTHS); 1000 OffsetDateTime t = TEST_2008_6_30_11_30_59_000000500.minus(period); 1001 assertEquals(t, OffsetDateTime.of(2007, 11, 30, 11, 30, 59, 500, OFFSET_PONE)); 1002 } 1003 1004 //----------------------------------------------------------------------- 1005 // minus(Duration) 1006 //----------------------------------------------------------------------- 1007 @Test 1008 public void test_minus_Duration() { 1009 Duration dur = Duration.ofSeconds(62, 3); 1010 OffsetDateTime t = TEST_2008_6_30_11_30_59_000000500.minus(dur); 1011 assertEquals(t, OffsetDateTime.of(2008, 6, 30, 11, 29, 57, 497, OFFSET_PONE)); 1012 } 1013 1014 @Test 1015 public void test_minus_Duration_zero() { 1016 OffsetDateTime t = TEST_2008_6_30_11_30_59_000000500.minus(Duration.ZERO); 1017 assertEquals(t, TEST_2008_6_30_11_30_59_000000500); 1018 } 1019 1020 @Test(expectedExceptions=NullPointerException.class) 1021 public void test_minus_Duration_null() { 1022 TEST_2008_6_30_11_30_59_000000500.minus((Duration) null); 1023 } 1024 1025 //----------------------------------------------------------------------- 1026 // minusYears() 1027 //----------------------------------------------------------------------- 1028 @Test 1029 public void test_minusYears() { 1030 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 1031 OffsetDateTime test = base.minusYears(1); 1032 assertEquals(test, OffsetDateTime.of(2007, 6, 30, 11, 30, 59, 0, OFFSET_PONE)); 1033 } 1034 1035 //----------------------------------------------------------------------- 1036 // minusMonths() 1037 //----------------------------------------------------------------------- 1038 @Test 1039 public void test_minusMonths() { 1040 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 1041 OffsetDateTime test = base.minusMonths(1); 1042 assertEquals(test, OffsetDateTime.of(2008, 5, 30, 11, 30, 59, 0, OFFSET_PONE)); 1043 } 1044 1045 //----------------------------------------------------------------------- 1046 // minusWeeks() 1047 //----------------------------------------------------------------------- 1048 @Test 1049 public void test_minusWeeks() { 1050 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 1051 OffsetDateTime test = base.minusWeeks(1); 1052 assertEquals(test, OffsetDateTime.of(2008, 6, 23, 11, 30, 59, 0, OFFSET_PONE)); 1053 } 1054 1055 //----------------------------------------------------------------------- 1056 // minusDays() 1057 //----------------------------------------------------------------------- 1058 @Test 1059 public void test_minusDays() { 1060 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 1061 OffsetDateTime test = base.minusDays(1); 1062 assertEquals(test, OffsetDateTime.of(2008, 6, 29, 11, 30, 59, 0, OFFSET_PONE)); 1063 } 1064 1065 //----------------------------------------------------------------------- 1066 // minusHours() 1067 //----------------------------------------------------------------------- 1068 @Test 1069 public void test_minusHours() { 1070 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 1071 OffsetDateTime test = base.minusHours(13); 1072 assertEquals(test, OffsetDateTime.of(2008, 6, 29, 22, 30, 59, 0, OFFSET_PONE)); 1073 } 1074 1075 //----------------------------------------------------------------------- 1076 // minusMinutes() 1077 //----------------------------------------------------------------------- 1078 @Test 1079 public void test_minusMinutes() { 1080 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 1081 OffsetDateTime test = base.minusMinutes(30); 1082 assertEquals(test, OffsetDateTime.of(2008, 6, 30, 11, 0, 59, 0, OFFSET_PONE)); 1083 } 1084 1085 //----------------------------------------------------------------------- 1086 // minusSeconds() 1087 //----------------------------------------------------------------------- 1088 @Test 1089 public void test_minusSeconds() { 1090 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 1091 OffsetDateTime test = base.minusSeconds(1); 1092 assertEquals(test, OffsetDateTime.of(2008, 6, 30, 11, 30, 58, 0, OFFSET_PONE)); 1093 } 1094 1095 //----------------------------------------------------------------------- 1096 // minusNanos() 1097 //----------------------------------------------------------------------- 1098 @Test 1099 public void test_minusNanos() { 1100 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 1101 OffsetDateTime test = base.minusNanos(1); 1102 assertEquals(test, OffsetDateTime.of(2008, 6, 30, 11, 30, 58, 999999999, OFFSET_PONE)); 1103 } 1104 1105 //----------------------------------------------------------------------- 1106 // format(DateTimeFormatter) 1107 //----------------------------------------------------------------------- 1108 @Test 1109 public void test_format_formatter() { 1110 DateTimeFormatter f = DateTimeFormatter.ofPattern("y M d H m s"); 1111 String t = OffsetDateTime.of(2010, 12, 3, 11, 30, 0, 0, OFFSET_PONE).format(f); 1112 assertEquals(t, "2010 12 3 11 30 0"); 1113 } 1114 1115 @Test(expectedExceptions=NullPointerException.class) 1116 public void test_format_formatter_null() { 1117 OffsetDateTime.of(2010, 12, 3, 11, 30, 0, 0, OFFSET_PONE).format(null); 1118 } 1119 1120 //----------------------------------------------------------------------- 1121 // atZoneSameInstant() 1122 //----------------------------------------------------------------------- 1123 @Test 1124 public void test_atZone() { 1125 OffsetDateTime t = OffsetDateTime.of(2008, 6, 30, 11, 30, 0, 0, OFFSET_MTWO); 1126 assertEquals(t.atZoneSameInstant(ZONE_PARIS), 1127 ZonedDateTime.of(2008, 6, 30, 15, 30, 0, 0, ZONE_PARIS)); 1128 } 1129 1130 @Test(expectedExceptions=NullPointerException.class) 1131 public void test_atZone_nullTimeZone() { 1132 OffsetDateTime t = OffsetDateTime.of(2008, 6, 30, 11, 30, 0, 0, OFFSET_PTWO); 1133 t.atZoneSameInstant((ZoneId) null); 1134 } 1135 1136 //----------------------------------------------------------------------- 1137 // atZoneSimilarLocal() 1138 //----------------------------------------------------------------------- 1139 @Test 1140 public void test_atZoneSimilarLocal() { 1141 OffsetDateTime t = OffsetDateTime.of(2008, 6, 30, 11, 30, 0, 0, OFFSET_MTWO); 1142 assertEquals(t.atZoneSimilarLocal(ZONE_PARIS), 1143 ZonedDateTime.of(2008, 6, 30, 11, 30, 0, 0, ZONE_PARIS)); 1144 } 1145 1146 @Test 1147 public void test_atZoneSimilarLocal_dstGap() { 1148 OffsetDateTime t = OffsetDateTime.of(2007, 4, 1, 0, 0, 0, 0, OFFSET_MTWO); 1149 assertEquals(t.atZoneSimilarLocal(ZONE_GAZA), 1150 ZonedDateTime.of(2007, 4, 1, 1, 0, 0, 0, ZONE_GAZA)); 1151 } 1152 1153 @Test 1154 public void test_atZone_dstOverlapSummer() { 1155 OffsetDateTime t = OffsetDateTime.of(2007, 10, 28, 2, 30, 0, 0, OFFSET_PTWO); 1156 assertEquals(t.atZoneSimilarLocal(ZONE_PARIS).toLocalDateTime(), t.toLocalDateTime()); 1157 assertEquals(t.atZoneSimilarLocal(ZONE_PARIS).getOffset(), OFFSET_PTWO); 1158 assertEquals(t.atZoneSimilarLocal(ZONE_PARIS).getZone(), ZONE_PARIS); 1159 } 1160 1161 @Test 1162 public void test_atZone_dstOverlapWinter() { 1163 OffsetDateTime t = OffsetDateTime.of(2007, 10, 28, 2, 30, 0, 0, OFFSET_PONE); 1164 assertEquals(t.atZoneSimilarLocal(ZONE_PARIS).toLocalDateTime(), t.toLocalDateTime()); 1165 assertEquals(t.atZoneSimilarLocal(ZONE_PARIS).getOffset(), OFFSET_PONE); 1166 assertEquals(t.atZoneSimilarLocal(ZONE_PARIS).getZone(), ZONE_PARIS); 1167 } 1168 1169 @Test(expectedExceptions=NullPointerException.class) 1170 public void test_atZoneSimilarLocal_nullTimeZone() { 1171 OffsetDateTime t = OffsetDateTime.of(2008, 6, 30, 11, 30, 0, 0, OFFSET_PTWO); 1172 t.atZoneSimilarLocal((ZoneId) null); 1173 } 1174 1175 //----------------------------------------------------------------------- 1176 // toEpochSecond() 1177 //----------------------------------------------------------------------- 1178 @Test 1179 public void test_toEpochSecond_afterEpoch() { 1180 for (int i = 0; i < 100000; i++) { 1181 OffsetDateTime a = OffsetDateTime.of(1970, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC).plusSeconds(i); 1182 assertEquals(a.toEpochSecond(), i); 1183 } 1184 } 1185 1186 @Test 1187 public void test_toEpochSecond_beforeEpoch() { 1188 for (int i = 0; i < 100000; i++) { 1189 OffsetDateTime a = OffsetDateTime.of(1970, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC).minusSeconds(i); 1190 assertEquals(a.toEpochSecond(), -i); 1191 } 1192 } 1193 1194 //----------------------------------------------------------------------- 1195 // compareTo() 1196 //----------------------------------------------------------------------- 1197 @Test 1198 public void test_compareTo_timeMins() { 1199 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 11, 29, 3, 0, OFFSET_PONE); 1200 OffsetDateTime b = OffsetDateTime.of(2008, 6, 30, 11, 30, 2, 0, OFFSET_PONE); // a is before b due to time 1201 assertEquals(a.compareTo(b) < 0, true); 1202 assertEquals(b.compareTo(a) > 0, true); 1203 assertEquals(a.compareTo(a) == 0, true); 1204 assertEquals(b.compareTo(b) == 0, true); 1205 assertEquals(a.toInstant().compareTo(b.toInstant()) < 0, true); 1206 } 1207 1208 @Test 1209 public void test_compareTo_timeSecs() { 1210 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 11, 29, 2, 0, OFFSET_PONE); 1211 OffsetDateTime b = OffsetDateTime.of(2008, 6, 30, 11, 29, 3, 0, OFFSET_PONE); // a is before b due to time 1212 assertEquals(a.compareTo(b) < 0, true); 1213 assertEquals(b.compareTo(a) > 0, true); 1214 assertEquals(a.compareTo(a) == 0, true); 1215 assertEquals(b.compareTo(b) == 0, true); 1216 assertEquals(a.toInstant().compareTo(b.toInstant()) < 0, true); 1217 } 1218 1219 @Test 1220 public void test_compareTo_timeNanos() { 1221 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 11, 29, 40, 4, OFFSET_PONE); 1222 OffsetDateTime b = OffsetDateTime.of(2008, 6, 30, 11, 29, 40, 5, OFFSET_PONE); // a is before b due to time 1223 assertEquals(a.compareTo(b) < 0, true); 1224 assertEquals(b.compareTo(a) > 0, true); 1225 assertEquals(a.compareTo(a) == 0, true); 1226 assertEquals(b.compareTo(b) == 0, true); 1227 assertEquals(a.toInstant().compareTo(b.toInstant()) < 0, true); 1228 } 1229 1230 @Test 1231 public void test_compareTo_offset() { 1232 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 11, 30, 0, 0, OFFSET_PTWO); 1233 OffsetDateTime b = OffsetDateTime.of(2008, 6, 30, 11, 30, 0, 0, OFFSET_PONE); // a is before b due to offset 1234 assertEquals(a.compareTo(b) < 0, true); 1235 assertEquals(b.compareTo(a) > 0, true); 1236 assertEquals(a.compareTo(a) == 0, true); 1237 assertEquals(b.compareTo(b) == 0, true); 1238 assertEquals(a.toInstant().compareTo(b.toInstant()) < 0, true); 1239 } 1240 1241 @Test 1242 public void test_compareTo_offsetNanos() { 1243 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 11, 30, 40, 6, OFFSET_PTWO); 1244 OffsetDateTime b = OffsetDateTime.of(2008, 6, 30, 11, 30, 40, 5, OFFSET_PONE); // a is before b due to offset 1245 assertEquals(a.compareTo(b) < 0, true); 1246 assertEquals(b.compareTo(a) > 0, true); 1247 assertEquals(a.compareTo(a) == 0, true); 1248 assertEquals(b.compareTo(b) == 0, true); 1249 assertEquals(a.toInstant().compareTo(b.toInstant()) < 0, true); 1250 } 1251 1252 @Test 1253 public void test_compareTo_both() { 1254 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 11, 50, 0, 0, OFFSET_PTWO); 1255 OffsetDateTime b = OffsetDateTime.of(2008, 6, 30, 11, 20, 0, 0, OFFSET_PONE); // a is before b on instant scale 1256 assertEquals(a.compareTo(b) < 0, true); 1257 assertEquals(b.compareTo(a) > 0, true); 1258 assertEquals(a.compareTo(a) == 0, true); 1259 assertEquals(b.compareTo(b) == 0, true); 1260 assertEquals(a.toInstant().compareTo(b.toInstant()) < 0, true); 1261 } 1262 1263 @Test 1264 public void test_compareTo_bothNanos() { 1265 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 11, 20, 40, 4, OFFSET_PTWO); 1266 OffsetDateTime b = OffsetDateTime.of(2008, 6, 30, 10, 20, 40, 5, OFFSET_PONE); // a is before b on instant scale 1267 assertEquals(a.compareTo(b) < 0, true); 1268 assertEquals(b.compareTo(a) > 0, true); 1269 assertEquals(a.compareTo(a) == 0, true); 1270 assertEquals(b.compareTo(b) == 0, true); 1271 assertEquals(a.toInstant().compareTo(b.toInstant()) < 0, true); 1272 } 1273 1274 @Test 1275 public void test_compareTo_hourDifference() { 1276 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 10, 0, 0, 0, OFFSET_PONE); 1277 OffsetDateTime b = OffsetDateTime.of(2008, 6, 30, 11, 0, 0, 0, OFFSET_PTWO); // a is before b despite being same time-line time 1278 assertEquals(a.compareTo(b) < 0, true); 1279 assertEquals(b.compareTo(a) > 0, true); 1280 assertEquals(a.compareTo(a) == 0, true); 1281 assertEquals(b.compareTo(b) == 0, true); 1282 assertEquals(a.toInstant().compareTo(b.toInstant()) == 0, true); 1283 } 1284 1285 @Test 1286 public void test_compareTo_max() { 1287 OffsetDateTime a = OffsetDateTime.of(Year.MAX_VALUE, 12, 31, 23, 59, 0, 0, OFFSET_MONE); 1288 OffsetDateTime b = OffsetDateTime.of(Year.MAX_VALUE, 12, 31, 23, 59, 0, 0, OFFSET_MTWO); // a is before b due to offset 1289 assertEquals(a.compareTo(b) < 0, true); 1290 assertEquals(b.compareTo(a) > 0, true); 1291 assertEquals(a.compareTo(a) == 0, true); 1292 assertEquals(b.compareTo(b) == 0, true); 1293 } 1294 1295 @Test 1296 public void test_compareTo_min() { 1297 OffsetDateTime a = OffsetDateTime.of(Year.MIN_VALUE, 1, 1, 0, 0, 0, 0, OFFSET_PTWO); 1298 OffsetDateTime b = OffsetDateTime.of(Year.MIN_VALUE, 1, 1, 0, 0, 0, 0, OFFSET_PONE); // a is before b due to offset 1299 assertEquals(a.compareTo(b) < 0, true); 1300 assertEquals(b.compareTo(a) > 0, true); 1301 assertEquals(a.compareTo(a) == 0, true); 1302 assertEquals(b.compareTo(b) == 0, true); 1303 } 1304 1305 @Test(expectedExceptions=NullPointerException.class) 1306 public void test_compareTo_null() { 1307 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 1308 a.compareTo(null); 1309 } 1310 1311 @Test(expectedExceptions=ClassCastException.class) 1312 @SuppressWarnings({"unchecked", "rawtypes"}) 1313 public void compareToNonOffsetDateTime() { 1314 Comparable c = TEST_2008_6_30_11_30_59_000000500; 1315 c.compareTo(new Object()); 1316 } 1317 1318 //----------------------------------------------------------------------- 1319 // isAfter() / isBefore() / isEqual() 1320 //----------------------------------------------------------------------- 1321 @Test 1322 public void test_isBeforeIsAfterIsEqual1() { 1323 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 11, 30, 58, 3, OFFSET_PONE); 1324 OffsetDateTime b = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 2, OFFSET_PONE); // a is before b due to time 1325 assertEquals(a.isBefore(b), true); 1326 assertEquals(a.isEqual(b), false); 1327 assertEquals(a.isAfter(b), false); 1328 1329 assertEquals(b.isBefore(a), false); 1330 assertEquals(b.isEqual(a), false); 1331 assertEquals(b.isAfter(a), true); 1332 1333 assertEquals(a.isBefore(a), false); 1334 assertEquals(b.isBefore(b), false); 1335 1336 assertEquals(a.isEqual(a), true); 1337 assertEquals(b.isEqual(b), true); 1338 1339 assertEquals(a.isAfter(a), false); 1340 assertEquals(b.isAfter(b), false); 1341 } 1342 1343 @Test 1344 public void test_isBeforeIsAfterIsEqual2() { 1345 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 2, OFFSET_PONE); 1346 OffsetDateTime b = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 3, OFFSET_PONE); // a is before b due to time 1347 assertEquals(a.isBefore(b), true); 1348 assertEquals(a.isEqual(b), false); 1349 assertEquals(a.isAfter(b), false); 1350 1351 assertEquals(b.isBefore(a), false); 1352 assertEquals(b.isEqual(a), false); 1353 assertEquals(b.isAfter(a), true); 1354 1355 assertEquals(a.isBefore(a), false); 1356 assertEquals(b.isBefore(b), false); 1357 1358 assertEquals(a.isEqual(a), true); 1359 assertEquals(b.isEqual(b), true); 1360 1361 assertEquals(a.isAfter(a), false); 1362 assertEquals(b.isAfter(b), false); 1363 } 1364 1365 @Test 1366 public void test_isBeforeIsAfterIsEqual_instantComparison() { 1367 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 10, 0, 0, 0, OFFSET_PONE); 1368 OffsetDateTime b = OffsetDateTime.of(2008, 6, 30, 11, 0, 0, 0, OFFSET_PTWO); // a is same instant as b 1369 assertEquals(a.isBefore(b), false); 1370 assertEquals(a.isEqual(b), true); 1371 assertEquals(a.isAfter(b), false); 1372 1373 assertEquals(b.isBefore(a), false); 1374 assertEquals(b.isEqual(a), true); 1375 assertEquals(b.isAfter(a), false); 1376 1377 assertEquals(a.isBefore(a), false); 1378 assertEquals(b.isBefore(b), false); 1379 1380 assertEquals(a.isEqual(a), true); 1381 assertEquals(b.isEqual(b), true); 1382 1383 assertEquals(a.isAfter(a), false); 1384 assertEquals(b.isAfter(b), false); 1385 } 1386 1387 @Test(expectedExceptions=NullPointerException.class) 1388 public void test_isBefore_null() { 1389 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 1390 a.isBefore(null); 1391 } 1392 1393 @Test(expectedExceptions=NullPointerException.class) 1394 public void test_isEqual_null() { 1395 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 1396 a.isEqual(null); 1397 } 1398 1399 @Test(expectedExceptions=NullPointerException.class) 1400 public void test_isAfter_null() { 1401 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 1402 a.isAfter(null); 1403 } 1404 1405 //----------------------------------------------------------------------- 1406 // equals() / hashCode() 1407 //----------------------------------------------------------------------- 1408 @Test(dataProvider="sampleTimes") 1409 public void test_equals_true(int y, int o, int d, int h, int m, int s, int n, ZoneOffset ignored) { 1410 OffsetDateTime a = OffsetDateTime.of(y, o, d, h, m, s, n, OFFSET_PONE); 1411 OffsetDateTime b = OffsetDateTime.of(y, o, d, h, m, s, n, OFFSET_PONE); 1412 assertEquals(a.equals(b), true); 1413 assertEquals(a.hashCode() == b.hashCode(), true); 1414 } 1415 @Test(dataProvider="sampleTimes") 1416 public void test_equals_false_year_differs(int y, int o, int d, int h, int m, int s, int n, ZoneOffset ignored) { 1417 OffsetDateTime a = OffsetDateTime.of(y, o, d, h, m, s, n, OFFSET_PONE); 1418 OffsetDateTime b = OffsetDateTime.of(y + 1, o, d, h, m, s, n, OFFSET_PONE); 1419 assertEquals(a.equals(b), false); 1420 } 1421 @Test(dataProvider="sampleTimes") 1422 public void test_equals_false_hour_differs(int y, int o, int d, int h, int m, int s, int n, ZoneOffset ignored) { 1423 h = (h == 23 ? 22 : h); 1424 OffsetDateTime a = OffsetDateTime.of(y, o, d, h, m, s, n, OFFSET_PONE); 1425 OffsetDateTime b = OffsetDateTime.of(y, o, d, h + 1, m, s, n, OFFSET_PONE); 1426 assertEquals(a.equals(b), false); 1427 } 1428 @Test(dataProvider="sampleTimes") 1429 public void test_equals_false_minute_differs(int y, int o, int d, int h, int m, int s, int n, ZoneOffset ignored) { 1430 m = (m == 59 ? 58 : m); 1431 OffsetDateTime a = OffsetDateTime.of(y, o, d, h, m, s, n, OFFSET_PONE); 1432 OffsetDateTime b = OffsetDateTime.of(y, o, d, h, m + 1, s, n, OFFSET_PONE); 1433 assertEquals(a.equals(b), false); 1434 } 1435 @Test(dataProvider="sampleTimes") 1436 public void test_equals_false_second_differs(int y, int o, int d, int h, int m, int s, int n, ZoneOffset ignored) { 1437 s = (s == 59 ? 58 : s); 1438 OffsetDateTime a = OffsetDateTime.of(y, o, d, h, m, s, n, OFFSET_PONE); 1439 OffsetDateTime b = OffsetDateTime.of(y, o, d, h, m, s + 1, n, OFFSET_PONE); 1440 assertEquals(a.equals(b), false); 1441 } 1442 @Test(dataProvider="sampleTimes") 1443 public void test_equals_false_nano_differs(int y, int o, int d, int h, int m, int s, int n, ZoneOffset ignored) { 1444 n = (n == 999999999 ? 999999998 : n); 1445 OffsetDateTime a = OffsetDateTime.of(y, o, d, h, m, s, n, OFFSET_PONE); 1446 OffsetDateTime b = OffsetDateTime.of(y, o, d, h, m, s, n + 1, OFFSET_PONE); 1447 assertEquals(a.equals(b), false); 1448 } 1449 @Test(dataProvider="sampleTimes") 1450 public void test_equals_false_offset_differs(int y, int o, int d, int h, int m, int s, int n, ZoneOffset ignored) { 1451 OffsetDateTime a = OffsetDateTime.of(y, o, d, h, m, s, n, OFFSET_PONE); 1452 OffsetDateTime b = OffsetDateTime.of(y, o, d, h, m, s, n, OFFSET_PTWO); 1453 assertEquals(a.equals(b), false); 1454 } 1455 1456 @Test 1457 public void test_equals_itself_true() { 1458 assertEquals(TEST_2008_6_30_11_30_59_000000500.equals(TEST_2008_6_30_11_30_59_000000500), true); 1459 } 1460 1461 @Test 1462 public void test_equals_string_false() { 1463 assertEquals(TEST_2008_6_30_11_30_59_000000500.equals("2007-07-15"), false); 1464 } 1465 1466 @Test 1467 public void test_equals_null_false() { 1468 assertEquals(TEST_2008_6_30_11_30_59_000000500.equals(null), false); 1469 } 1470 1471 //----------------------------------------------------------------------- 1472 // toString() 1473 //----------------------------------------------------------------------- 1474 @DataProvider(name="sampleToString") 1475 Object[][] provider_sampleToString() { 1476 return new Object[][] { 1477 {2008, 6, 30, 11, 30, 59, 0, "Z", "2008-06-30T11:30:59Z"}, 1478 {2008, 6, 30, 11, 30, 59, 0, "+01:00", "2008-06-30T11:30:59+01:00"}, 1479 {2008, 6, 30, 11, 30, 59, 999000000, "Z", "2008-06-30T11:30:59.999Z"}, 1480 {2008, 6, 30, 11, 30, 59, 999000000, "+01:00", "2008-06-30T11:30:59.999+01:00"}, 1481 {2008, 6, 30, 11, 30, 59, 999000, "Z", "2008-06-30T11:30:59.000999Z"}, 1482 {2008, 6, 30, 11, 30, 59, 999000, "+01:00", "2008-06-30T11:30:59.000999+01:00"}, 1483 {2008, 6, 30, 11, 30, 59, 999, "Z", "2008-06-30T11:30:59.000000999Z"}, 1484 {2008, 6, 30, 11, 30, 59, 999, "+01:00", "2008-06-30T11:30:59.000000999+01:00"}, 1485 }; 1486 } 1487 1488 @Test(dataProvider="sampleToString") 1489 public void test_toString(int y, int o, int d, int h, int m, int s, int n, String offsetId, String expected) { 1490 OffsetDateTime t = OffsetDateTime.of(y, o, d, h, m, s, n, ZoneOffset.of(offsetId)); 1491 String str = t.toString(); 1492 assertEquals(str, expected); 1493 } 1494 1495 } |