1 /* 2 * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * This file is available under and governed by the GNU General Public 26 * License version 2 only, as published by the Free Software Foundation. 27 * However, the following notice accompanied the original version of this 28 * file: 29 * 30 * Copyright (c) 2008-2012, Stephen Colebourne & Michael Nascimento Santos 31 * 32 * All rights reserved. 33 * 34 * Redistribution and use in source and binary forms, with or without 35 * modification, are permitted provided that the following conditions are met: 36 * 37 * * Redistributions of source code must retain the above copyright notice, 38 * this list of conditions and the following disclaimer. 39 * 40 * * Redistributions in binary form must reproduce the above copyright notice, 41 * this list of conditions and the following disclaimer in the documentation 42 * and/or other materials provided with the distribution. 43 * 44 * * Neither the name of JSR-310 nor the names of its contributors 45 * may be used to endorse or promote products derived from this software 46 * without specific prior written permission. 47 * 48 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 49 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 50 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 51 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 52 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 53 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 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.FOREVER; 95 import static java.time.temporal.ChronoUnit.HALF_DAYS; 96 import static java.time.temporal.ChronoUnit.HOURS; 97 import static java.time.temporal.ChronoUnit.MICROS; 98 import static java.time.temporal.ChronoUnit.MILLIS; 99 import static java.time.temporal.ChronoUnit.MINUTES; 100 import static java.time.temporal.ChronoUnit.MONTHS; 101 import static java.time.temporal.ChronoUnit.NANOS; 102 import static java.time.temporal.ChronoUnit.SECONDS; 103 import static org.testng.Assert.assertEquals; 104 import static org.testng.Assert.assertTrue; 105 import static org.testng.Assert.fail; 106 107 import java.time.Clock; 108 import java.time.DateTimeException; 109 import java.time.Duration; 110 import java.time.Instant; 111 import java.time.LocalDate; 112 import java.time.LocalDateTime; 113 import java.time.LocalTime; 114 import java.time.Month; 115 import java.time.OffsetDateTime; 116 import java.time.OffsetTime; 117 import java.time.Year; 118 import java.time.ZoneId; 119 import java.time.ZoneOffset; 120 import java.time.ZonedDateTime; 121 import java.time.chrono.IsoChronology; 122 import java.time.format.DateTimeFormatter; 123 import java.time.format.DateTimeParseException; 124 import java.time.temporal.ChronoField; 125 import java.time.temporal.ChronoUnit; 126 import java.time.temporal.JulianFields; 127 import java.time.temporal.Temporal; 128 import java.time.temporal.TemporalAccessor; 129 import java.time.temporal.TemporalAdjuster; 130 import java.time.temporal.TemporalField; 131 import java.time.temporal.TemporalQueries; 132 import java.time.temporal.TemporalQuery; 133 import java.time.temporal.TemporalUnit; 134 import java.util.ArrayList; 135 import java.util.Arrays; 136 import java.util.List; 137 138 import org.testng.annotations.BeforeClass; 139 import org.testng.annotations.DataProvider; 140 import org.testng.annotations.Test; 141 import test.java.time.MockSimplePeriod; 142 143 /** 144 * Test OffsetDateTime. 145 */ 146 @Test 147 public class TCKOffsetDateTime extends AbstractDateTimeTest { 148 149 private static final ZoneId ZONE_PARIS = ZoneId.of("Europe/Paris"); 150 private static final ZoneId ZONE_GAZA = ZoneId.of("Asia/Gaza"); 151 private static final ZoneOffset OFFSET_PONE = ZoneOffset.ofHours(1); 152 private static final ZoneOffset OFFSET_PTWO = ZoneOffset.ofHours(2); 153 private static final ZoneOffset OFFSET_MONE = ZoneOffset.ofHours(-1); 154 private static final ZoneOffset OFFSET_MTWO = ZoneOffset.ofHours(-2); 155 156 // Android-changed: This was originally non-static and initialized in @BeforeMethod, 157 // but @BeforeMethod is run after @DataProvider methods are run, so it only worked by accident, 158 // since multiple test methods were run and the first one did not require this value. 159 private static OffsetDateTime TEST_2008_6_30_11_30_59_000000500; 160 161 @BeforeClass setUp()162 public static void setUp() { 163 TEST_2008_6_30_11_30_59_000000500 = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 500, OFFSET_PONE); 164 } 165 166 //----------------------------------------------------------------------- 167 @Override samples()168 protected List<TemporalAccessor> samples() { 169 TemporalAccessor[] array = {TEST_2008_6_30_11_30_59_000000500, OffsetDateTime.MIN, OffsetDateTime.MAX}; 170 return Arrays.asList(array); 171 } 172 173 @Override validFields()174 protected List<TemporalField> validFields() { 175 TemporalField[] array = { 176 NANO_OF_SECOND, 177 NANO_OF_DAY, 178 MICRO_OF_SECOND, 179 MICRO_OF_DAY, 180 MILLI_OF_SECOND, 181 MILLI_OF_DAY, 182 SECOND_OF_MINUTE, 183 SECOND_OF_DAY, 184 MINUTE_OF_HOUR, 185 MINUTE_OF_DAY, 186 CLOCK_HOUR_OF_AMPM, 187 HOUR_OF_AMPM, 188 CLOCK_HOUR_OF_DAY, 189 HOUR_OF_DAY, 190 AMPM_OF_DAY, 191 DAY_OF_WEEK, 192 ALIGNED_DAY_OF_WEEK_IN_MONTH, 193 ALIGNED_DAY_OF_WEEK_IN_YEAR, 194 DAY_OF_MONTH, 195 DAY_OF_YEAR, 196 EPOCH_DAY, 197 ALIGNED_WEEK_OF_MONTH, 198 ALIGNED_WEEK_OF_YEAR, 199 MONTH_OF_YEAR, 200 PROLEPTIC_MONTH, 201 YEAR_OF_ERA, 202 YEAR, 203 ERA, 204 OFFSET_SECONDS, 205 INSTANT_SECONDS, 206 JulianFields.JULIAN_DAY, 207 JulianFields.MODIFIED_JULIAN_DAY, 208 JulianFields.RATA_DIE, 209 }; 210 return Arrays.asList(array); 211 } 212 213 @Override invalidFields()214 protected List<TemporalField> invalidFields() { 215 List<TemporalField> list = new ArrayList<>(Arrays.<TemporalField>asList(ChronoField.values())); 216 list.removeAll(validFields()); 217 return list; 218 } 219 220 //----------------------------------------------------------------------- 221 // constants 222 //----------------------------------------------------------------------- 223 @Test constant_MIN()224 public void constant_MIN() { 225 check(OffsetDateTime.MIN, Year.MIN_VALUE, 1, 1, 0, 0, 0, 0, ZoneOffset.MAX); 226 } 227 228 @Test constant_MAX()229 public void constant_MAX() { 230 check(OffsetDateTime.MAX, Year.MAX_VALUE, 12, 31, 23, 59, 59, 999999999, ZoneOffset.MIN); 231 } 232 233 //----------------------------------------------------------------------- 234 // now() 235 //----------------------------------------------------------------------- 236 @Test now()237 public void now() { 238 final long DELTA = 20_000_000_000L; // 20 seconds of nanos leeway 239 OffsetDateTime expected = OffsetDateTime.now(Clock.systemDefaultZone()); 240 OffsetDateTime test = OffsetDateTime.now(); 241 long diff = Math.abs(test.toLocalTime().toNanoOfDay() - expected.toLocalTime().toNanoOfDay()); 242 if (diff >= DELTA) { 243 // may be date change 244 expected = OffsetDateTime.now(Clock.systemDefaultZone()); 245 test = OffsetDateTime.now(); 246 diff = Math.abs(test.toLocalTime().toNanoOfDay() - expected.toLocalTime().toNanoOfDay()); 247 } 248 assertTrue(diff < DELTA); 249 } 250 251 //----------------------------------------------------------------------- 252 // now(Clock) 253 //----------------------------------------------------------------------- 254 @Test 255 public void now_Clock_allSecsInDay_utc() { 256 for (int i = 0; i < (2 * 24 * 60 * 60); i++) { 257 Instant instant = Instant.ofEpochSecond(i).plusNanos(123456789L); 258 Clock clock = Clock.fixed(instant, ZoneOffset.UTC); 259 OffsetDateTime test = OffsetDateTime.now(clock); 260 assertEquals(test.getYear(), 1970); 261 assertEquals(test.getMonth(), Month.JANUARY); 262 assertEquals(test.getDayOfMonth(), (i < 24 * 60 * 60 ? 1 : 2)); 263 assertEquals(test.getHour(), (i / (60 * 60)) % 24); 264 assertEquals(test.getMinute(), (i / 60) % 60); 265 assertEquals(test.getSecond(), i % 60); 266 assertEquals(test.getNano(), 123456789); 267 assertEquals(test.getOffset(), ZoneOffset.UTC); 268 } 269 } 270 271 @Test 272 public void now_Clock_allSecsInDay_offset() { 273 for (int i = 0; i < (2 * 24 * 60 * 60); i++) { 274 Instant instant = Instant.ofEpochSecond(i).plusNanos(123456789L); 275 Clock clock = Clock.fixed(instant.minusSeconds(OFFSET_PONE.getTotalSeconds()), OFFSET_PONE); 276 OffsetDateTime test = OffsetDateTime.now(clock); 277 assertEquals(test.getYear(), 1970); 278 assertEquals(test.getMonth(), Month.JANUARY); 279 assertEquals(test.getDayOfMonth(), (i < 24 * 60 * 60) ? 1 : 2); 280 assertEquals(test.getHour(), (i / (60 * 60)) % 24); 281 assertEquals(test.getMinute(), (i / 60) % 60); 282 assertEquals(test.getSecond(), i % 60); 283 assertEquals(test.getNano(), 123456789); 284 assertEquals(test.getOffset(), OFFSET_PONE); 285 } 286 } 287 288 @Test 289 public void now_Clock_allSecsInDay_beforeEpoch() { 290 LocalTime expected = LocalTime.MIDNIGHT.plusNanos(123456789L); 291 for (int i =-1; i >= -(24 * 60 * 60); i--) { 292 Instant instant = Instant.ofEpochSecond(i).plusNanos(123456789L); 293 Clock clock = Clock.fixed(instant, ZoneOffset.UTC); 294 OffsetDateTime test = OffsetDateTime.now(clock); 295 assertEquals(test.getYear(), 1969); 296 assertEquals(test.getMonth(), Month.DECEMBER); 297 assertEquals(test.getDayOfMonth(), 31); 298 expected = expected.minusSeconds(1); 299 assertEquals(test.toLocalTime(), expected); 300 assertEquals(test.getOffset(), ZoneOffset.UTC); 301 } 302 } 303 304 @Test 305 public void now_Clock_offsets() { 306 OffsetDateTime base = OffsetDateTime.of(1970, 1, 1, 12, 0, 0, 0, ZoneOffset.UTC); 307 for (int i = -9; i < 15; i++) { 308 ZoneOffset offset = ZoneOffset.ofHours(i); 309 Clock clock = Clock.fixed(base.toInstant(), offset); 310 OffsetDateTime test = OffsetDateTime.now(clock); 311 assertEquals(test.getHour(), (12 + i) % 24); 312 assertEquals(test.getMinute(), 0); 313 assertEquals(test.getSecond(), 0); 314 assertEquals(test.getNano(), 0); 315 assertEquals(test.getOffset(), offset); 316 } 317 } 318 319 @Test(expectedExceptions=NullPointerException.class) 320 public void now_Clock_nullZoneId() { 321 OffsetDateTime.now((ZoneId) null); 322 } 323 324 @Test(expectedExceptions=NullPointerException.class) 325 public void now_Clock_nullClock() { 326 OffsetDateTime.now((Clock) null); 327 } 328 329 //----------------------------------------------------------------------- 330 private void check(OffsetDateTime test, int y, int mo, int d, int h, int m, int s, int n, ZoneOffset offset) { 331 assertEquals(test.getYear(), y); 332 assertEquals(test.getMonth().getValue(), mo); 333 assertEquals(test.getDayOfMonth(), d); 334 assertEquals(test.getHour(), h); 335 assertEquals(test.getMinute(), m); 336 assertEquals(test.getSecond(), s); 337 assertEquals(test.getNano(), n); 338 assertEquals(test.getOffset(), offset); 339 assertEquals(test, test); 340 assertEquals(test.hashCode(), test.hashCode()); 341 assertEquals(OffsetDateTime.of(LocalDateTime.of(y, mo, d, h, m, s, n), offset), test); 342 } 343 344 //----------------------------------------------------------------------- 345 // factories 346 //----------------------------------------------------------------------- 347 @Test 348 public void factory_of_intsHMSN() { 349 OffsetDateTime test = OffsetDateTime.of(2008, 6, 30, 11, 30, 10, 500, OFFSET_PONE); 350 check(test, 2008, 6, 30, 11, 30, 10, 500, OFFSET_PONE); 351 } 352 353 //----------------------------------------------------------------------- 354 @Test 355 public void factory_of_LocalDateLocalTimeZoneOffset() { 356 LocalDate date = LocalDate.of(2008, 6, 30); 357 LocalTime time = LocalTime.of(11, 30, 10, 500); 358 OffsetDateTime test = OffsetDateTime.of(date, time, OFFSET_PONE); 359 check(test, 2008, 6, 30, 11, 30, 10, 500, OFFSET_PONE); 360 } 361 362 @Test(expectedExceptions=NullPointerException.class) 363 public void factory_of_LocalDateLocalTimeZoneOffset_nullLocalDate() { 364 LocalTime time = LocalTime.of(11, 30, 10, 500); 365 OffsetDateTime.of((LocalDate) null, time, OFFSET_PONE); 366 } 367 368 @Test(expectedExceptions=NullPointerException.class) 369 public void factory_of_LocalDateLocalTimeZoneOffset_nullLocalTime() { 370 LocalDate date = LocalDate.of(2008, 6, 30); 371 OffsetDateTime.of(date, (LocalTime) null, OFFSET_PONE); 372 } 373 374 @Test(expectedExceptions=NullPointerException.class) 375 public void factory_of_LocalDateLocalTimeZoneOffset_nullOffset() { 376 LocalDate date = LocalDate.of(2008, 6, 30); 377 LocalTime time = LocalTime.of(11, 30, 10, 500); 378 OffsetDateTime.of(date, time, (ZoneOffset) null); 379 } 380 381 //----------------------------------------------------------------------- 382 @Test 383 public void factory_of_LocalDateTimeZoneOffset() { 384 LocalDateTime dt = LocalDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 10, 500)); 385 OffsetDateTime test = OffsetDateTime.of(dt, 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_LocalDateTimeZoneOffset_nullProvider() { 391 OffsetDateTime.of((LocalDateTime) null, OFFSET_PONE); 392 } 393 394 @Test(expectedExceptions=NullPointerException.class) 395 public void factory_of_LocalDateTimeZoneOffset_nullOffset() { 396 LocalDateTime dt = LocalDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 10, 500)); 397 OffsetDateTime.of(dt, (ZoneOffset) null); 398 } 399 400 //----------------------------------------------------------------------- 401 // from() 402 //----------------------------------------------------------------------- 403 @Test 404 public void test_factory_CalendricalObject() { 405 assertEquals(OffsetDateTime.from( 406 OffsetDateTime.of(LocalDate.of(2007, 7, 15), LocalTime.of(17, 30), OFFSET_PONE)), 407 OffsetDateTime.of(LocalDate.of(2007, 7, 15), LocalTime.of(17, 30), OFFSET_PONE)); 408 } 409 410 @Test(expectedExceptions=DateTimeException.class) 411 public void test_factory_CalendricalObject_invalid_noDerive() { 412 OffsetDateTime.from(LocalTime.of(12, 30)); 413 } 414 415 @Test(expectedExceptions=NullPointerException.class) 416 public void test_factory_Calendricals_null() { 417 OffsetDateTime.from((TemporalAccessor) null); 418 } 419 420 //----------------------------------------------------------------------- 421 // parse() 422 //----------------------------------------------------------------------- 423 @Test(dataProvider="sampleToString") 424 public void test_parse(int y, int month, int d, int h, int m, int s, int n, String offsetId, String text) { 425 OffsetDateTime t = OffsetDateTime.parse(text); 426 assertEquals(t.getYear(), y); 427 assertEquals(t.getMonth().getValue(), month); 428 assertEquals(t.getDayOfMonth(), d); 429 assertEquals(t.getHour(), h); 430 assertEquals(t.getMinute(), m); 431 assertEquals(t.getSecond(), s); 432 assertEquals(t.getNano(), n); 433 assertEquals(t.getOffset().getId(), offsetId); 434 } 435 436 @Test(expectedExceptions=DateTimeParseException.class) 437 public void factory_parse_illegalValue() { 438 OffsetDateTime.parse("2008-06-32T11:15+01:00"); 439 } 440 441 @Test(expectedExceptions=DateTimeParseException.class) 442 public void factory_parse_invalidValue() { 443 OffsetDateTime.parse("2008-06-31T11:15+01:00"); 444 } 445 446 @Test(expectedExceptions=NullPointerException.class) 447 public void factory_parse_nullText() { 448 OffsetDateTime.parse((String) null); 449 } 450 451 //----------------------------------------------------------------------- 452 // parse(DateTimeFormatter) 453 //----------------------------------------------------------------------- 454 @Test 455 public void factory_parse_formatter() { 456 DateTimeFormatter f = DateTimeFormatter.ofPattern("y M d H m s XXX"); 457 OffsetDateTime test = OffsetDateTime.parse("2010 12 3 11 30 0 +01:00", f); 458 assertEquals(test, OffsetDateTime.of(LocalDate.of(2010, 12, 3), LocalTime.of(11, 30), ZoneOffset.ofHours(1))); 459 } 460 461 @Test(expectedExceptions=NullPointerException.class) 462 public void factory_parse_formatter_nullText() { 463 DateTimeFormatter f = DateTimeFormatter.ofPattern("y M d H m s"); 464 OffsetDateTime.parse((String) null, f); 465 } 466 467 @Test(expectedExceptions=NullPointerException.class) 468 public void factory_parse_formatter_nullFormatter() { 469 OffsetDateTime.parse("ANY", null); 470 } 471 472 //----------------------------------------------------------------------- 473 @Test(expectedExceptions=NullPointerException.class) 474 public void constructor_nullTime() throws Throwable { 475 OffsetDateTime.of(null, OFFSET_PONE); 476 } 477 478 @Test(expectedExceptions=NullPointerException.class) 479 public void constructor_nullOffset() throws Throwable { 480 OffsetDateTime.of(LocalDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30)), null); 481 } 482 483 //----------------------------------------------------------------------- 484 // basics 485 //----------------------------------------------------------------------- 486 @DataProvider(name="sampleTimes") 487 Object[][] provider_sampleTimes() { 488 return new Object[][] { 489 {2008, 6, 30, 11, 30, 20, 500, OFFSET_PONE}, 490 {2008, 6, 30, 11, 0, 0, 0, OFFSET_PONE}, 491 {2008, 6, 30, 23, 59, 59, 999999999, OFFSET_PONE}, 492 {-1, 1, 1, 0, 0, 0, 0, OFFSET_PONE}, 493 }; 494 } 495 496 @Test(dataProvider="sampleTimes") 497 public void test_get(int y, int o, int d, int h, int m, int s, int n, ZoneOffset offset) { 498 LocalDate localDate = LocalDate.of(y, o, d); 499 LocalTime localTime = LocalTime.of(h, m, s, n); 500 LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime); 501 OffsetDateTime a = OffsetDateTime.of(localDateTime, offset); 502 503 assertEquals(a.getYear(), localDate.getYear()); 504 assertEquals(a.getMonth(), localDate.getMonth()); 505 assertEquals(a.getDayOfMonth(), localDate.getDayOfMonth()); 506 assertEquals(a.getDayOfYear(), localDate.getDayOfYear()); 507 assertEquals(a.getDayOfWeek(), localDate.getDayOfWeek()); 508 509 assertEquals(a.getHour(), localDateTime.getHour()); 510 assertEquals(a.getMinute(), localDateTime.getMinute()); 511 assertEquals(a.getSecond(), localDateTime.getSecond()); 512 assertEquals(a.getNano(), localDateTime.getNano()); 513 514 assertEquals(a.toOffsetTime(), OffsetTime.of(localTime, offset)); 515 assertEquals(a.toString(), localDateTime.toString() + offset.toString()); 516 } 517 518 //----------------------------------------------------------------------- 519 // isSupported(TemporalField) 520 //----------------------------------------------------------------------- 521 @Test 522 public void test_isSupported_TemporalField() { 523 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported((TemporalField) null), false); 524 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.NANO_OF_SECOND), true); 525 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.NANO_OF_DAY), true); 526 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.MICRO_OF_SECOND), true); 527 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.MICRO_OF_DAY), true); 528 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.MILLI_OF_SECOND), true); 529 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.MILLI_OF_DAY), true); 530 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.SECOND_OF_MINUTE), true); 531 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.SECOND_OF_DAY), true); 532 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.MINUTE_OF_HOUR), true); 533 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.MINUTE_OF_DAY), true); 534 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.HOUR_OF_AMPM), true); 535 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.CLOCK_HOUR_OF_AMPM), true); 536 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.HOUR_OF_DAY), true); 537 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.CLOCK_HOUR_OF_DAY), true); 538 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.AMPM_OF_DAY), true); 539 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.DAY_OF_WEEK), true); 540 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH), true); 541 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR), true); 542 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.DAY_OF_MONTH), true); 543 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.DAY_OF_YEAR), true); 544 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.EPOCH_DAY), true); 545 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.ALIGNED_WEEK_OF_MONTH), true); 546 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.ALIGNED_WEEK_OF_YEAR), true); 547 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.MONTH_OF_YEAR), true); 548 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.PROLEPTIC_MONTH), true); 549 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.YEAR), true); 550 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.YEAR_OF_ERA), true); 551 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.ERA), true); 552 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.INSTANT_SECONDS), true); 553 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoField.OFFSET_SECONDS), true); 554 } 555 556 //----------------------------------------------------------------------- 557 // isSupported(TemporalUnit) 558 //----------------------------------------------------------------------- 559 @Test 560 public void test_isSupported_TemporalUnit() { 561 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported((TemporalUnit) null), false); 562 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoUnit.NANOS), true); 563 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoUnit.MICROS), true); 564 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoUnit.MILLIS), true); 565 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoUnit.SECONDS), true); 566 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoUnit.MINUTES), true); 567 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoUnit.HOURS), true); 568 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoUnit.HALF_DAYS), true); 569 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoUnit.DAYS), true); 570 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoUnit.WEEKS), true); 571 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoUnit.MONTHS), true); 572 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoUnit.YEARS), true); 573 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoUnit.DECADES), true); 574 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoUnit.CENTURIES), true); 575 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoUnit.MILLENNIA), true); 576 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoUnit.ERAS), true); 577 assertEquals(TEST_2008_6_30_11_30_59_000000500.isSupported(ChronoUnit.FOREVER), false); 578 } 579 580 //----------------------------------------------------------------------- 581 // get(TemporalField) 582 //----------------------------------------------------------------------- 583 @Test 584 public void test_get_TemporalField() { 585 OffsetDateTime test = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(12, 30, 40, 987654321), OFFSET_PONE); 586 assertEquals(test.get(ChronoField.YEAR), 2008); 587 assertEquals(test.get(ChronoField.MONTH_OF_YEAR), 6); 588 assertEquals(test.get(ChronoField.DAY_OF_MONTH), 30); 589 assertEquals(test.get(ChronoField.DAY_OF_WEEK), 1); 590 assertEquals(test.get(ChronoField.DAY_OF_YEAR), 182); 591 592 assertEquals(test.get(ChronoField.HOUR_OF_DAY), 12); 593 assertEquals(test.get(ChronoField.MINUTE_OF_HOUR), 30); 594 assertEquals(test.get(ChronoField.SECOND_OF_MINUTE), 40); 595 assertEquals(test.get(ChronoField.NANO_OF_SECOND), 987654321); 596 assertEquals(test.get(ChronoField.HOUR_OF_AMPM), 0); 597 assertEquals(test.get(ChronoField.AMPM_OF_DAY), 1); 598 599 assertEquals(test.get(ChronoField.OFFSET_SECONDS), 3600); 600 } 601 602 @Test 603 public void test_getLong_TemporalField() { 604 OffsetDateTime test = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(12, 30, 40, 987654321), OFFSET_PONE); 605 assertEquals(test.getLong(ChronoField.YEAR), 2008); 606 assertEquals(test.getLong(ChronoField.MONTH_OF_YEAR), 6); 607 assertEquals(test.getLong(ChronoField.DAY_OF_MONTH), 30); 608 assertEquals(test.getLong(ChronoField.DAY_OF_WEEK), 1); 609 assertEquals(test.getLong(ChronoField.DAY_OF_YEAR), 182); 610 611 assertEquals(test.getLong(ChronoField.HOUR_OF_DAY), 12); 612 assertEquals(test.getLong(ChronoField.MINUTE_OF_HOUR), 30); 613 assertEquals(test.getLong(ChronoField.SECOND_OF_MINUTE), 40); 614 assertEquals(test.getLong(ChronoField.NANO_OF_SECOND), 987654321); 615 assertEquals(test.getLong(ChronoField.HOUR_OF_AMPM), 0); 616 assertEquals(test.getLong(ChronoField.AMPM_OF_DAY), 1); 617 618 assertEquals(test.getLong(ChronoField.INSTANT_SECONDS), test.toEpochSecond()); 619 assertEquals(test.getLong(ChronoField.OFFSET_SECONDS), 3600); 620 } 621 622 //----------------------------------------------------------------------- 623 // query(TemporalQuery) 624 //----------------------------------------------------------------------- 625 @DataProvider(name="query") 626 Object[][] data_query() { 627 return new Object[][] { 628 {TEST_2008_6_30_11_30_59_000000500, TemporalQueries.chronology(), IsoChronology.INSTANCE}, 629 {TEST_2008_6_30_11_30_59_000000500, TemporalQueries.zoneId(), null}, 630 {TEST_2008_6_30_11_30_59_000000500, TemporalQueries.precision(), ChronoUnit.NANOS}, 631 {TEST_2008_6_30_11_30_59_000000500, TemporalQueries.zone(), OFFSET_PONE}, 632 {TEST_2008_6_30_11_30_59_000000500, TemporalQueries.offset(), OFFSET_PONE}, 633 {TEST_2008_6_30_11_30_59_000000500, TemporalQueries.localDate(), LocalDate.of(2008, 6, 30)}, 634 {TEST_2008_6_30_11_30_59_000000500, TemporalQueries.localTime(), LocalTime.of(11, 30, 59, 500)}, 635 }; 636 } 637 638 @Test(dataProvider="query") 639 public <T> void test_query(TemporalAccessor temporal, TemporalQuery<T> query, T expected) { 640 assertEquals(temporal.query(query), expected); 641 } 642 643 @Test(dataProvider="query") 644 public <T> void test_queryFrom(TemporalAccessor temporal, TemporalQuery<T> query, T expected) { 645 assertEquals(query.queryFrom(temporal), expected); 646 } 647 648 @Test(expectedExceptions=NullPointerException.class) 649 public void test_query_null() { 650 TEST_2008_6_30_11_30_59_000000500.query(null); 651 } 652 653 //----------------------------------------------------------------------- 654 // adjustInto(Temporal) 655 //----------------------------------------------------------------------- 656 @DataProvider(name="adjustInto") 657 Object[][] data_adjustInto() { 658 return new Object[][]{ 659 {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}, 660 {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}, 661 {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}, 662 {OffsetDateTime.MAX, OffsetDateTime.of(2012, 3, 4, 23, 5, 0, 0, OFFSET_PONE), OffsetDateTime.of(OffsetDateTime.MAX.toLocalDateTime(), ZoneOffset.ofHours(-18)), null}, 663 {OffsetDateTime.MIN, OffsetDateTime.of(2012, 3, 4, 23, 5, 0, 0, OFFSET_PONE), OffsetDateTime.of(OffsetDateTime.MIN.toLocalDateTime(), ZoneOffset.ofHours(18)), null}, 664 665 666 {OffsetDateTime.of(2012, 3, 4, 23, 5, 0, 0, OFFSET_PONE), 667 ZonedDateTime.of(2012, 3, 4, 1, 1, 1, 100, ZONE_GAZA), ZonedDateTime.of(2012, 3, 4, 23, 5, 0, 0, ZONE_GAZA), null}, 668 669 {OffsetDateTime.of(2012, 3, 4, 23, 5, 0, 0, OFFSET_PONE), LocalDateTime.of(2012, 3, 4, 1, 1, 1, 100), null, DateTimeException.class}, 670 {OffsetDateTime.of(2012, 3, 4, 23, 5, 0, 0, OFFSET_PONE), LocalDate.of(2210, 2, 2), null, DateTimeException.class}, 671 {OffsetDateTime.of(2012, 3, 4, 23, 5, 0, 0, OFFSET_PONE), LocalTime.of(22, 3, 0), null, DateTimeException.class}, 672 {OffsetDateTime.of(2012, 3, 4, 23, 5, 0, 0, OFFSET_PONE), OffsetTime.of(22, 3, 0, 0, ZoneOffset.UTC), null, DateTimeException.class}, 673 {OffsetDateTime.of(2012, 3, 4, 23, 5, 0, 0, OFFSET_PONE), null, null, NullPointerException.class}, 674 675 }; 676 } 677 678 @Test(dataProvider="adjustInto") 679 public void test_adjustInto(OffsetDateTime test, Temporal temporal, Temporal expected, Class<?> expectedEx) { 680 if (expectedEx == null) { 681 Temporal result = test.adjustInto(temporal); 682 assertEquals(result, expected); 683 } else { 684 try { 685 Temporal result = test.adjustInto(temporal); 686 fail(); 687 } catch (Exception ex) { 688 assertTrue(expectedEx.isInstance(ex)); 689 } 690 } 691 } 692 693 //----------------------------------------------------------------------- 694 // with(WithAdjuster) 695 //----------------------------------------------------------------------- 696 @Test 697 public void test_with_adjustment() { 698 final OffsetDateTime sample = OffsetDateTime.of(LocalDate.of(2012, 3, 4), LocalTime.of(23, 5), OFFSET_PONE); 699 TemporalAdjuster adjuster = new TemporalAdjuster() { 700 @Override 701 public Temporal adjustInto(Temporal dateTime) { 702 return sample; 703 } 704 }; 705 assertEquals(TEST_2008_6_30_11_30_59_000000500.with(adjuster), sample); 706 } 707 708 @Test 709 public void test_with_adjustment_LocalDate() { 710 OffsetDateTime test = TEST_2008_6_30_11_30_59_000000500.with(LocalDate.of(2012, 9, 3)); 711 assertEquals(test, OffsetDateTime.of(LocalDate.of(2012, 9, 3), LocalTime.of(11, 30, 59, 500), OFFSET_PONE)); 712 } 713 714 @Test 715 public void test_with_adjustment_LocalTime() { 716 OffsetDateTime test = TEST_2008_6_30_11_30_59_000000500.with(LocalTime.of(19, 15)); 717 assertEquals(test, OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(19, 15), OFFSET_PONE)); 718 } 719 720 @Test 721 public void test_with_adjustment_LocalDateTime() { 722 OffsetDateTime test = TEST_2008_6_30_11_30_59_000000500.with(LocalDateTime.of(LocalDate.of(2012, 9, 3), LocalTime.of(19, 15))); 723 assertEquals(test, OffsetDateTime.of(LocalDate.of(2012, 9, 3), LocalTime.of(19, 15), OFFSET_PONE)); 724 } 725 726 @Test 727 public void test_with_adjustment_OffsetTime() { 728 OffsetDateTime test = TEST_2008_6_30_11_30_59_000000500.with(OffsetTime.of(LocalTime.of(19, 15), OFFSET_PTWO)); 729 assertEquals(test, OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(19, 15), OFFSET_PTWO)); 730 } 731 732 @Test 733 public void test_with_adjustment_OffsetDateTime() { 734 OffsetDateTime test = TEST_2008_6_30_11_30_59_000000500.with(OffsetDateTime.of(LocalDate.of(2012, 9, 3), LocalTime.of(19, 15), OFFSET_PTWO)); 735 assertEquals(test, OffsetDateTime.of(LocalDate.of(2012, 9, 3), LocalTime.of(19, 15), OFFSET_PTWO)); 736 } 737 738 @Test 739 public void test_with_adjustment_Month() { 740 OffsetDateTime test = TEST_2008_6_30_11_30_59_000000500.with(DECEMBER); 741 assertEquals(test, OffsetDateTime.of(LocalDate.of(2008, 12, 30),LocalTime.of(11, 30, 59, 500), OFFSET_PONE)); 742 } 743 744 @Test 745 public void test_with_adjustment_ZoneOffset() { 746 OffsetDateTime test = TEST_2008_6_30_11_30_59_000000500.with(OFFSET_PTWO); 747 assertEquals(test, OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59, 500), OFFSET_PTWO)); 748 } 749 750 @Test(expectedExceptions=NullPointerException.class) 751 public void test_with_adjustment_null() { 752 TEST_2008_6_30_11_30_59_000000500.with((TemporalAdjuster) null); 753 } 754 755 @Test(expectedExceptions=NullPointerException.class) 756 public void test_withOffsetSameLocal_null() { 757 OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE); 758 base.withOffsetSameLocal(null); 759 } 760 761 //----------------------------------------------------------------------- 762 // withOffsetSameInstant() 763 //----------------------------------------------------------------------- 764 @Test 765 public void test_withOffsetSameInstant() { 766 OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE); 767 OffsetDateTime test = base.withOffsetSameInstant(OFFSET_PTWO); 768 OffsetDateTime expected = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(12, 30, 59), OFFSET_PTWO); 769 assertEquals(test, expected); 770 } 771 772 @Test(expectedExceptions=NullPointerException.class) 773 public void test_withOffsetSameInstant_null() { 774 OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE); 775 base.withOffsetSameInstant(null); 776 } 777 778 //----------------------------------------------------------------------- 779 // with(long,TemporalUnit) 780 //----------------------------------------------------------------------- 781 @DataProvider(name = "withFieldLong") 782 Object[][] data_withFieldLong() { 783 return new Object[][] { 784 {TEST_2008_6_30_11_30_59_000000500, YEAR, 2009, 785 OffsetDateTime.of(2009, 6, 30, 11, 30, 59, 500, OFFSET_PONE)}, 786 {TEST_2008_6_30_11_30_59_000000500, MONTH_OF_YEAR, 7, 787 OffsetDateTime.of(2008, 7, 30, 11, 30, 59, 500, OFFSET_PONE)}, 788 {TEST_2008_6_30_11_30_59_000000500, DAY_OF_MONTH, 15, 789 OffsetDateTime.of(2008, 6, 15, 11, 30, 59, 500, OFFSET_PONE)}, 790 {TEST_2008_6_30_11_30_59_000000500, HOUR_OF_DAY, 14, 791 OffsetDateTime.of(2008, 6, 30, 14, 30, 59, 500, OFFSET_PONE)}, 792 {TEST_2008_6_30_11_30_59_000000500, OFFSET_SECONDS, -3600, 793 OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 500, OFFSET_MONE)}, 794 }; 795 }; 796 797 @Test(dataProvider = "withFieldLong") 798 public void test_with_fieldLong(OffsetDateTime base, TemporalField setField, long setValue, OffsetDateTime expected) { 799 assertEquals(base.with(setField, setValue), expected); 800 } 801 802 //----------------------------------------------------------------------- 803 // withYear() 804 //----------------------------------------------------------------------- 805 @Test 806 public void test_withYear_normal() { 807 OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE); 808 OffsetDateTime test = base.withYear(2007); 809 assertEquals(test, OffsetDateTime.of(LocalDate.of(2007, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE)); 810 } 811 812 //----------------------------------------------------------------------- 813 // withMonth() 814 //----------------------------------------------------------------------- 815 @Test 816 public void test_withMonth_normal() { 817 OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE); 818 OffsetDateTime test = base.withMonth(1); 819 assertEquals(test, OffsetDateTime.of(LocalDate.of(2008, 1, 30), LocalTime.of(11, 30, 59), OFFSET_PONE)); 820 } 821 822 //----------------------------------------------------------------------- 823 // withDayOfMonth() 824 //----------------------------------------------------------------------- 825 @Test 826 public void test_withDayOfMonth_normal() { 827 OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE); 828 OffsetDateTime test = base.withDayOfMonth(15); 829 assertEquals(test, OffsetDateTime.of(LocalDate.of(2008, 6, 15), LocalTime.of(11, 30, 59), OFFSET_PONE)); 830 } 831 832 //----------------------------------------------------------------------- 833 // withDayOfYear(int) 834 //----------------------------------------------------------------------- 835 @Test 836 public void test_withDayOfYear_normal() { 837 OffsetDateTime t = TEST_2008_6_30_11_30_59_000000500.withDayOfYear(33); 838 assertEquals(t, OffsetDateTime.of(LocalDate.of(2008, 2, 2), LocalTime.of(11, 30, 59, 500), OFFSET_PONE)); 839 } 840 841 @Test(expectedExceptions=DateTimeException.class) 842 public void test_withDayOfYear_illegal() { 843 TEST_2008_6_30_11_30_59_000000500.withDayOfYear(367); 844 } 845 846 @Test(expectedExceptions=DateTimeException.class) 847 public void test_withDayOfYear_invalid() { 848 OffsetDateTime.of(LocalDate.of(2007, 2, 2), LocalTime.of(11, 30), OFFSET_PONE).withDayOfYear(366); 849 } 850 851 //----------------------------------------------------------------------- 852 // withHour() 853 //----------------------------------------------------------------------- 854 @Test 855 public void test_withHour_normal() { 856 OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE); 857 OffsetDateTime test = base.withHour(15); 858 assertEquals(test, OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(15, 30, 59), OFFSET_PONE)); 859 } 860 861 //----------------------------------------------------------------------- 862 // withMinute() 863 //----------------------------------------------------------------------- 864 @Test 865 public void test_withMinute_normal() { 866 OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE); 867 OffsetDateTime test = base.withMinute(15); 868 assertEquals(test, OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 15, 59), OFFSET_PONE)); 869 } 870 871 //----------------------------------------------------------------------- 872 // withSecond() 873 //----------------------------------------------------------------------- 874 @Test 875 public void test_withSecond_normal() { 876 OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59), OFFSET_PONE); 877 OffsetDateTime test = base.withSecond(15); 878 assertEquals(test, OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 15), OFFSET_PONE)); 879 } 880 881 //----------------------------------------------------------------------- 882 // withNano() 883 //----------------------------------------------------------------------- 884 @Test 885 public void test_withNanoOfSecond_normal() { 886 OffsetDateTime base = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59, 1), OFFSET_PONE); 887 OffsetDateTime test = base.withNano(15); 888 assertEquals(test, OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(11, 30, 59, 15), OFFSET_PONE)); 889 } 890 891 //----------------------------------------------------------------------- 892 // truncatedTo(TemporalUnit) 893 //----------------------------------------------------------------------- 894 @Test 895 public void test_truncatedTo_normal() { 896 assertEquals(TEST_2008_6_30_11_30_59_000000500.truncatedTo(NANOS), TEST_2008_6_30_11_30_59_000000500); 897 assertEquals(TEST_2008_6_30_11_30_59_000000500.truncatedTo(SECONDS), TEST_2008_6_30_11_30_59_000000500.withNano(0)); 898 assertEquals(TEST_2008_6_30_11_30_59_000000500.truncatedTo(DAYS), TEST_2008_6_30_11_30_59_000000500.with(LocalTime.MIDNIGHT)); 899 } 900 901 @Test(expectedExceptions=NullPointerException.class) 902 public void test_truncatedTo_null() { 903 TEST_2008_6_30_11_30_59_000000500.truncatedTo(null); 904 } 905 906 //----------------------------------------------------------------------- 907 // plus(Period) 908 //----------------------------------------------------------------------- 909 @Test 910 public void test_plus_Period() { 911 MockSimplePeriod period = MockSimplePeriod.of(7, ChronoUnit.MONTHS); 912 OffsetDateTime t = TEST_2008_6_30_11_30_59_000000500.plus(period); 913 assertEquals(t, OffsetDateTime.of(2009, 1, 30, 11, 30, 59, 500, OFFSET_PONE)); 914 } 915 916 //----------------------------------------------------------------------- 917 // plus(Duration) 918 //----------------------------------------------------------------------- 919 @Test 920 public void test_plus_Duration() { 921 Duration dur = Duration.ofSeconds(62, 3); 922 OffsetDateTime t = TEST_2008_6_30_11_30_59_000000500.plus(dur); 923 assertEquals(t, OffsetDateTime.of(2008, 6, 30, 11, 32, 1, 503, OFFSET_PONE)); 924 } 925 926 @Test 927 public void test_plus_Duration_zero() { 928 OffsetDateTime t = TEST_2008_6_30_11_30_59_000000500.plus(Duration.ZERO); 929 assertEquals(t, TEST_2008_6_30_11_30_59_000000500); 930 } 931 932 @Test(expectedExceptions=NullPointerException.class) 933 public void test_plus_Duration_null() { 934 TEST_2008_6_30_11_30_59_000000500.plus((Duration) null); 935 } 936 937 //----------------------------------------------------------------------- 938 // plusYears() 939 //----------------------------------------------------------------------- 940 @Test 941 public void test_plusYears() { 942 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 943 OffsetDateTime test = base.plusYears(1); 944 assertEquals(test, OffsetDateTime.of(2009, 6, 30, 11, 30, 59, 0, OFFSET_PONE)); 945 } 946 947 //----------------------------------------------------------------------- 948 // plusMonths() 949 //----------------------------------------------------------------------- 950 @Test 951 public void test_plusMonths() { 952 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 953 OffsetDateTime test = base.plusMonths(1); 954 assertEquals(test, OffsetDateTime.of(2008, 7, 30, 11, 30, 59, 0, OFFSET_PONE)); 955 } 956 957 //----------------------------------------------------------------------- 958 // plusWeeks() 959 //----------------------------------------------------------------------- 960 @Test 961 public void test_plusWeeks() { 962 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 963 OffsetDateTime test = base.plusWeeks(1); 964 assertEquals(test, OffsetDateTime.of(2008, 7, 7, 11, 30, 59, 0, OFFSET_PONE)); 965 } 966 967 //----------------------------------------------------------------------- 968 // plusDays() 969 //----------------------------------------------------------------------- 970 @Test 971 public void test_plusDays() { 972 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 973 OffsetDateTime test = base.plusDays(1); 974 assertEquals(test, OffsetDateTime.of(2008, 7, 1, 11, 30, 59, 0, OFFSET_PONE)); 975 } 976 977 //----------------------------------------------------------------------- 978 // plusHours() 979 //----------------------------------------------------------------------- 980 @Test 981 public void test_plusHours() { 982 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 983 OffsetDateTime test = base.plusHours(13); 984 assertEquals(test, OffsetDateTime.of(2008, 7, 1, 0, 30, 59, 0, OFFSET_PONE)); 985 } 986 987 //----------------------------------------------------------------------- 988 // plusMinutes() 989 //----------------------------------------------------------------------- 990 @Test 991 public void test_plusMinutes() { 992 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 993 OffsetDateTime test = base.plusMinutes(30); 994 assertEquals(test, OffsetDateTime.of(2008, 6, 30, 12, 0, 59, 0, OFFSET_PONE)); 995 } 996 997 //----------------------------------------------------------------------- 998 // plusSeconds() 999 //----------------------------------------------------------------------- 1000 @Test 1001 public void test_plusSeconds() { 1002 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 1003 OffsetDateTime test = base.plusSeconds(1); 1004 assertEquals(test, OffsetDateTime.of(2008, 6, 30, 11, 31, 0, 0, OFFSET_PONE)); 1005 } 1006 1007 //----------------------------------------------------------------------- 1008 // plusNanos() 1009 //----------------------------------------------------------------------- 1010 @Test 1011 public void test_plusNanos() { 1012 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 1013 OffsetDateTime test = base.plusNanos(1); 1014 assertEquals(test, OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 1, OFFSET_PONE)); 1015 } 1016 1017 //----------------------------------------------------------------------- 1018 // minus(Period) 1019 //----------------------------------------------------------------------- 1020 @Test 1021 public void test_minus_Period() { 1022 MockSimplePeriod period = MockSimplePeriod.of(7, ChronoUnit.MONTHS); 1023 OffsetDateTime t = TEST_2008_6_30_11_30_59_000000500.minus(period); 1024 assertEquals(t, OffsetDateTime.of(2007, 11, 30, 11, 30, 59, 500, OFFSET_PONE)); 1025 } 1026 1027 //----------------------------------------------------------------------- 1028 // minus(Duration) 1029 //----------------------------------------------------------------------- 1030 @Test 1031 public void test_minus_Duration() { 1032 Duration dur = Duration.ofSeconds(62, 3); 1033 OffsetDateTime t = TEST_2008_6_30_11_30_59_000000500.minus(dur); 1034 assertEquals(t, OffsetDateTime.of(2008, 6, 30, 11, 29, 57, 497, OFFSET_PONE)); 1035 } 1036 1037 @Test 1038 public void test_minus_Duration_zero() { 1039 OffsetDateTime t = TEST_2008_6_30_11_30_59_000000500.minus(Duration.ZERO); 1040 assertEquals(t, TEST_2008_6_30_11_30_59_000000500); 1041 } 1042 1043 @Test(expectedExceptions=NullPointerException.class) 1044 public void test_minus_Duration_null() { 1045 TEST_2008_6_30_11_30_59_000000500.minus((Duration) null); 1046 } 1047 1048 //----------------------------------------------------------------------- 1049 // minusYears() 1050 //----------------------------------------------------------------------- 1051 @Test 1052 public void test_minusYears() { 1053 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 1054 OffsetDateTime test = base.minusYears(1); 1055 assertEquals(test, OffsetDateTime.of(2007, 6, 30, 11, 30, 59, 0, OFFSET_PONE)); 1056 } 1057 1058 //----------------------------------------------------------------------- 1059 // minusMonths() 1060 //----------------------------------------------------------------------- 1061 @Test 1062 public void test_minusMonths() { 1063 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 1064 OffsetDateTime test = base.minusMonths(1); 1065 assertEquals(test, OffsetDateTime.of(2008, 5, 30, 11, 30, 59, 0, OFFSET_PONE)); 1066 } 1067 1068 //----------------------------------------------------------------------- 1069 // minusWeeks() 1070 //----------------------------------------------------------------------- 1071 @Test 1072 public void test_minusWeeks() { 1073 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 1074 OffsetDateTime test = base.minusWeeks(1); 1075 assertEquals(test, OffsetDateTime.of(2008, 6, 23, 11, 30, 59, 0, OFFSET_PONE)); 1076 } 1077 1078 //----------------------------------------------------------------------- 1079 // minusDays() 1080 //----------------------------------------------------------------------- 1081 @Test 1082 public void test_minusDays() { 1083 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 1084 OffsetDateTime test = base.minusDays(1); 1085 assertEquals(test, OffsetDateTime.of(2008, 6, 29, 11, 30, 59, 0, OFFSET_PONE)); 1086 } 1087 1088 //----------------------------------------------------------------------- 1089 // minusHours() 1090 //----------------------------------------------------------------------- 1091 @Test 1092 public void test_minusHours() { 1093 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 1094 OffsetDateTime test = base.minusHours(13); 1095 assertEquals(test, OffsetDateTime.of(2008, 6, 29, 22, 30, 59, 0, OFFSET_PONE)); 1096 } 1097 1098 //----------------------------------------------------------------------- 1099 // minusMinutes() 1100 //----------------------------------------------------------------------- 1101 @Test 1102 public void test_minusMinutes() { 1103 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 1104 OffsetDateTime test = base.minusMinutes(30); 1105 assertEquals(test, OffsetDateTime.of(2008, 6, 30, 11, 0, 59, 0, OFFSET_PONE)); 1106 } 1107 1108 //----------------------------------------------------------------------- 1109 // minusSeconds() 1110 //----------------------------------------------------------------------- 1111 @Test 1112 public void test_minusSeconds() { 1113 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 1114 OffsetDateTime test = base.minusSeconds(1); 1115 assertEquals(test, OffsetDateTime.of(2008, 6, 30, 11, 30, 58, 0, OFFSET_PONE)); 1116 } 1117 1118 //----------------------------------------------------------------------- 1119 // minusNanos() 1120 //----------------------------------------------------------------------- 1121 @Test 1122 public void test_minusNanos() { 1123 OffsetDateTime base = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 1124 OffsetDateTime test = base.minusNanos(1); 1125 assertEquals(test, OffsetDateTime.of(2008, 6, 30, 11, 30, 58, 999999999, OFFSET_PONE)); 1126 } 1127 1128 //----------------------------------------------------------------------- 1129 // until(Temporal, TemporalUnit) 1130 //----------------------------------------------------------------------- 1131 @DataProvider(name="periodUntilUnit") 1132 Object[][] data_untilUnit() { 1133 return new Object[][] { 1134 {OffsetDateTime.of(2010, 6, 30, 1, 1, 1, 0, OFFSET_PONE), OffsetDateTime.of(2010, 6, 30, 13, 1, 1, 0, OFFSET_PONE), HALF_DAYS, 1}, 1135 {OffsetDateTime.of(2010, 6, 30, 1, 1, 1, 0, OFFSET_PONE), OffsetDateTime.of(2010, 6, 30, 2, 1, 1, 0, OFFSET_PONE), HOURS, 1}, 1136 {OffsetDateTime.of(2010, 6, 30, 1, 1, 1, 0, OFFSET_PONE), OffsetDateTime.of(2010, 6, 30, 2, 1, 1, 0, OFFSET_PONE), MINUTES, 60}, 1137 {OffsetDateTime.of(2010, 6, 30, 1, 1, 1, 0, OFFSET_PONE), OffsetDateTime.of(2010, 6, 30, 2, 1, 1, 0, OFFSET_PONE), SECONDS, 3600}, 1138 {OffsetDateTime.of(2010, 6, 30, 1, 1, 1, 0, OFFSET_PONE), OffsetDateTime.of(2010, 6, 30, 2, 1, 1, 0, OFFSET_PONE), MILLIS, 3600*1000}, 1139 {OffsetDateTime.of(2010, 6, 30, 1, 1, 1, 0, OFFSET_PONE), OffsetDateTime.of(2010, 6, 30, 2, 1, 1, 0, OFFSET_PONE), MICROS, 3600*1000*1000L}, 1140 {OffsetDateTime.of(2010, 6, 30, 1, 1, 1, 0, OFFSET_PONE), OffsetDateTime.of(2010, 6, 30, 2, 1, 1, 0, OFFSET_PONE), NANOS, 3600*1000*1000L*1000}, 1141 1142 {OffsetDateTime.of(2010, 6, 30, 1, 1, 1, 0, OFFSET_PONE), OffsetDateTime.of(2010, 6, 30, 14, 1, 1, 0, OFFSET_PTWO), HALF_DAYS, 1}, 1143 {OffsetDateTime.of(2010, 6, 30, 1, 1, 1, 0, OFFSET_PONE), OffsetDateTime.of(2010, 6, 30, 3, 1, 1, 0, OFFSET_PTWO), HOURS, 1}, 1144 {OffsetDateTime.of(2010, 6, 30, 1, 1, 1, 0, OFFSET_PONE), OffsetDateTime.of(2010, 6, 30, 3, 1, 1, 0, OFFSET_PTWO), MINUTES, 60}, 1145 {OffsetDateTime.of(2010, 6, 30, 1, 1, 1, 0, OFFSET_PONE), OffsetDateTime.of(2010, 6, 30, 3, 1, 1, 0, OFFSET_PTWO), SECONDS, 3600}, 1146 {OffsetDateTime.of(2010, 6, 30, 1, 1, 1, 0, OFFSET_PONE), OffsetDateTime.of(2010, 6, 30, 3, 1, 1, 0, OFFSET_PTWO), MILLIS, 3600*1000}, 1147 {OffsetDateTime.of(2010, 6, 30, 1, 1, 1, 0, OFFSET_PONE), OffsetDateTime.of(2010, 6, 30, 3, 1, 1, 0, OFFSET_PTWO), MICROS, 3600*1000*1000L}, 1148 {OffsetDateTime.of(2010, 6, 30, 1, 1, 1, 0, OFFSET_PONE), OffsetDateTime.of(2010, 6, 30, 3, 1, 1, 0, OFFSET_PTWO), NANOS, 3600*1000*1000L*1000}, 1149 1150 {OffsetDateTime.of(2010, 6, 30, 1, 1, 1, 0, OFFSET_PONE), OffsetDateTime.of(2010, 7, 1, 1, 1, 0, 999999999, OFFSET_PONE), DAYS, 0}, 1151 {OffsetDateTime.of(2010, 6, 30, 1, 1, 1, 0, OFFSET_PONE), OffsetDateTime.of(2010, 7, 1, 1, 1, 1, 0, OFFSET_PONE), DAYS, 1}, 1152 {OffsetDateTime.of(2010, 6, 30, 1, 1, 1, 0, OFFSET_PONE), OffsetDateTime.of(2010, 8, 29, 1, 1, 1, 0, OFFSET_PONE), MONTHS, 1}, 1153 {OffsetDateTime.of(2010, 6, 30, 1, 1, 1, 0, OFFSET_PONE), OffsetDateTime.of(2010, 8, 30, 1, 1, 1, 0, OFFSET_PONE), MONTHS, 2}, 1154 {OffsetDateTime.of(2010, 6, 30, 1, 1, 1, 0, OFFSET_PONE), OffsetDateTime.of(2010, 8, 31, 1, 1, 1, 0, OFFSET_PONE), MONTHS, 2}, 1155 }; 1156 } 1157 1158 @Test(dataProvider="periodUntilUnit") 1159 public void test_until_TemporalUnit(OffsetDateTime odt1, OffsetDateTime odt2, TemporalUnit unit, long expected) { 1160 long amount = odt1.until(odt2, unit); 1161 assertEquals(amount, expected); 1162 } 1163 1164 @Test(dataProvider="periodUntilUnit") 1165 public void test_until_TemporalUnit_negated(OffsetDateTime odt1, OffsetDateTime odt2, TemporalUnit unit, long expected) { 1166 long amount = odt2.until(odt1, unit); 1167 assertEquals(amount, -expected); 1168 } 1169 1170 @Test(dataProvider="periodUntilUnit") 1171 public void test_until_TemporalUnit_between(OffsetDateTime odt1, OffsetDateTime odt2, TemporalUnit unit, long expected) { 1172 long amount = unit.between(odt1, odt2); 1173 assertEquals(amount, expected); 1174 } 1175 1176 @Test 1177 public void test_until_convertedType() { 1178 OffsetDateTime odt = OffsetDateTime.of(2010, 6, 30, 1, 1, 1, 0, OFFSET_PONE); 1179 ZonedDateTime zdt = odt.plusSeconds(3).toZonedDateTime(); 1180 assertEquals(odt.until(zdt, SECONDS), 3); 1181 } 1182 1183 @Test(expectedExceptions=DateTimeException.class) 1184 public void test_until_invalidType() { 1185 OffsetDateTime odt = OffsetDateTime.of(2010, 6, 30, 1, 1, 1, 0, OFFSET_PONE); 1186 odt.until(Instant.ofEpochSecond(12), SECONDS); 1187 } 1188 1189 @Test(expectedExceptions=DateTimeException.class) 1190 public void test_until_invalidTemporalUnit() { 1191 OffsetDateTime odt1 = OffsetDateTime.of(2010, 6, 30, 1, 1, 1, 0, OFFSET_PONE); 1192 OffsetDateTime odt2 = OffsetDateTime.of(2010, 6, 30, 2, 1, 1, 0, OFFSET_PONE); 1193 odt1.until(odt2, FOREVER); 1194 } 1195 1196 //----------------------------------------------------------------------- 1197 // format(DateTimeFormatter) 1198 //----------------------------------------------------------------------- 1199 @Test 1200 public void test_format_formatter() { 1201 DateTimeFormatter f = DateTimeFormatter.ofPattern("y M d H m s"); 1202 String t = OffsetDateTime.of(2010, 12, 3, 11, 30, 0, 0, OFFSET_PONE).format(f); 1203 assertEquals(t, "2010 12 3 11 30 0"); 1204 } 1205 1206 @Test(expectedExceptions=NullPointerException.class) 1207 public void test_format_formatter_null() { 1208 OffsetDateTime.of(2010, 12, 3, 11, 30, 0, 0, OFFSET_PONE).format(null); 1209 } 1210 1211 //----------------------------------------------------------------------- 1212 // atZoneSameInstant() 1213 //----------------------------------------------------------------------- 1214 @Test 1215 public void test_atZone() { 1216 OffsetDateTime t = OffsetDateTime.of(2008, 6, 30, 11, 30, 0, 0, OFFSET_MTWO); 1217 assertEquals(t.atZoneSameInstant(ZONE_PARIS), 1218 ZonedDateTime.of(2008, 6, 30, 15, 30, 0, 0, ZONE_PARIS)); 1219 } 1220 1221 @Test(expectedExceptions=NullPointerException.class) 1222 public void test_atZone_nullTimeZone() { 1223 OffsetDateTime t = OffsetDateTime.of(2008, 6, 30, 11, 30, 0, 0, OFFSET_PTWO); 1224 t.atZoneSameInstant((ZoneId) null); 1225 } 1226 1227 //----------------------------------------------------------------------- 1228 // atZoneSimilarLocal() 1229 //----------------------------------------------------------------------- 1230 @Test 1231 public void test_atZoneSimilarLocal() { 1232 OffsetDateTime t = OffsetDateTime.of(2008, 6, 30, 11, 30, 0, 0, OFFSET_MTWO); 1233 assertEquals(t.atZoneSimilarLocal(ZONE_PARIS), 1234 ZonedDateTime.of(2008, 6, 30, 11, 30, 0, 0, ZONE_PARIS)); 1235 } 1236 1237 @Test 1238 public void test_atZoneSimilarLocal_dstGap() { 1239 OffsetDateTime t = OffsetDateTime.of(2007, 4, 1, 0, 0, 0, 0, OFFSET_MTWO); 1240 assertEquals(t.atZoneSimilarLocal(ZONE_GAZA), 1241 ZonedDateTime.of(2007, 4, 1, 1, 0, 0, 0, ZONE_GAZA)); 1242 } 1243 1244 @Test 1245 public void test_atZone_dstOverlapSummer() { 1246 OffsetDateTime t = OffsetDateTime.of(2007, 10, 28, 2, 30, 0, 0, OFFSET_PTWO); 1247 assertEquals(t.atZoneSimilarLocal(ZONE_PARIS).toLocalDateTime(), t.toLocalDateTime()); 1248 assertEquals(t.atZoneSimilarLocal(ZONE_PARIS).getOffset(), OFFSET_PTWO); 1249 assertEquals(t.atZoneSimilarLocal(ZONE_PARIS).getZone(), ZONE_PARIS); 1250 } 1251 1252 @Test 1253 public void test_atZone_dstOverlapWinter() { 1254 OffsetDateTime t = OffsetDateTime.of(2007, 10, 28, 2, 30, 0, 0, OFFSET_PONE); 1255 assertEquals(t.atZoneSimilarLocal(ZONE_PARIS).toLocalDateTime(), t.toLocalDateTime()); 1256 assertEquals(t.atZoneSimilarLocal(ZONE_PARIS).getOffset(), OFFSET_PONE); 1257 assertEquals(t.atZoneSimilarLocal(ZONE_PARIS).getZone(), ZONE_PARIS); 1258 } 1259 1260 @Test(expectedExceptions=NullPointerException.class) 1261 public void test_atZoneSimilarLocal_nullTimeZone() { 1262 OffsetDateTime t = OffsetDateTime.of(2008, 6, 30, 11, 30, 0, 0, OFFSET_PTWO); 1263 t.atZoneSimilarLocal((ZoneId) null); 1264 } 1265 1266 //----------------------------------------------------------------------- 1267 // toEpochSecond() 1268 //----------------------------------------------------------------------- 1269 @Test 1270 public void test_toEpochSecond_afterEpoch() { 1271 for (int i = 0; i < 100000; i++) { 1272 OffsetDateTime a = OffsetDateTime.of(1970, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC).plusSeconds(i); 1273 assertEquals(a.toEpochSecond(), i); 1274 } 1275 } 1276 1277 @Test 1278 public void test_toEpochSecond_beforeEpoch() { 1279 for (int i = 0; i < 100000; i++) { 1280 OffsetDateTime a = OffsetDateTime.of(1970, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC).minusSeconds(i); 1281 assertEquals(a.toEpochSecond(), -i); 1282 } 1283 } 1284 1285 //----------------------------------------------------------------------- 1286 // compareTo() 1287 //----------------------------------------------------------------------- 1288 @Test 1289 public void test_compareTo_timeMins() { 1290 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 11, 29, 3, 0, OFFSET_PONE); 1291 OffsetDateTime b = OffsetDateTime.of(2008, 6, 30, 11, 30, 2, 0, OFFSET_PONE); // a is before b due to time 1292 assertEquals(a.compareTo(b) < 0, true); 1293 assertEquals(b.compareTo(a) > 0, true); 1294 assertEquals(a.compareTo(a) == 0, true); 1295 assertEquals(b.compareTo(b) == 0, true); 1296 assertEquals(a.toInstant().compareTo(b.toInstant()) < 0, true); 1297 assertEquals(OffsetDateTime.timeLineOrder().compare(a, b) < 0, true); 1298 } 1299 1300 @Test 1301 public void test_compareTo_timeSecs() { 1302 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 11, 29, 2, 0, OFFSET_PONE); 1303 OffsetDateTime b = OffsetDateTime.of(2008, 6, 30, 11, 29, 3, 0, OFFSET_PONE); // a is before b due to time 1304 assertEquals(a.compareTo(b) < 0, true); 1305 assertEquals(b.compareTo(a) > 0, true); 1306 assertEquals(a.compareTo(a) == 0, true); 1307 assertEquals(b.compareTo(b) == 0, true); 1308 assertEquals(a.toInstant().compareTo(b.toInstant()) < 0, true); 1309 assertEquals(OffsetDateTime.timeLineOrder().compare(a, b) < 0, true); 1310 } 1311 1312 @Test 1313 public void test_compareTo_timeNanos() { 1314 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 11, 29, 40, 4, OFFSET_PONE); 1315 OffsetDateTime b = OffsetDateTime.of(2008, 6, 30, 11, 29, 40, 5, OFFSET_PONE); // a is before b due to time 1316 assertEquals(a.compareTo(b) < 0, true); 1317 assertEquals(b.compareTo(a) > 0, true); 1318 assertEquals(a.compareTo(a) == 0, true); 1319 assertEquals(b.compareTo(b) == 0, true); 1320 assertEquals(a.toInstant().compareTo(b.toInstant()) < 0, true); 1321 assertEquals(OffsetDateTime.timeLineOrder().compare(a, b) < 0, true); 1322 } 1323 1324 @Test 1325 public void test_compareTo_offset() { 1326 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 11, 30, 0, 0, OFFSET_PTWO); 1327 OffsetDateTime b = OffsetDateTime.of(2008, 6, 30, 11, 30, 0, 0, OFFSET_PONE); // a is before b due to offset 1328 assertEquals(a.compareTo(b) < 0, true); 1329 assertEquals(b.compareTo(a) > 0, true); 1330 assertEquals(a.compareTo(a) == 0, true); 1331 assertEquals(b.compareTo(b) == 0, true); 1332 assertEquals(a.toInstant().compareTo(b.toInstant()) < 0, true); 1333 assertEquals(OffsetDateTime.timeLineOrder().compare(a, b) < 0, true); 1334 } 1335 1336 @Test 1337 public void test_compareTo_offsetNanos() { 1338 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 11, 30, 40, 6, OFFSET_PTWO); 1339 OffsetDateTime b = OffsetDateTime.of(2008, 6, 30, 11, 30, 40, 5, OFFSET_PONE); // a is before b due to offset 1340 assertEquals(a.compareTo(b) < 0, true); 1341 assertEquals(b.compareTo(a) > 0, true); 1342 assertEquals(a.compareTo(a) == 0, true); 1343 assertEquals(b.compareTo(b) == 0, true); 1344 assertEquals(a.toInstant().compareTo(b.toInstant()) < 0, true); 1345 assertEquals(OffsetDateTime.timeLineOrder().compare(a, b) < 0, true); 1346 } 1347 1348 @Test 1349 public void test_compareTo_both() { 1350 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 11, 50, 0, 0, OFFSET_PTWO); 1351 OffsetDateTime b = OffsetDateTime.of(2008, 6, 30, 11, 20, 0, 0, OFFSET_PONE); // a is before b on instant scale 1352 assertEquals(a.compareTo(b) < 0, true); 1353 assertEquals(b.compareTo(a) > 0, true); 1354 assertEquals(a.compareTo(a) == 0, true); 1355 assertEquals(b.compareTo(b) == 0, true); 1356 assertEquals(a.toInstant().compareTo(b.toInstant()) < 0, true); 1357 assertEquals(OffsetDateTime.timeLineOrder().compare(a, b) < 0, true); 1358 } 1359 1360 @Test 1361 public void test_compareTo_bothNanos() { 1362 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 11, 20, 40, 4, OFFSET_PTWO); 1363 OffsetDateTime b = OffsetDateTime.of(2008, 6, 30, 10, 20, 40, 5, OFFSET_PONE); // a is before b on instant scale 1364 assertEquals(a.compareTo(b) < 0, true); 1365 assertEquals(b.compareTo(a) > 0, true); 1366 assertEquals(a.compareTo(a) == 0, true); 1367 assertEquals(b.compareTo(b) == 0, true); 1368 assertEquals(a.toInstant().compareTo(b.toInstant()) < 0, true); 1369 assertEquals(OffsetDateTime.timeLineOrder().compare(a, b) < 0, true); 1370 } 1371 1372 @Test 1373 public void test_compareTo_bothInstantComparator() { 1374 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 11, 20, 40, 4, OFFSET_PTWO); 1375 OffsetDateTime b = OffsetDateTime.of(2008, 6, 30, 10, 20, 40, 5, OFFSET_PONE); 1376 assertEquals(a.compareTo(b), OffsetDateTime.timeLineOrder().compare(a,b), "for nano != nano, compareTo and timeLineOrder() should be the same"); 1377 } 1378 1379 @Test 1380 public void test_compareTo_hourDifference() { 1381 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 10, 0, 0, 0, OFFSET_PONE); 1382 OffsetDateTime b = OffsetDateTime.of(2008, 6, 30, 11, 0, 0, 0, OFFSET_PTWO); // a is before b despite being same time-line time 1383 assertEquals(a.compareTo(b) < 0, true); 1384 assertEquals(b.compareTo(a) > 0, true); 1385 assertEquals(a.compareTo(a) == 0, true); 1386 assertEquals(b.compareTo(b) == 0, true); 1387 assertEquals(a.toInstant().compareTo(b.toInstant()) == 0, true); 1388 } 1389 1390 @Test 1391 public void test_compareTo_max() { 1392 OffsetDateTime a = OffsetDateTime.of(Year.MAX_VALUE, 12, 31, 23, 59, 0, 0, OFFSET_MONE); 1393 OffsetDateTime b = OffsetDateTime.of(Year.MAX_VALUE, 12, 31, 23, 59, 0, 0, OFFSET_MTWO); // a is before b due to offset 1394 assertEquals(a.compareTo(b) < 0, true); 1395 assertEquals(b.compareTo(a) > 0, true); 1396 assertEquals(a.compareTo(a) == 0, true); 1397 assertEquals(b.compareTo(b) == 0, true); 1398 } 1399 1400 @Test 1401 public void test_compareTo_min() { 1402 OffsetDateTime a = OffsetDateTime.of(Year.MIN_VALUE, 1, 1, 0, 0, 0, 0, OFFSET_PTWO); 1403 OffsetDateTime b = OffsetDateTime.of(Year.MIN_VALUE, 1, 1, 0, 0, 0, 0, OFFSET_PONE); // a is before b due to offset 1404 assertEquals(a.compareTo(b) < 0, true); 1405 assertEquals(b.compareTo(a) > 0, true); 1406 assertEquals(a.compareTo(a) == 0, true); 1407 assertEquals(b.compareTo(b) == 0, true); 1408 } 1409 1410 @Test(expectedExceptions=NullPointerException.class) 1411 public void test_compareTo_null() { 1412 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 1413 a.compareTo(null); 1414 } 1415 1416 @Test(expectedExceptions=ClassCastException.class) 1417 @SuppressWarnings({"unchecked", "rawtypes"}) 1418 public void compareToNonOffsetDateTime() { 1419 Comparable c = TEST_2008_6_30_11_30_59_000000500; 1420 c.compareTo(new Object()); 1421 } 1422 1423 //----------------------------------------------------------------------- 1424 // isAfter() / isBefore() / isEqual() 1425 //----------------------------------------------------------------------- 1426 @Test 1427 public void test_isBeforeIsAfterIsEqual1() { 1428 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 11, 30, 58, 3, OFFSET_PONE); 1429 OffsetDateTime b = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 2, OFFSET_PONE); // a is before b due to time 1430 assertEquals(a.isBefore(b), true); 1431 assertEquals(a.isEqual(b), false); 1432 assertEquals(a.isAfter(b), false); 1433 1434 assertEquals(b.isBefore(a), false); 1435 assertEquals(b.isEqual(a), false); 1436 assertEquals(b.isAfter(a), true); 1437 1438 assertEquals(a.isBefore(a), false); 1439 assertEquals(b.isBefore(b), false); 1440 1441 assertEquals(a.isEqual(a), true); 1442 assertEquals(b.isEqual(b), true); 1443 1444 assertEquals(a.isAfter(a), false); 1445 assertEquals(b.isAfter(b), false); 1446 } 1447 1448 @Test 1449 public void test_isBeforeIsAfterIsEqual2() { 1450 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 2, OFFSET_PONE); 1451 OffsetDateTime b = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 3, OFFSET_PONE); // a is before b due to time 1452 assertEquals(a.isBefore(b), true); 1453 assertEquals(a.isEqual(b), false); 1454 assertEquals(a.isAfter(b), false); 1455 1456 assertEquals(b.isBefore(a), false); 1457 assertEquals(b.isEqual(a), false); 1458 assertEquals(b.isAfter(a), true); 1459 1460 assertEquals(a.isBefore(a), false); 1461 assertEquals(b.isBefore(b), false); 1462 1463 assertEquals(a.isEqual(a), true); 1464 assertEquals(b.isEqual(b), true); 1465 1466 assertEquals(a.isAfter(a), false); 1467 assertEquals(b.isAfter(b), false); 1468 } 1469 1470 @Test 1471 public void test_isBeforeIsAfterIsEqual_instantComparison() { 1472 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 10, 0, 0, 0, OFFSET_PONE); 1473 OffsetDateTime b = OffsetDateTime.of(2008, 6, 30, 11, 0, 0, 0, OFFSET_PTWO); // a is same instant as b 1474 assertEquals(a.isBefore(b), false); 1475 assertEquals(a.isEqual(b), true); 1476 assertEquals(a.isAfter(b), false); 1477 1478 assertEquals(b.isBefore(a), false); 1479 assertEquals(b.isEqual(a), true); 1480 assertEquals(b.isAfter(a), false); 1481 1482 assertEquals(a.isBefore(a), false); 1483 assertEquals(b.isBefore(b), false); 1484 1485 assertEquals(a.isEqual(a), true); 1486 assertEquals(b.isEqual(b), true); 1487 1488 assertEquals(a.isAfter(a), false); 1489 assertEquals(b.isAfter(b), false); 1490 } 1491 1492 @Test(expectedExceptions=NullPointerException.class) 1493 public void test_isBefore_null() { 1494 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 1495 a.isBefore(null); 1496 } 1497 1498 @Test(expectedExceptions=NullPointerException.class) 1499 public void test_isEqual_null() { 1500 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 1501 a.isEqual(null); 1502 } 1503 1504 @Test(expectedExceptions=NullPointerException.class) 1505 public void test_isAfter_null() { 1506 OffsetDateTime a = OffsetDateTime.of(2008, 6, 30, 11, 30, 59, 0, OFFSET_PONE); 1507 a.isAfter(null); 1508 } 1509 1510 //----------------------------------------------------------------------- 1511 // equals() / hashCode() 1512 //----------------------------------------------------------------------- 1513 @Test(dataProvider="sampleTimes") 1514 public void test_equals_true(int y, int o, int d, int h, int m, int s, int n, ZoneOffset ignored) { 1515 OffsetDateTime a = OffsetDateTime.of(y, o, d, h, m, s, n, OFFSET_PONE); 1516 OffsetDateTime b = OffsetDateTime.of(y, o, d, h, m, s, n, OFFSET_PONE); 1517 assertEquals(a.equals(b), true); 1518 assertEquals(a.hashCode() == b.hashCode(), true); 1519 } 1520 @Test(dataProvider="sampleTimes") 1521 public void test_equals_false_year_differs(int y, int o, int d, int h, int m, int s, int n, ZoneOffset ignored) { 1522 OffsetDateTime a = OffsetDateTime.of(y, o, d, h, m, s, n, OFFSET_PONE); 1523 OffsetDateTime b = OffsetDateTime.of(y + 1, o, d, h, m, s, n, OFFSET_PONE); 1524 assertEquals(a.equals(b), false); 1525 } 1526 @Test(dataProvider="sampleTimes") 1527 public void test_equals_false_hour_differs(int y, int o, int d, int h, int m, int s, int n, ZoneOffset ignored) { 1528 h = (h == 23 ? 22 : h); 1529 OffsetDateTime a = OffsetDateTime.of(y, o, d, h, m, s, n, OFFSET_PONE); 1530 OffsetDateTime b = OffsetDateTime.of(y, o, d, h + 1, m, s, n, OFFSET_PONE); 1531 assertEquals(a.equals(b), false); 1532 } 1533 @Test(dataProvider="sampleTimes") 1534 public void test_equals_false_minute_differs(int y, int o, int d, int h, int m, int s, int n, ZoneOffset ignored) { 1535 m = (m == 59 ? 58 : m); 1536 OffsetDateTime a = OffsetDateTime.of(y, o, d, h, m, s, n, OFFSET_PONE); 1537 OffsetDateTime b = OffsetDateTime.of(y, o, d, h, m + 1, s, n, OFFSET_PONE); 1538 assertEquals(a.equals(b), false); 1539 } 1540 @Test(dataProvider="sampleTimes") 1541 public void test_equals_false_second_differs(int y, int o, int d, int h, int m, int s, int n, ZoneOffset ignored) { 1542 s = (s == 59 ? 58 : s); 1543 OffsetDateTime a = OffsetDateTime.of(y, o, d, h, m, s, n, OFFSET_PONE); 1544 OffsetDateTime b = OffsetDateTime.of(y, o, d, h, m, s + 1, n, OFFSET_PONE); 1545 assertEquals(a.equals(b), false); 1546 } 1547 @Test(dataProvider="sampleTimes") 1548 public void test_equals_false_nano_differs(int y, int o, int d, int h, int m, int s, int n, ZoneOffset ignored) { 1549 n = (n == 999999999 ? 999999998 : n); 1550 OffsetDateTime a = OffsetDateTime.of(y, o, d, h, m, s, n, OFFSET_PONE); 1551 OffsetDateTime b = OffsetDateTime.of(y, o, d, h, m, s, n + 1, OFFSET_PONE); 1552 assertEquals(a.equals(b), false); 1553 } 1554 @Test(dataProvider="sampleTimes") 1555 public void test_equals_false_offset_differs(int y, int o, int d, int h, int m, int s, int n, ZoneOffset ignored) { 1556 OffsetDateTime a = OffsetDateTime.of(y, o, d, h, m, s, n, OFFSET_PONE); 1557 OffsetDateTime b = OffsetDateTime.of(y, o, d, h, m, s, n, OFFSET_PTWO); 1558 assertEquals(a.equals(b), false); 1559 } 1560 1561 @Test 1562 public void test_equals_itself_true() { 1563 assertEquals(TEST_2008_6_30_11_30_59_000000500.equals(TEST_2008_6_30_11_30_59_000000500), true); 1564 } 1565 1566 @Test 1567 public void test_equals_string_false() { 1568 assertEquals(TEST_2008_6_30_11_30_59_000000500.equals("2007-07-15"), false); 1569 } 1570 1571 @Test 1572 public void test_equals_null_false() { 1573 assertEquals(TEST_2008_6_30_11_30_59_000000500.equals(null), false); 1574 } 1575 1576 //----------------------------------------------------------------------- 1577 // toString() 1578 //----------------------------------------------------------------------- 1579 @DataProvider(name="sampleToString") 1580 Object[][] provider_sampleToString() { 1581 return new Object[][] { 1582 {2008, 6, 30, 11, 30, 59, 0, "Z", "2008-06-30T11:30:59Z"}, 1583 {2008, 6, 30, 11, 30, 59, 0, "+01:00", "2008-06-30T11:30:59+01:00"}, 1584 {2008, 6, 30, 11, 30, 59, 999000000, "Z", "2008-06-30T11:30:59.999Z"}, 1585 {2008, 6, 30, 11, 30, 59, 999000000, "+01:00", "2008-06-30T11:30:59.999+01:00"}, 1586 {2008, 6, 30, 11, 30, 59, 999000, "Z", "2008-06-30T11:30:59.000999Z"}, 1587 {2008, 6, 30, 11, 30, 59, 999000, "+01:00", "2008-06-30T11:30:59.000999+01:00"}, 1588 {2008, 6, 30, 11, 30, 59, 999, "Z", "2008-06-30T11:30:59.000000999Z"}, 1589 {2008, 6, 30, 11, 30, 59, 999, "+01:00", "2008-06-30T11:30:59.000000999+01:00"}, 1590 }; 1591 } 1592 1593 @Test(dataProvider="sampleToString") 1594 public void test_toString(int y, int o, int d, int h, int m, int s, int n, String offsetId, String expected) { 1595 OffsetDateTime t = OffsetDateTime.of(y, o, d, h, m, s, n, ZoneOffset.of(offsetId)); 1596 String str = t.toString(); 1597 assertEquals(str, expected); 1598 } 1599 1600 } 1601