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