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