1 /* 2 * Copyright (c) 2012, 2013, 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 java.time.*; 63 import test.java.time.MockSimplePeriod; 64 65 import static java.time.Month.JANUARY; 66 import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH; 67 import static java.time.temporal.ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR; 68 import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_MONTH; 69 import static java.time.temporal.ChronoField.ALIGNED_WEEK_OF_YEAR; 70 import static java.time.temporal.ChronoField.AMPM_OF_DAY; 71 import static java.time.temporal.ChronoField.CLOCK_HOUR_OF_AMPM; 72 import static java.time.temporal.ChronoField.CLOCK_HOUR_OF_DAY; 73 import static java.time.temporal.ChronoField.DAY_OF_MONTH; 74 import static java.time.temporal.ChronoField.DAY_OF_WEEK; 75 import static java.time.temporal.ChronoField.DAY_OF_YEAR; 76 import static java.time.temporal.ChronoField.EPOCH_DAY; 77 import static java.time.temporal.ChronoField.EPOCH_MONTH; 78 import static java.time.temporal.ChronoField.ERA; 79 import static java.time.temporal.ChronoField.HOUR_OF_AMPM; 80 import static java.time.temporal.ChronoField.HOUR_OF_DAY; 81 import static java.time.temporal.ChronoField.INSTANT_SECONDS; 82 import static java.time.temporal.ChronoField.MICRO_OF_DAY; 83 import static java.time.temporal.ChronoField.MICRO_OF_SECOND; 84 import static java.time.temporal.ChronoField.MILLI_OF_DAY; 85 import static java.time.temporal.ChronoField.MILLI_OF_SECOND; 86 import static java.time.temporal.ChronoField.MINUTE_OF_DAY; 87 import static java.time.temporal.ChronoField.MINUTE_OF_HOUR; 88 import static java.time.temporal.ChronoField.MONTH_OF_YEAR; 89 import static java.time.temporal.ChronoField.NANO_OF_DAY; 90 import static java.time.temporal.ChronoField.NANO_OF_SECOND; 91 import static java.time.temporal.ChronoField.OFFSET_SECONDS; 92 import static java.time.temporal.ChronoField.SECOND_OF_DAY; 93 import static java.time.temporal.ChronoField.SECOND_OF_MINUTE; 94 import static java.time.temporal.ChronoField.YEAR; 95 import static java.time.temporal.ChronoField.YEAR_OF_ERA; 96 import static java.time.temporal.ChronoUnit.DAYS; 97 import static java.time.temporal.ChronoUnit.HOURS; 98 import static java.time.temporal.ChronoUnit.MINUTES; 99 import static java.time.temporal.ChronoUnit.NANOS; 100 import static java.time.temporal.ChronoUnit.SECONDS; 101 import static org.testng.Assert.assertEquals; 102 import static org.testng.Assert.assertTrue; 103 104 import java.io.ByteArrayOutputStream; 105 import java.io.DataOutputStream; 106 import java.io.IOException; 107 import java.util.ArrayList; 108 import java.util.Arrays; 109 import java.util.List; 110 111 import java.time.temporal.ChronoField; 112 import java.time.temporal.ChronoUnit; 113 import java.time.temporal.Queries; 114 import java.time.temporal.TemporalSubtractor; 115 import java.time.temporal.TemporalAdder; 116 import java.time.temporal.TemporalAdjuster; 117 import java.time.temporal.TemporalAccessor; 118 import java.time.temporal.TemporalQuery; 119 import java.time.temporal.TemporalField; 120 import java.time.temporal.ISOChrono; 121 import java.time.temporal.JulianFields; 122 import test.java.time.temporal.MockFieldNoValue; 123 124 import java.time.format.DateTimeFormatter; 125 import java.time.format.DateTimeFormatters; 126 import java.time.format.DateTimeParseException; 127 import java.time.temporal.OffsetDateTime; 128 import java.time.temporal.Year; 129 130 import org.testng.annotations.BeforeMethod; 131 import org.testng.annotations.DataProvider; 132 import org.testng.annotations.Test; 133 134 /** 135 * Test ZonedDateTime. 136 */ 137 @Test 138 public class TCKZonedDateTime extends AbstractDateTimeTest { 139 140 private static final ZoneOffset OFFSET_0100 = ZoneOffset.ofHours(1); 141 private static final ZoneOffset OFFSET_0200 = ZoneOffset.ofHours(2); 142 private static final ZoneOffset OFFSET_0130 = ZoneOffset.of("+01:30"); 143 private static final ZoneOffset OFFSET_MAX = ZoneOffset.MAX; 144 private static final ZoneOffset OFFSET_MIN = ZoneOffset.MIN; 145 146 private static final ZoneId ZONE_0100 = OFFSET_0100; 147 private static final ZoneId ZONE_0200 = OFFSET_0200; 148 private static final ZoneId ZONE_M0100 = ZoneOffset.ofHours(-1); 149 private static final ZoneId ZONE_LONDON = ZoneId.of("Europe/London"); 150 private static final ZoneId ZONE_PARIS = ZoneId.of("Europe/Paris"); 151 private LocalDateTime TEST_PARIS_GAP_2008_03_30_02_30; 152 private LocalDateTime TEST_PARIS_OVERLAP_2008_10_26_02_30; 153 private LocalDateTime TEST_LOCAL_2008_06_30_11_30_59_500; 154 private ZonedDateTime TEST_DATE_TIME; 155 private ZonedDateTime TEST_DATE_TIME_PARIS; 156 157 @BeforeMethod(groups={"tck","implementation"}) 158 public void setUp() { 159 TEST_LOCAL_2008_06_30_11_30_59_500 = LocalDateTime.of(2008, 6, 30, 11, 30, 59, 500); 160 TEST_DATE_TIME = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100); 161 TEST_DATE_TIME_PARIS = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_PARIS); 162 TEST_PARIS_OVERLAP_2008_10_26_02_30 = LocalDateTime.of(2008, 10, 26, 2, 30); 163 TEST_PARIS_GAP_2008_03_30_02_30 = LocalDateTime.of(2008, 3, 30, 2, 30); 164 } 165 166 //----------------------------------------------------------------------- 167 @Override 168 protected List<TemporalAccessor> samples() { 169 TemporalAccessor[] array = {TEST_DATE_TIME, }; 170 return Arrays.asList(array); 171 } 172 173 @Override 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 EPOCH_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 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 @Test 222 public void test_serialization() throws ClassNotFoundException, IOException { 223 assertSerializable(TEST_DATE_TIME); 224 } 225 226 @Test 227 public void test_serialization_format_zoneId() throws Exception { 228 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 229 try (DataOutputStream dos = new DataOutputStream(baos) ) { 230 dos.writeByte(6); 231 dos.writeInt(2012); // date 232 dos.writeByte(9); 233 dos.writeByte(16); 234 dos.writeByte(22); // time 235 dos.writeByte(17); 236 dos.writeByte(59); 237 dos.writeInt(470_000_000); 238 dos.writeByte(4); // offset 239 dos.writeByte(7); // zoneId 240 dos.writeUTF("Europe/London"); 241 } 242 byte[] bytes = baos.toByteArray(); 243 ZonedDateTime zdt = LocalDateTime.of(2012, 9, 16, 22, 17, 59, 470_000_000).atZone(ZoneId.of("Europe/London")); 244 assertSerializedBySer(zdt, bytes); 245 } 246 247 @Test 248 public void test_serialization_format_zoneOffset() throws Exception { 249 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 250 try (DataOutputStream dos = new DataOutputStream(baos) ) { 251 dos.writeByte(6); 252 dos.writeInt(2012); // date 253 dos.writeByte(9); 254 dos.writeByte(16); 255 dos.writeByte(22); // time 256 dos.writeByte(17); 257 dos.writeByte(59); 258 dos.writeInt(470_000_000); 259 dos.writeByte(4); // offset 260 dos.writeByte(8); // zoneId 261 dos.writeByte(4); 262 } 263 byte[] bytes = baos.toByteArray(); 264 ZonedDateTime zdt = LocalDateTime.of(2012, 9, 16, 22, 17, 59, 470_000_000).atZone(ZoneOffset.ofHours(1)); 265 assertSerializedBySer(zdt, bytes); 266 } 267 268 //----------------------------------------------------------------------- 269 // now() 270 //----------------------------------------------------------------------- 271 @Test(groups={"tck"}) 272 public void now() { 273 ZonedDateTime expected = ZonedDateTime.now(Clock.systemDefaultZone()); 274 ZonedDateTime test = ZonedDateTime.now(); 275 long diff = Math.abs(test.getTime().toNanoOfDay() - expected.getTime().toNanoOfDay()); 276 if (diff >= 100000000) { 277 // may be date change 278 expected = ZonedDateTime.now(Clock.systemDefaultZone()); 279 test = ZonedDateTime.now(); 280 diff = Math.abs(test.getTime().toNanoOfDay() - expected.getTime().toNanoOfDay()); 281 } 282 assertTrue(diff < 100000000); // less than 0.1 secs 283 } 284 285 //----------------------------------------------------------------------- 286 // now(ZoneId) 287 //----------------------------------------------------------------------- 288 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 289 public void now_ZoneId_nullZoneId() { 290 ZonedDateTime.now((ZoneId) null); 291 } 292 293 @Test(groups={"tck"}) 294 public void now_ZoneId() { 295 ZoneId zone = ZoneId.of("UTC+01:02:03"); 296 ZonedDateTime expected = ZonedDateTime.now(Clock.system(zone)); 297 ZonedDateTime test = ZonedDateTime.now(zone); 298 for (int i = 0; i < 100; i++) { 299 if (expected.equals(test)) { 300 return; 301 } 302 expected = ZonedDateTime.now(Clock.system(zone)); 303 test = ZonedDateTime.now(zone); 304 } 305 assertEquals(test, expected); 306 } 307 308 //----------------------------------------------------------------------- 309 // now(Clock) 310 //----------------------------------------------------------------------- 311 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 312 public void now_Clock_nullClock() { 313 ZonedDateTime.now((Clock)null); 314 } 315 316 @Test(groups={"tck"}) 317 public void now_Clock_allSecsInDay_utc() { 318 for (int i = 0; i < (2 * 24 * 60 * 60); i++) { 319 Instant instant = Instant.ofEpochSecond(i).plusNanos(123456789L); 320 Clock clock = Clock.fixed(instant, ZoneOffset.UTC); 321 ZonedDateTime test = ZonedDateTime.now(clock); 322 assertEquals(test.getYear(), 1970); 323 assertEquals(test.getMonth(), Month.JANUARY); 324 assertEquals(test.getDayOfMonth(), (i < 24 * 60 * 60 ? 1 : 2)); 325 assertEquals(test.getHour(), (i / (60 * 60)) % 24); 326 assertEquals(test.getMinute(), (i / 60) % 60); 327 assertEquals(test.getSecond(), i % 60); 328 assertEquals(test.getNano(), 123456789); 329 assertEquals(test.getOffset(), ZoneOffset.UTC); 330 assertEquals(test.getZone(), ZoneOffset.UTC); 331 } 332 } 333 334 @Test(groups={"tck"}) 335 public void now_Clock_allSecsInDay_zone() { 336 ZoneId zone = ZoneId.of("Europe/London"); 337 for (int i = 0; i < (2 * 24 * 60 * 60); i++) { 338 Instant instant = Instant.ofEpochSecond(i).plusNanos(123456789L); 339 ZonedDateTime expected = ZonedDateTime.ofInstant(instant, zone); 340 Clock clock = Clock.fixed(expected.toInstant(), zone); 341 ZonedDateTime test = ZonedDateTime.now(clock); 342 assertEquals(test, expected); 343 } 344 } 345 346 @Test(groups={"tck"}) 347 public void now_Clock_allSecsInDay_beforeEpoch() { 348 LocalTime expected = LocalTime.MIDNIGHT.plusNanos(123456789L); 349 for (int i =-1; i >= -(24 * 60 * 60); i--) { 350 Instant instant = Instant.ofEpochSecond(i).plusNanos(123456789L); 351 Clock clock = Clock.fixed(instant, ZoneOffset.UTC); 352 ZonedDateTime test = ZonedDateTime.now(clock); 353 assertEquals(test.getYear(), 1969); 354 assertEquals(test.getMonth(), Month.DECEMBER); 355 assertEquals(test.getDayOfMonth(), 31); 356 expected = expected.minusSeconds(1); 357 assertEquals(test.getTime(), expected); 358 assertEquals(test.getOffset(), ZoneOffset.UTC); 359 assertEquals(test.getZone(), ZoneOffset.UTC); 360 } 361 } 362 363 @Test(groups={"tck"}) 364 public void now_Clock_offsets() { 365 ZonedDateTime base = ZonedDateTime.of(LocalDateTime.of(1970, 1, 1, 12, 0), ZoneOffset.UTC); 366 for (int i = -9; i < 15; i++) { 367 ZoneOffset offset = ZoneOffset.ofHours(i); 368 Clock clock = Clock.fixed(base.toInstant(), offset); 369 ZonedDateTime test = ZonedDateTime.now(clock); 370 assertEquals(test.getHour(), (12 + i) % 24); 371 assertEquals(test.getMinute(), 0); 372 assertEquals(test.getSecond(), 0); 373 assertEquals(test.getNano(), 0); 374 assertEquals(test.getOffset(), offset); 375 assertEquals(test.getZone(), offset); 376 } 377 } 378 379 //----------------------------------------------------------------------- 380 // dateTime factories 381 //----------------------------------------------------------------------- 382 void check(ZonedDateTime test, int y, int m, int d, int h, int min, int s, int n, ZoneOffset offset, ZoneId zone) { 383 assertEquals(test.getYear(), y); 384 assertEquals(test.getMonth().getValue(), m); 385 assertEquals(test.getDayOfMonth(), d); 386 assertEquals(test.getHour(), h); 387 assertEquals(test.getMinute(), min); 388 assertEquals(test.getSecond(), s); 389 assertEquals(test.getNano(), n); 390 assertEquals(test.getOffset(), offset); 391 assertEquals(test.getZone(), zone); 392 } 393 394 //----------------------------------------------------------------------- 395 // of(LocalDateTime, ZoneId) 396 //----------------------------------------------------------------------- 397 // TODO: tests of overlap/gap 398 399 @Test(groups={"tck"}) 400 public void factory_of_LocalDateTime() { 401 LocalDateTime base = LocalDateTime.of(2008, 6, 30, 11, 30, 10, 500); 402 ZonedDateTime test = ZonedDateTime.of(base, ZONE_PARIS); 403 check(test, 2008, 6, 30, 11, 30, 10, 500, OFFSET_0200, ZONE_PARIS); 404 } 405 406 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 407 public void factory_of_LocalDateTime_nullDateTime() { 408 ZonedDateTime.of((LocalDateTime) null, ZONE_PARIS); 409 } 410 411 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 412 public void factory_of_LocalDateTime_nullZone() { 413 LocalDateTime base = LocalDateTime.of(2008, 6, 30, 11, 30, 10, 500); 414 ZonedDateTime.of(base, null); 415 } 416 417 //----------------------------------------------------------------------- 418 // ofInstant(Instant, ZoneId) 419 //----------------------------------------------------------------------- 420 @Test(groups={"tck"}) 421 public void factory_ofInstant_Instant_ZR() { 422 Instant instant = LocalDateTime.of(2008, 6, 30, 11, 30, 10, 35).toInstant(OFFSET_0200); 423 ZonedDateTime test = ZonedDateTime.ofInstant(instant, ZONE_PARIS); 424 check(test, 2008, 6, 30, 11, 30, 10, 35, OFFSET_0200, ZONE_PARIS); 425 } 426 427 @Test(groups={"tck"}) 428 public void factory_ofInstant_Instant_ZO() { 429 Instant instant = LocalDateTime.of(2008, 6, 30, 11, 30, 10, 45).toInstant(OFFSET_0200); 430 ZonedDateTime test = ZonedDateTime.ofInstant(instant, OFFSET_0200); 431 check(test, 2008, 6, 30, 11, 30, 10, 45, OFFSET_0200, OFFSET_0200); 432 } 433 434 @Test(groups={"tck"}) 435 public void factory_ofInstant_Instant_inGap() { 436 Instant instant = TEST_PARIS_GAP_2008_03_30_02_30.toInstant(OFFSET_0100); 437 ZonedDateTime test = ZonedDateTime.ofInstant(instant, ZONE_PARIS); 438 check(test, 2008, 3, 30, 3, 30, 0, 0, OFFSET_0200, ZONE_PARIS); // one hour later in summer offset 439 } 440 441 @Test(groups={"tck"}) 442 public void factory_ofInstant_Instant_inOverlap_earlier() { 443 Instant instant = TEST_PARIS_OVERLAP_2008_10_26_02_30.toInstant(OFFSET_0200); 444 ZonedDateTime test = ZonedDateTime.ofInstant(instant, ZONE_PARIS); 445 check(test, 2008, 10, 26, 2, 30, 0, 0, OFFSET_0200, ZONE_PARIS); // same time and offset 446 } 447 448 @Test(groups={"tck"}) 449 public void factory_ofInstant_Instant_inOverlap_later() { 450 Instant instant = TEST_PARIS_OVERLAP_2008_10_26_02_30.toInstant(OFFSET_0100); 451 ZonedDateTime test = ZonedDateTime.ofInstant(instant, ZONE_PARIS); 452 check(test, 2008, 10, 26, 2, 30, 0, 0, OFFSET_0100, ZONE_PARIS); // same time and offset 453 } 454 455 @Test(groups={"tck"}) 456 public void factory_ofInstant_Instant_invalidOffset() { 457 Instant instant = LocalDateTime.of(2008, 6, 30, 11, 30, 10, 500).toInstant(OFFSET_0130); 458 ZonedDateTime test = ZonedDateTime.ofInstant(instant, ZONE_PARIS); 459 check(test, 2008, 6, 30, 12, 0, 10, 500, OFFSET_0200, ZONE_PARIS); // corrected offset, thus altered time 460 } 461 462 @Test(groups={"tck"}) 463 public void factory_ofInstant_allSecsInDay() { 464 for (int i = 0; i < (24 * 60 * 60); i++) { 465 Instant instant = Instant.ofEpochSecond(i); 466 ZonedDateTime test = ZonedDateTime.ofInstant(instant, OFFSET_0100); 467 assertEquals(test.getYear(), 1970); 468 assertEquals(test.getMonth(), Month.JANUARY); 469 assertEquals(test.getDayOfMonth(), 1 + (i >= 23 * 60 * 60 ? 1 : 0)); 470 assertEquals(test.getHour(), ((i / (60 * 60)) + 1) % 24); 471 assertEquals(test.getMinute(), (i / 60) % 60); 472 assertEquals(test.getSecond(), i % 60); 473 } 474 } 475 476 @Test(groups={"tck"}) 477 public void factory_ofInstant_allDaysInCycle() { 478 // sanity check using different algorithm 479 ZonedDateTime expected = LocalDateTime.of(1970, 1, 1, 0, 0, 0, 0).atZone(ZoneOffset.UTC); 480 for (long i = 0; i < 146097; i++) { 481 Instant instant = Instant.ofEpochSecond(i * 24L * 60L * 60L); 482 ZonedDateTime test = ZonedDateTime.ofInstant(instant, ZoneOffset.UTC); 483 assertEquals(test, expected); 484 expected = expected.plusDays(1); 485 } 486 } 487 488 @Test(groups={"tck"}) 489 public void factory_ofInstant_minWithMinOffset() { 490 long days_0000_to_1970 = (146097 * 5) - (30 * 365 + 7); 491 int year = Year.MIN_VALUE; 492 long days = (year * 365L + (year / 4 - year / 100 + year / 400)) - days_0000_to_1970; 493 Instant instant = Instant.ofEpochSecond(days * 24L * 60L * 60L - OFFSET_MIN.getTotalSeconds()); 494 ZonedDateTime test = ZonedDateTime.ofInstant(instant, OFFSET_MIN); 495 assertEquals(test.getYear(), Year.MIN_VALUE); 496 assertEquals(test.getMonth().getValue(), 1); 497 assertEquals(test.getDayOfMonth(), 1); 498 assertEquals(test.getOffset(), OFFSET_MIN); 499 assertEquals(test.getHour(), 0); 500 assertEquals(test.getMinute(), 0); 501 assertEquals(test.getSecond(), 0); 502 assertEquals(test.getNano(), 0); 503 } 504 505 @Test(groups={"tck"}) 506 public void factory_ofInstant_minWithMaxOffset() { 507 long days_0000_to_1970 = (146097 * 5) - (30 * 365 + 7); 508 int year = Year.MIN_VALUE; 509 long days = (year * 365L + (year / 4 - year / 100 + year / 400)) - days_0000_to_1970; 510 Instant instant = Instant.ofEpochSecond(days * 24L * 60L * 60L - OFFSET_MAX.getTotalSeconds()); 511 ZonedDateTime test = ZonedDateTime.ofInstant(instant, OFFSET_MAX); 512 assertEquals(test.getYear(), Year.MIN_VALUE); 513 assertEquals(test.getMonth().getValue(), 1); 514 assertEquals(test.getDayOfMonth(), 1); 515 assertEquals(test.getOffset(), OFFSET_MAX); 516 assertEquals(test.getHour(), 0); 517 assertEquals(test.getMinute(), 0); 518 assertEquals(test.getSecond(), 0); 519 assertEquals(test.getNano(), 0); 520 } 521 522 @Test(groups={"tck"}) 523 public void factory_ofInstant_maxWithMinOffset() { 524 long days_0000_to_1970 = (146097 * 5) - (30 * 365 + 7); 525 int year = Year.MAX_VALUE; 526 long days = (year * 365L + (year / 4 - year / 100 + year / 400)) + 365 - days_0000_to_1970; 527 Instant instant = Instant.ofEpochSecond((days + 1) * 24L * 60L * 60L - 1 - OFFSET_MIN.getTotalSeconds()); 528 ZonedDateTime test = ZonedDateTime.ofInstant(instant, OFFSET_MIN); 529 assertEquals(test.getYear(), Year.MAX_VALUE); 530 assertEquals(test.getMonth().getValue(), 12); 531 assertEquals(test.getDayOfMonth(), 31); 532 assertEquals(test.getOffset(), OFFSET_MIN); 533 assertEquals(test.getHour(), 23); 534 assertEquals(test.getMinute(), 59); 535 assertEquals(test.getSecond(), 59); 536 assertEquals(test.getNano(), 0); 537 } 538 539 @Test(groups={"tck"}) 540 public void factory_ofInstant_maxWithMaxOffset() { 541 long days_0000_to_1970 = (146097 * 5) - (30 * 365 + 7); 542 int year = Year.MAX_VALUE; 543 long days = (year * 365L + (year / 4 - year / 100 + year / 400)) + 365 - days_0000_to_1970; 544 Instant instant = Instant.ofEpochSecond((days + 1) * 24L * 60L * 60L - 1 - OFFSET_MAX.getTotalSeconds()); 545 ZonedDateTime test = ZonedDateTime.ofInstant(instant, OFFSET_MAX); 546 assertEquals(test.getYear(), Year.MAX_VALUE); 547 assertEquals(test.getMonth().getValue(), 12); 548 assertEquals(test.getDayOfMonth(), 31); 549 assertEquals(test.getOffset(), OFFSET_MAX); 550 assertEquals(test.getHour(), 23); 551 assertEquals(test.getMinute(), 59); 552 assertEquals(test.getSecond(), 59); 553 assertEquals(test.getNano(), 0); 554 } 555 556 //----------------------------------------------------------------------- 557 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 558 public void factory_ofInstant_maxInstantWithMaxOffset() { 559 Instant instant = Instant.ofEpochSecond(Long.MAX_VALUE); 560 ZonedDateTime.ofInstant(instant, OFFSET_MAX); 561 } 562 563 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 564 public void factory_ofInstant_maxInstantWithMinOffset() { 565 Instant instant = Instant.ofEpochSecond(Long.MAX_VALUE); 566 ZonedDateTime.ofInstant(instant, OFFSET_MIN); 567 } 568 569 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 570 public void factory_ofInstant_tooBig() { 571 long days_0000_to_1970 = (146097 * 5) - (30 * 365 + 7); 572 long year = Year.MAX_VALUE + 1L; 573 long days = (year * 365L + (year / 4 - year / 100 + year / 400)) - days_0000_to_1970; 574 Instant instant = Instant.ofEpochSecond(days * 24L * 60L * 60L); 575 ZonedDateTime.ofInstant(instant, ZoneOffset.UTC); 576 } 577 578 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 579 public void factory_ofInstant_tooLow() { 580 long days_0000_to_1970 = (146097 * 5) - (30 * 365 + 7); 581 int year = Year.MIN_VALUE - 1; 582 long days = (year * 365L + (year / 4 - year / 100 + year / 400)) - days_0000_to_1970; 583 Instant instant = Instant.ofEpochSecond(days * 24L * 60L * 60L); 584 ZonedDateTime.ofInstant(instant, ZoneOffset.UTC); 585 } 586 587 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 588 public void factory_ofInstant_Instant_nullInstant() { 589 ZonedDateTime.ofInstant((Instant) null, ZONE_0100); 590 } 591 592 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 593 public void factory_ofInstant_Instant_nullZone() { 594 ZonedDateTime.ofInstant(Instant.EPOCH, null); 595 } 596 597 //----------------------------------------------------------------------- 598 // ofStrict(LocalDateTime, ZoneId, ZoneOffset) 599 //----------------------------------------------------------------------- 600 @Test(groups={"tck"}) 601 public void factory_ofStrict_LDT_ZI_ZO() { 602 LocalDateTime normal = LocalDateTime.of(2008, 6, 30, 11, 30, 10, 500); 603 ZonedDateTime test = ZonedDateTime.ofStrict(normal, OFFSET_0200, ZONE_PARIS); 604 check(test, 2008, 6, 30, 11, 30, 10, 500, OFFSET_0200, ZONE_PARIS); 605 } 606 607 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 608 public void factory_ofStrict_LDT_ZI_ZO_inGap() { 609 try { 610 ZonedDateTime.ofStrict(TEST_PARIS_GAP_2008_03_30_02_30, OFFSET_0100, ZONE_PARIS); 611 } catch (DateTimeException ex) { 612 assertEquals(ex.getMessage().contains(" gap"), true); 613 throw ex; 614 } 615 } 616 617 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 618 public void factory_ofStrict_LDT_ZI_ZO_inOverlap_invalidOfset() { 619 try { 620 ZonedDateTime.ofStrict(TEST_PARIS_OVERLAP_2008_10_26_02_30, OFFSET_0130, ZONE_PARIS); 621 } catch (DateTimeException ex) { 622 assertEquals(ex.getMessage().contains(" is not valid for "), true); 623 throw ex; 624 } 625 } 626 627 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 628 public void factory_ofStrict_LDT_ZI_ZO_invalidOffset() { 629 try { 630 ZonedDateTime.ofStrict(TEST_LOCAL_2008_06_30_11_30_59_500, OFFSET_0130, ZONE_PARIS); 631 } catch (DateTimeException ex) { 632 assertEquals(ex.getMessage().contains(" is not valid for "), true); 633 throw ex; 634 } 635 } 636 637 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 638 public void factory_ofStrict_LDT_ZI_ZO_nullLDT() { 639 ZonedDateTime.ofStrict((LocalDateTime) null, OFFSET_0100, ZONE_PARIS); 640 } 641 642 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 643 public void factory_ofStrict_LDT_ZI_ZO_nullZO() { 644 ZonedDateTime.ofStrict(TEST_LOCAL_2008_06_30_11_30_59_500, null, ZONE_PARIS); 645 } 646 647 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 648 public void factory_ofStrict_LDT_ZI_ZO_nullZI() { 649 ZonedDateTime.ofStrict(TEST_LOCAL_2008_06_30_11_30_59_500, OFFSET_0100, null); 650 } 651 652 //----------------------------------------------------------------------- 653 // from(TemporalAccessor) 654 //----------------------------------------------------------------------- 655 @Test(groups={"tck"}) 656 public void factory_from_TemporalAccessor_ZDT() { 657 assertEquals(ZonedDateTime.from(TEST_DATE_TIME_PARIS), TEST_DATE_TIME_PARIS); 658 } 659 660 @Test(groups={"tck"}) 661 public void factory_from_TemporalAccessor_LDT_ZoneId() { 662 assertEquals(ZonedDateTime.from(new TemporalAccessor() { 663 @Override 664 public boolean isSupported(TemporalField field) { 665 return TEST_DATE_TIME_PARIS.getDateTime().isSupported(field); 666 } 667 @Override 668 public long getLong(TemporalField field) { 669 return TEST_DATE_TIME_PARIS.getDateTime().getLong(field); 670 } 671 @SuppressWarnings("unchecked") 672 @Override 673 public <R> R query(TemporalQuery<R> query) { 674 if (query == Queries.zoneId()) { 675 return (R) TEST_DATE_TIME_PARIS.getZone(); 676 } 677 return TemporalAccessor.super.query(query); 678 } 679 }), TEST_DATE_TIME_PARIS); 680 } 681 682 @Test(groups={"tck"}) 683 public void factory_from_TemporalAccessor_Instant_ZoneId() { 684 assertEquals(ZonedDateTime.from(new TemporalAccessor() { 685 @Override 686 public boolean isSupported(TemporalField field) { 687 return field == INSTANT_SECONDS || field == NANO_OF_SECOND; 688 } 689 690 @Override 691 public long getLong(TemporalField field) { 692 return TEST_DATE_TIME_PARIS.toInstant().getLong(field); 693 } 694 695 @SuppressWarnings("unchecked") 696 @Override 697 public <R> R query(TemporalQuery<R> query) { 698 if (query == Queries.zoneId()) { 699 return (R) TEST_DATE_TIME_PARIS.getZone(); 700 } 701 return TemporalAccessor.super.query(query); 702 } 703 }), TEST_DATE_TIME_PARIS); 704 } 705 706 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 707 public void factory_from_TemporalAccessor_invalid_noDerive() { 708 ZonedDateTime.from(LocalTime.of(12, 30)); 709 } 710 711 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 712 public void factory_from_TemporalAccessor_null() { 713 ZonedDateTime.from((TemporalAccessor) null); 714 } 715 716 //----------------------------------------------------------------------- 717 // parse() 718 //----------------------------------------------------------------------- 719 @Test(dataProvider="sampleToString", groups={"tck"}) 720 public void test_parse(int y, int month, int d, int h, int m, int s, int n, String zoneId, String text) { 721 ZonedDateTime t = ZonedDateTime.parse(text); 722 assertEquals(t.getYear(), y); 723 assertEquals(t.getMonth().getValue(), month); 724 assertEquals(t.getDayOfMonth(), d); 725 assertEquals(t.getHour(), h); 726 assertEquals(t.getMinute(), m); 727 assertEquals(t.getSecond(), s); 728 assertEquals(t.getNano(), n); 729 assertEquals(t.getZone().getId(), zoneId); 730 } 731 732 @Test(expectedExceptions=DateTimeParseException.class, groups={"tck"}) 733 public void factory_parse_illegalValue() { 734 ZonedDateTime.parse("2008-06-32T11:15+01:00[Europe/Paris]"); 735 } 736 737 @Test(expectedExceptions=DateTimeParseException.class, groups={"tck"}) 738 public void factory_parse_invalidValue() { 739 ZonedDateTime.parse("2008-06-31T11:15+01:00[Europe/Paris]"); 740 } 741 742 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 743 public void factory_parse_nullText() { 744 ZonedDateTime.parse((String) null); 745 } 746 747 //----------------------------------------------------------------------- 748 // parse(DateTimeFormatter) 749 //----------------------------------------------------------------------- 750 @Test(groups={"tck"}) 751 public void factory_parse_formatter() { 752 DateTimeFormatter f = DateTimeFormatters.pattern("y M d H m s I"); 753 ZonedDateTime test = ZonedDateTime.parse("2010 12 3 11 30 0 Europe/London", f); 754 assertEquals(test, ZonedDateTime.of(LocalDateTime.of(2010, 12, 3, 11, 30), ZoneId.of("Europe/London"))); 755 } 756 757 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 758 public void factory_parse_formatter_nullText() { 759 DateTimeFormatter f = DateTimeFormatters.pattern("y M d H m s"); 760 ZonedDateTime.parse((String) null, f); 761 } 762 763 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 764 public void factory_parse_formatter_nullFormatter() { 765 ZonedDateTime.parse("ANY", null); 766 } 767 768 //----------------------------------------------------------------------- 769 // basics 770 //----------------------------------------------------------------------- 771 @DataProvider(name="sampleTimes") 772 Object[][] provider_sampleTimes() { 773 return new Object[][] { 774 {2008, 6, 30, 11, 30, 20, 500, ZONE_0100}, 775 {2008, 6, 30, 11, 0, 0, 0, ZONE_0100}, 776 {2008, 6, 30, 11, 30, 20, 500, ZONE_PARIS}, 777 {2008, 6, 30, 11, 0, 0, 0, ZONE_PARIS}, 778 {2008, 6, 30, 23, 59, 59, 999999999, ZONE_0100}, 779 {-1, 1, 1, 0, 0, 0, 0, ZONE_0100}, 780 }; 781 } 782 783 @Test(dataProvider="sampleTimes", groups={"tck"}) 784 public void test_get(int y, int o, int d, int h, int m, int s, int n, ZoneId zone) { 785 LocalDate localDate = LocalDate.of(y, o, d); 786 LocalTime localTime = LocalTime.of(h, m, s, n); 787 LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime); 788 ZoneOffset offset = zone.getRules().getOffset(localDateTime); 789 ZonedDateTime a = ZonedDateTime.of(localDateTime, zone); 790 791 assertEquals(a.getYear(), localDate.getYear()); 792 assertEquals(a.getMonth(), localDate.getMonth()); 793 assertEquals(a.getDayOfMonth(), localDate.getDayOfMonth()); 794 assertEquals(a.getDayOfYear(), localDate.getDayOfYear()); 795 assertEquals(a.getDayOfWeek(), localDate.getDayOfWeek()); 796 797 assertEquals(a.getHour(), localTime.getHour()); 798 assertEquals(a.getMinute(), localTime.getMinute()); 799 assertEquals(a.getSecond(), localTime.getSecond()); 800 assertEquals(a.getNano(), localTime.getNano()); 801 802 assertEquals(a.getDate(), localDate); 803 assertEquals(a.getTime(), localTime); 804 assertEquals(a.getDateTime(), localDateTime); 805 if (zone instanceof ZoneOffset) { 806 assertEquals(a.toString(), localDateTime.toString() + offset.toString()); 807 } else { 808 assertEquals(a.toString(), localDateTime.toString() + offset.toString() + "[" + zone.toString() + "]"); 809 } 810 } 811 812 //----------------------------------------------------------------------- 813 // get(TemporalField) 814 //----------------------------------------------------------------------- 815 @Test 816 public void test_get_TemporalField() { 817 ZonedDateTime test = ZonedDateTime.of(LocalDateTime.of(2008, 6, 30, 12, 30, 40, 987654321), ZONE_0100); 818 assertEquals(test.get(ChronoField.YEAR), 2008); 819 assertEquals(test.get(ChronoField.MONTH_OF_YEAR), 6); 820 assertEquals(test.get(ChronoField.DAY_OF_MONTH), 30); 821 assertEquals(test.get(ChronoField.DAY_OF_WEEK), 1); 822 assertEquals(test.get(ChronoField.DAY_OF_YEAR), 182); 823 824 assertEquals(test.get(ChronoField.HOUR_OF_DAY), 12); 825 assertEquals(test.get(ChronoField.MINUTE_OF_HOUR), 30); 826 assertEquals(test.get(ChronoField.SECOND_OF_MINUTE), 40); 827 assertEquals(test.get(ChronoField.NANO_OF_SECOND), 987654321); 828 assertEquals(test.get(ChronoField.HOUR_OF_AMPM), 0); 829 assertEquals(test.get(ChronoField.AMPM_OF_DAY), 1); 830 831 assertEquals(test.get(ChronoField.OFFSET_SECONDS), 3600); 832 } 833 834 @Test 835 public void test_getLong_TemporalField() { 836 ZonedDateTime test = ZonedDateTime.of(LocalDateTime.of(2008, 6, 30, 12, 30, 40, 987654321), ZONE_0100); 837 assertEquals(test.getLong(ChronoField.YEAR), 2008); 838 assertEquals(test.getLong(ChronoField.MONTH_OF_YEAR), 6); 839 assertEquals(test.getLong(ChronoField.DAY_OF_MONTH), 30); 840 assertEquals(test.getLong(ChronoField.DAY_OF_WEEK), 1); 841 assertEquals(test.getLong(ChronoField.DAY_OF_YEAR), 182); 842 843 assertEquals(test.getLong(ChronoField.HOUR_OF_DAY), 12); 844 assertEquals(test.getLong(ChronoField.MINUTE_OF_HOUR), 30); 845 assertEquals(test.getLong(ChronoField.SECOND_OF_MINUTE), 40); 846 assertEquals(test.getLong(ChronoField.NANO_OF_SECOND), 987654321); 847 assertEquals(test.getLong(ChronoField.HOUR_OF_AMPM), 0); 848 assertEquals(test.getLong(ChronoField.AMPM_OF_DAY), 1); 849 850 assertEquals(test.getLong(ChronoField.OFFSET_SECONDS), 3600); 851 assertEquals(test.getLong(ChronoField.INSTANT_SECONDS), test.toEpochSecond()); 852 } 853 854 //----------------------------------------------------------------------- 855 // query(TemporalQuery) 856 //----------------------------------------------------------------------- 857 @Test 858 public void test_query_chrono() { 859 assertEquals(TEST_DATE_TIME.query(Queries.chrono()), ISOChrono.INSTANCE); 860 assertEquals(Queries.chrono().queryFrom(TEST_DATE_TIME), ISOChrono.INSTANCE); 861 } 862 863 @Test 864 public void test_query_zoneId() { 865 assertEquals(TEST_DATE_TIME.query(Queries.zoneId()), TEST_DATE_TIME.getZone()); 866 assertEquals(Queries.zoneId().queryFrom(TEST_DATE_TIME), TEST_DATE_TIME.getZone()); 867 } 868 869 @Test 870 public void test_query_precision() { 871 assertEquals(TEST_DATE_TIME.query(Queries.precision()), NANOS); 872 assertEquals(Queries.precision().queryFrom(TEST_DATE_TIME), NANOS); 873 } 874 875 @Test 876 public void test_query_offset() { 877 assertEquals(TEST_DATE_TIME.query(Queries.offset()), TEST_DATE_TIME.getOffset()); 878 assertEquals(Queries.offset().queryFrom(TEST_DATE_TIME), TEST_DATE_TIME.getOffset()); 879 } 880 881 @Test 882 public void test_query_zone() { 883 assertEquals(TEST_DATE_TIME.query(Queries.zone()), TEST_DATE_TIME.getZone()); 884 assertEquals(Queries.zone().queryFrom(TEST_DATE_TIME), TEST_DATE_TIME.getZone()); 885 } 886 887 @Test(expectedExceptions=NullPointerException.class) 888 public void test_query_null() { 889 TEST_DATE_TIME.query(null); 890 } 891 892 //----------------------------------------------------------------------- 893 // withEarlierOffsetAtOverlap() 894 //----------------------------------------------------------------------- 895 @Test(groups={"tck"}) 896 public void test_withEarlierOffsetAtOverlap_notAtOverlap() { 897 ZonedDateTime base = ZonedDateTime.ofStrict(TEST_LOCAL_2008_06_30_11_30_59_500, OFFSET_0200, ZONE_PARIS); 898 ZonedDateTime test = base.withEarlierOffsetAtOverlap(); 899 assertEquals(test, base); // not changed 900 } 901 902 @Test(groups={"tck"}) 903 public void test_withEarlierOffsetAtOverlap_atOverlap() { 904 ZonedDateTime base = ZonedDateTime.ofStrict(TEST_PARIS_OVERLAP_2008_10_26_02_30, OFFSET_0100, ZONE_PARIS); 905 ZonedDateTime test = base.withEarlierOffsetAtOverlap(); 906 assertEquals(test.getOffset(), OFFSET_0200); // offset changed to earlier 907 assertEquals(test.getDateTime(), base.getDateTime()); // date-time not changed 908 } 909 910 @Test(groups={"tck"}) 911 public void test_withEarlierOffsetAtOverlap_atOverlap_noChange() { 912 ZonedDateTime base = ZonedDateTime.ofStrict(TEST_PARIS_OVERLAP_2008_10_26_02_30, OFFSET_0200, ZONE_PARIS); 913 ZonedDateTime test = base.withEarlierOffsetAtOverlap(); 914 assertEquals(test, base); // not changed 915 } 916 917 //----------------------------------------------------------------------- 918 // withLaterOffsetAtOverlap() 919 //----------------------------------------------------------------------- 920 @Test(groups={"tck"}) 921 public void test_withLaterOffsetAtOverlap_notAtOverlap() { 922 ZonedDateTime base = ZonedDateTime.ofStrict(TEST_LOCAL_2008_06_30_11_30_59_500, OFFSET_0200, ZONE_PARIS); 923 ZonedDateTime test = base.withLaterOffsetAtOverlap(); 924 assertEquals(test, base); // not changed 925 } 926 927 @Test(groups={"tck"}) 928 public void test_withLaterOffsetAtOverlap_atOverlap() { 929 ZonedDateTime base = ZonedDateTime.ofStrict(TEST_PARIS_OVERLAP_2008_10_26_02_30, OFFSET_0200, ZONE_PARIS); 930 ZonedDateTime test = base.withLaterOffsetAtOverlap(); 931 assertEquals(test.getOffset(), OFFSET_0100); // offset changed to later 932 assertEquals(test.getDateTime(), base.getDateTime()); // date-time not changed 933 } 934 935 @Test(groups={"tck"}) 936 public void test_withLaterOffsetAtOverlap_atOverlap_noChange() { 937 ZonedDateTime base = ZonedDateTime.ofStrict(TEST_PARIS_OVERLAP_2008_10_26_02_30, OFFSET_0100, ZONE_PARIS); 938 ZonedDateTime test = base.withLaterOffsetAtOverlap(); 939 assertEquals(test, base); // not changed 940 } 941 942 //----------------------------------------------------------------------- 943 // withZoneSameLocal(ZoneId) 944 //----------------------------------------------------------------------- 945 @Test(groups={"tck"}) 946 public void test_withZoneSameLocal() { 947 LocalDateTime ldt = LocalDateTime.of(2008, 6, 30, 23, 30, 59, 0); 948 ZonedDateTime base = ZonedDateTime.of(ldt, ZONE_0100); 949 ZonedDateTime test = base.withZoneSameLocal(ZONE_0200); 950 assertEquals(test.getDateTime(), base.getDateTime()); 951 } 952 953 @Test(groups={"implementation"}) 954 public void test_withZoneSameLocal_noChange() { 955 LocalDateTime ldt = LocalDateTime.of(2008, 6, 30, 23, 30, 59, 0); 956 ZonedDateTime base = ZonedDateTime.of(ldt, ZONE_0100); 957 ZonedDateTime test = base.withZoneSameLocal(ZONE_0100); 958 assertEquals(test, base); 959 } 960 961 @Test(groups={"tck"}) 962 public void test_withZoneSameLocal_retainOffset1() { 963 LocalDateTime ldt = LocalDateTime.of(2008, 11, 2, 1, 30, 59, 0); // overlap 964 ZonedDateTime base = ZonedDateTime.of(ldt, ZoneId.of("UTC-04:00") ); 965 ZonedDateTime test = base.withZoneSameLocal(ZoneId.of("America/New_York")); 966 assertEquals(base.getOffset(), ZoneOffset.ofHours(-4)); 967 assertEquals(test.getOffset(), ZoneOffset.ofHours(-4)); 968 } 969 970 @Test(groups={"tck"}) 971 public void test_withZoneSameLocal_retainOffset2() { 972 LocalDateTime ldt = LocalDateTime.of(2008, 11, 2, 1, 30, 59, 0); // overlap 973 ZonedDateTime base = ZonedDateTime.of(ldt, ZoneId.of("UTC-05:00") ); 974 ZonedDateTime test = base.withZoneSameLocal(ZoneId.of("America/New_York")); 975 assertEquals(base.getOffset(), ZoneOffset.ofHours(-5)); 976 assertEquals(test.getOffset(), ZoneOffset.ofHours(-5)); 977 } 978 979 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 980 public void test_withZoneSameLocal_null() { 981 LocalDateTime ldt = LocalDateTime.of(2008, 6, 30, 23, 30, 59, 0); 982 ZonedDateTime base = ZonedDateTime.of(ldt, ZONE_0100); 983 base.withZoneSameLocal(null); 984 } 985 986 //----------------------------------------------------------------------- 987 // withZoneSameInstant() 988 //----------------------------------------------------------------------- 989 @Test(groups={"tck"}) 990 public void test_withZoneSameInstant() { 991 ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100); 992 ZonedDateTime test = base.withZoneSameInstant(ZONE_0200); 993 ZonedDateTime expected = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500.plusHours(1), ZONE_0200); 994 assertEquals(test, expected); 995 } 996 997 @Test(groups={"tck"}) 998 public void test_withZoneSameInstant_noChange() { 999 ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100); 1000 ZonedDateTime test = base.withZoneSameInstant(ZONE_0100); 1001 assertEquals(test, base); 1002 } 1003 1004 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 1005 public void test_withZoneSameInstant_null() { 1006 ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100); 1007 base.withZoneSameInstant(null); 1008 } 1009 1010 //----------------------------------------------------------------------- 1011 // withFixedOffsetZone() 1012 //----------------------------------------------------------------------- 1013 @Test(groups={"tck"}) 1014 public void test_withZoneLocked() { 1015 ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_PARIS); 1016 ZonedDateTime test = base.withFixedOffsetZone(); 1017 ZonedDateTime expected = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0200); 1018 assertEquals(test, expected); 1019 } 1020 1021 //----------------------------------------------------------------------- 1022 // with(WithAdjuster) 1023 //----------------------------------------------------------------------- 1024 @Test(groups={"tck"}) 1025 public void test_with_WithAdjuster_LocalDateTime_sameOffset() { 1026 ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_PARIS); 1027 ZonedDateTime test = base.with(LocalDateTime.of(2012, 7, 15, 14, 30)); 1028 check(test, 2012, 7, 15, 14, 30, 0, 0, OFFSET_0200, ZONE_PARIS); 1029 } 1030 1031 @Test(groups={"tck"}) 1032 public void test_with_WithAdjuster_LocalDateTime_adjustedOffset() { 1033 ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_PARIS); 1034 ZonedDateTime test = base.with(LocalDateTime.of(2012, 1, 15, 14, 30)); 1035 check(test, 2012, 1, 15, 14, 30, 0, 0, OFFSET_0100, ZONE_PARIS); 1036 } 1037 1038 @Test(groups={"tck"}) 1039 public void test_with_WithAdjuster_LocalDate() { 1040 ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_PARIS); 1041 ZonedDateTime test = base.with(LocalDate.of(2012, 7, 28)); 1042 check(test, 2012, 7, 28, 11, 30, 59, 500, OFFSET_0200, ZONE_PARIS); 1043 } 1044 1045 @Test(groups={"tck"}) 1046 public void test_with_WithAdjuster_LocalTime() { 1047 ZonedDateTime base = ZonedDateTime.of(TEST_PARIS_OVERLAP_2008_10_26_02_30, ZONE_PARIS); 1048 ZonedDateTime test = base.with(LocalTime.of(2, 29)); 1049 check(test, 2008, 10, 26, 2, 29, 0, 0, OFFSET_0200, ZONE_PARIS); 1050 } 1051 1052 @Test(groups={"tck"}) 1053 public void test_with_WithAdjuster_Year() { 1054 LocalDateTime ldt = LocalDateTime.of(2008, 6, 30, 23, 30, 59, 0); 1055 ZonedDateTime base = ZonedDateTime.of(ldt, ZONE_0100); 1056 ZonedDateTime test = base.with(Year.of(2007)); 1057 assertEquals(test, ZonedDateTime.of(ldt.withYear(2007), ZONE_0100)); 1058 } 1059 1060 @Test(groups={"tck"}) 1061 public void test_with_WithAdjuster_Month_adjustedDayOfMonth() { 1062 ZonedDateTime base = ZonedDateTime.of(LocalDateTime.of(2012, 7, 31, 0, 0), ZONE_PARIS); 1063 ZonedDateTime test = base.with(Month.JUNE); 1064 check(test, 2012, 6, 30, 0, 0, 0, 0, OFFSET_0200, ZONE_PARIS); 1065 } 1066 1067 @Test(groups={"tck"}) 1068 public void test_with_WithAdjuster_Offset_same() { 1069 ZonedDateTime base = ZonedDateTime.of(LocalDateTime.of(2012, 7, 31, 0, 0), ZONE_PARIS); 1070 ZonedDateTime test = base.with(ZoneOffset.ofHours(2)); 1071 check(test, 2012, 7, 31, 0, 0, 0, 0, OFFSET_0200, ZONE_PARIS); 1072 } 1073 1074 @Test(groups={"tck"}) 1075 public void test_with_WithAdjuster_Offset_timeAdjust() { 1076 ZonedDateTime base = ZonedDateTime.of(LocalDateTime.of(2012, 7, 31, 0, 0), ZONE_PARIS); 1077 ZonedDateTime test = base.with(ZoneOffset.ofHours(1)); 1078 check(test, 2012, 7, 31, 1, 0, 0, 0, OFFSET_0200, ZONE_PARIS); // time adjusted 1079 } 1080 1081 @Test(groups={"tck"}) 1082 public void test_with_WithAdjuster_LocalDate_retainOffset1() { 1083 ZoneId newYork = ZoneId.of("America/New_York"); 1084 LocalDateTime ldt = LocalDateTime.of(2008, 11, 1, 1, 30); 1085 ZonedDateTime base = ZonedDateTime.of(ldt, newYork); 1086 assertEquals(base.getOffset(), ZoneOffset.ofHours(-4)); 1087 ZonedDateTime test = base.with(LocalDate.of(2008, 11, 2)); 1088 assertEquals(test.getOffset(), ZoneOffset.ofHours(-4)); 1089 } 1090 1091 @Test(groups={"tck"}) 1092 public void test_with_WithAdjuster_LocalDate_retainOffset2() { 1093 ZoneId newYork = ZoneId.of("America/New_York"); 1094 LocalDateTime ldt = LocalDateTime.of(2008, 11, 3, 1, 30); 1095 ZonedDateTime base = ZonedDateTime.of(ldt, newYork); 1096 assertEquals(base.getOffset(), ZoneOffset.ofHours(-5)); 1097 ZonedDateTime test = base.with(LocalDate.of(2008, 11, 2)); 1098 assertEquals(test.getOffset(), ZoneOffset.ofHours(-5)); 1099 } 1100 1101 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 1102 public void test_with_WithAdjuster_null() { 1103 ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100); 1104 base.with((TemporalAdjuster) null); 1105 } 1106 1107 //----------------------------------------------------------------------- 1108 // withYear() 1109 //----------------------------------------------------------------------- 1110 @Test(groups={"tck"}) 1111 public void test_withYear_normal() { 1112 ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100); 1113 ZonedDateTime test = base.withYear(2007); 1114 assertEquals(test, ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500.withYear(2007), ZONE_0100)); 1115 } 1116 1117 @Test(groups={"tck"}) 1118 public void test_withYear_noChange() { 1119 ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100); 1120 ZonedDateTime test = base.withYear(2008); 1121 assertEquals(test, base); 1122 } 1123 1124 //----------------------------------------------------------------------- 1125 // with(Month) 1126 //----------------------------------------------------------------------- 1127 @Test(groups={"tck"}) 1128 public void test_withMonth_Month_normal() { 1129 ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100); 1130 ZonedDateTime test = base.with(JANUARY); 1131 assertEquals(test, ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500.withMonth(1), ZONE_0100)); 1132 } 1133 1134 @Test(expectedExceptions = NullPointerException.class, groups={"tck"}) 1135 public void test_withMonth_Month_null() { 1136 ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100); 1137 base.with((Month) null); 1138 } 1139 1140 //----------------------------------------------------------------------- 1141 // withMonth() 1142 //----------------------------------------------------------------------- 1143 @Test(groups={"tck"}) 1144 public void test_withMonth_normal() { 1145 ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100); 1146 ZonedDateTime test = base.withMonth(1); 1147 assertEquals(test, ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500.withMonth(1), ZONE_0100)); 1148 } 1149 1150 @Test(groups={"tck"}) 1151 public void test_withMonth_noChange() { 1152 ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100); 1153 ZonedDateTime test = base.withMonth(6); 1154 assertEquals(test, base); 1155 } 1156 1157 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 1158 public void test_withMonth_tooBig() { 1159 TEST_DATE_TIME.withMonth(13); 1160 } 1161 1162 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 1163 public void test_withMonth_tooSmall() { 1164 TEST_DATE_TIME.withMonth(0); 1165 } 1166 1167 //----------------------------------------------------------------------- 1168 // withDayOfMonth() 1169 //----------------------------------------------------------------------- 1170 @Test(groups={"tck"}) 1171 public void test_withDayOfMonth_normal() { 1172 ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100); 1173 ZonedDateTime test = base.withDayOfMonth(15); 1174 assertEquals(test, ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500.withDayOfMonth(15), ZONE_0100)); 1175 } 1176 1177 @Test(groups={"tck"}) 1178 public void test_withDayOfMonth_noChange() { 1179 ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100); 1180 ZonedDateTime test = base.withDayOfMonth(30); 1181 assertEquals(test, base); 1182 } 1183 1184 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 1185 public void test_withDayOfMonth_tooBig() { 1186 LocalDateTime.of(2007, 7, 2, 11, 30).atZone(ZONE_PARIS).withDayOfMonth(32); 1187 } 1188 1189 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 1190 public void test_withDayOfMonth_tooSmall() { 1191 TEST_DATE_TIME.withDayOfMonth(0); 1192 } 1193 1194 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 1195 public void test_withDayOfMonth_invalid31() { 1196 LocalDateTime.of(2007, 6, 2, 11, 30).atZone(ZONE_PARIS).withDayOfMonth(31); 1197 } 1198 1199 //----------------------------------------------------------------------- 1200 // withDayOfYear() 1201 //----------------------------------------------------------------------- 1202 @Test(groups={"tck"}) 1203 public void test_withDayOfYear_normal() { 1204 ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100); 1205 ZonedDateTime test = base.withDayOfYear(33); 1206 assertEquals(test, ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500.withDayOfYear(33), ZONE_0100)); 1207 } 1208 1209 @Test(groups={"tck"}) 1210 public void test_withDayOfYear_noChange() { 1211 LocalDateTime ldt = LocalDateTime.of(2008, 2, 5, 23, 30, 59, 0); 1212 ZonedDateTime base = ZonedDateTime.of(ldt, ZONE_0100); 1213 ZonedDateTime test = base.withDayOfYear(36); 1214 assertEquals(test, base); 1215 } 1216 1217 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 1218 public void test_withDayOfYear_tooBig() { 1219 TEST_DATE_TIME.withDayOfYear(367); 1220 } 1221 1222 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 1223 public void test_withDayOfYear_tooSmall() { 1224 TEST_DATE_TIME.withDayOfYear(0); 1225 } 1226 1227 @Test(expectedExceptions=DateTimeException.class, groups={"tck"}) 1228 public void test_withDayOfYear_invalid366() { 1229 LocalDateTime.of(2007, 2, 2, 11, 30).atZone(ZONE_PARIS).withDayOfYear(366); 1230 } 1231 1232 //----------------------------------------------------------------------- 1233 // withHour() 1234 //----------------------------------------------------------------------- 1235 @Test(groups={"tck"}) 1236 public void test_withHour_normal() { 1237 ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100); 1238 ZonedDateTime test = base.withHour(15); 1239 assertEquals(test, ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500.withHour(15), ZONE_0100)); 1240 } 1241 1242 @Test(groups={"tck"}) 1243 public void test_withHour_noChange() { 1244 ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100); 1245 ZonedDateTime test = base.withHour(11); 1246 assertEquals(test, base); 1247 } 1248 1249 //----------------------------------------------------------------------- 1250 // withMinute() 1251 //----------------------------------------------------------------------- 1252 @Test(groups={"tck"}) 1253 public void test_withMinute_normal() { 1254 ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100); 1255 ZonedDateTime test = base.withMinute(15); 1256 assertEquals(test, ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500.withMinute(15), ZONE_0100)); 1257 } 1258 1259 @Test(groups={"tck"}) 1260 public void test_withMinute_noChange() { 1261 ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100); 1262 ZonedDateTime test = base.withMinute(30); 1263 assertEquals(test, base); 1264 } 1265 1266 //----------------------------------------------------------------------- 1267 // withSecond() 1268 //----------------------------------------------------------------------- 1269 @Test(groups={"tck"}) 1270 public void test_withSecond_normal() { 1271 ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100); 1272 ZonedDateTime test = base.withSecond(12); 1273 assertEquals(test, ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500.withSecond(12), ZONE_0100)); 1274 } 1275 1276 @Test(groups={"tck"}) 1277 public void test_withSecond_noChange() { 1278 ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100); 1279 ZonedDateTime test = base.withSecond(59); 1280 assertEquals(test, base); 1281 } 1282 1283 //----------------------------------------------------------------------- 1284 // withNano() 1285 //----------------------------------------------------------------------- 1286 @Test(groups={"tck"}) 1287 public void test_withNanoOfSecond_normal() { 1288 ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100); 1289 ZonedDateTime test = base.withNano(15); 1290 assertEquals(test, ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500.withNano(15), ZONE_0100)); 1291 } 1292 1293 @Test(groups={"tck"}) 1294 public void test_withNanoOfSecond_noChange() { 1295 ZonedDateTime base = ZonedDateTime.of(TEST_LOCAL_2008_06_30_11_30_59_500, ZONE_0100); 1296 ZonedDateTime test = base.withNano(500); 1297 assertEquals(test, base); 1298 } 1299 1300 //----------------------------------------------------------------------- 1301 // truncatedTo(TemporalUnit) 1302 //----------------------------------------------------------------------- 1303 @Test(groups={"tck"}) 1304 public void test_truncatedTo_normal() { 1305 assertEquals(TEST_DATE_TIME.truncatedTo(NANOS), TEST_DATE_TIME); 1306 assertEquals(TEST_DATE_TIME.truncatedTo(SECONDS), TEST_DATE_TIME.withNano(0)); 1307 assertEquals(TEST_DATE_TIME.truncatedTo(DAYS), TEST_DATE_TIME.with(LocalTime.MIDNIGHT)); 1308 } 1309 1310 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 1311 public void test_truncatedTo_null() { 1312 TEST_DATE_TIME.truncatedTo(null); 1313 } 1314 1315 //----------------------------------------------------------------------- 1316 // plus/minus 1317 //----------------------------------------------------------------------- 1318 @DataProvider(name="plusDays") 1319 Object[][] data_plusDays() { 1320 return new Object[][] { 1321 // normal 1322 {dateTime(2008, 6, 30, 23, 30, 59, 0, OFFSET_0100, ZONE_0100), 0, dateTime(2008, 6, 30, 23, 30, 59, 0, OFFSET_0100, ZONE_0100)}, 1323 {dateTime(2008, 6, 30, 23, 30, 59, 0, OFFSET_0100, ZONE_0100), 1, dateTime(2008, 7, 1, 23, 30, 59, 0, OFFSET_0100, ZONE_0100)}, 1324 {dateTime(2008, 6, 30, 23, 30, 59, 0, OFFSET_0100, ZONE_0100), -1, dateTime(2008, 6, 29, 23, 30, 59, 0, OFFSET_0100, ZONE_0100)}, 1325 // skip over gap 1326 {dateTime(2008, 3, 30, 1, 30, 0, 0, OFFSET_0100, ZONE_PARIS), 1, dateTime(2008, 3, 31, 1, 30, 0, 0, OFFSET_0200, ZONE_PARIS)}, 1327 {dateTime(2008, 3, 30, 3, 30, 0, 0, OFFSET_0200, ZONE_PARIS), -1, dateTime(2008, 3, 29, 3, 30, 0, 0, OFFSET_0100, ZONE_PARIS)}, 1328 // land in gap 1329 {dateTime(2008, 3, 29, 2, 30, 0, 0, OFFSET_0100, ZONE_PARIS), 1, dateTime(2008, 3, 30, 3, 30, 0, 0, OFFSET_0200, ZONE_PARIS)}, 1330 {dateTime(2008, 3, 31, 2, 30, 0, 0, OFFSET_0200, ZONE_PARIS), -1, dateTime(2008, 3, 30, 3, 30, 0, 0, OFFSET_0200, ZONE_PARIS)}, 1331 // skip over overlap 1332 {dateTime(2008, 10, 26, 1, 30, 0, 0, OFFSET_0200, ZONE_PARIS), 1, dateTime(2008, 10, 27, 1, 30, 0, 0, OFFSET_0100, ZONE_PARIS)}, 1333 {dateTime(2008, 10, 25, 3, 30, 0, 0, OFFSET_0200, ZONE_PARIS), 1, dateTime(2008, 10, 26, 3, 30, 0, 0, OFFSET_0100, ZONE_PARIS)}, 1334 // land in overlap 1335 {dateTime(2008, 10, 25, 2, 30, 0, 0, OFFSET_0200, ZONE_PARIS), 1, dateTime(2008, 10, 26, 2, 30, 0, 0, OFFSET_0200, ZONE_PARIS)}, 1336 {dateTime(2008, 10, 27, 2, 30, 0, 0, OFFSET_0100, ZONE_PARIS), -1, dateTime(2008, 10, 26, 2, 30, 0, 0, OFFSET_0100, ZONE_PARIS)}, 1337 }; 1338 } 1339 1340 @DataProvider(name="plusTime") 1341 Object[][] data_plusTime() { 1342 return new Object[][] { 1343 // normal 1344 {dateTime(2008, 6, 30, 23, 30, 59, 0, OFFSET_0100, ZONE_0100), 0, dateTime(2008, 6, 30, 23, 30, 59, 0, OFFSET_0100, ZONE_0100)}, 1345 {dateTime(2008, 6, 30, 23, 30, 59, 0, OFFSET_0100, ZONE_0100), 1, dateTime(2008, 7, 1, 0, 30, 59, 0, OFFSET_0100, ZONE_0100)}, 1346 {dateTime(2008, 6, 30, 23, 30, 59, 0, OFFSET_0100, ZONE_0100), -1, dateTime(2008, 6, 30, 22, 30, 59, 0, OFFSET_0100, ZONE_0100)}, 1347 // gap 1348 {dateTime(2008, 3, 30, 1, 30, 0, 0, OFFSET_0100, ZONE_PARIS), 1, dateTime(2008, 3, 30, 3, 30, 0, 0, OFFSET_0200, ZONE_PARIS)}, 1349 {dateTime(2008, 3, 30, 3, 30, 0, 0, OFFSET_0200, ZONE_PARIS), -1, dateTime(2008, 3, 30, 1, 30, 0, 0, OFFSET_0100, ZONE_PARIS)}, 1350 // overlap 1351 {dateTime(2008, 10, 26, 1, 30, 0, 0, OFFSET_0200, ZONE_PARIS), 1, dateTime(2008, 10, 26, 2, 30, 0, 0, OFFSET_0200, ZONE_PARIS)}, 1352 {dateTime(2008, 10, 26, 1, 30, 0, 0, OFFSET_0200, ZONE_PARIS), 2, dateTime(2008, 10, 26, 2, 30, 0, 0, OFFSET_0100, ZONE_PARIS)}, 1353 {dateTime(2008, 10, 26, 1, 30, 0, 0, OFFSET_0200, ZONE_PARIS), 3, dateTime(2008, 10, 26, 3, 30, 0, 0, OFFSET_0100, ZONE_PARIS)}, 1354 {dateTime(2008, 10, 26, 2, 30, 0, 0, OFFSET_0200, ZONE_PARIS), 1, dateTime(2008, 10, 26, 2, 30, 0, 0, OFFSET_0100, ZONE_PARIS)}, 1355 {dateTime(2008, 10, 26, 2, 30, 0, 0, OFFSET_0200, ZONE_PARIS), 2, dateTime(2008, 10, 26, 3, 30, 0, 0, OFFSET_0100, ZONE_PARIS)}, 1356 }; 1357 } 1358 1359 //----------------------------------------------------------------------- 1360 // plus(adjuster) 1361 //----------------------------------------------------------------------- 1362 @Test(groups={"tck"}, dataProvider="plusDays") 1363 public void test_plus_adjuster_Period_days(ZonedDateTime base, long amount, ZonedDateTime expected) { 1364 assertEquals(base.plus(Period.of(amount, DAYS)), expected); 1365 } 1366 1367 @Test(groups={"tck"}, dataProvider="plusTime") 1368 public void test_plus_adjuster_Period_hours(ZonedDateTime base, long amount, ZonedDateTime expected) { 1369 assertEquals(base.plus(Period.of(amount, HOURS)), expected); 1370 } 1371 1372 @Test(groups={"tck"}, dataProvider="plusTime") 1373 public void test_plus_adjuster_Duration_hours(ZonedDateTime base, long amount, ZonedDateTime expected) { 1374 assertEquals(base.plus(Duration.ofHours(amount)), expected); 1375 } 1376 1377 @Test(groups={"tck"}) 1378 public void test_plus_adjuster() { 1379 MockSimplePeriod period = MockSimplePeriod.of(7, ChronoUnit.MONTHS); 1380 ZonedDateTime t = ZonedDateTime.of(LocalDateTime.of(2008, 6, 1, 12, 30, 59, 500), ZONE_0100); 1381 ZonedDateTime expected = ZonedDateTime.of(LocalDateTime.of(2009, 1, 1, 12, 30, 59, 500), ZONE_0100); 1382 assertEquals(t.plus(period), expected); 1383 } 1384 1385 @Test(groups={"tck"}) 1386 public void test_plus_adjuster_Duration() { 1387 Duration duration = Duration.ofSeconds(4L * 60 * 60 + 5L * 60 + 6L); 1388 ZonedDateTime t = ZonedDateTime.of(LocalDateTime.of(2008, 6, 1, 12, 30, 59, 500), ZONE_0100); 1389 ZonedDateTime expected = ZonedDateTime.of(LocalDateTime.of(2008, 6, 1, 16, 36, 5, 500), ZONE_0100); 1390 assertEquals(t.plus(duration), expected); 1391 } 1392 1393 @Test(groups={"tck"}) 1394 public void test_plus_adjuster_Period_zero() { 1395 ZonedDateTime t = TEST_DATE_TIME.plus(MockSimplePeriod.ZERO_DAYS); 1396 assertEquals(t, TEST_DATE_TIME); 1397 } 1398 1399 @Test(groups={"tck"}) 1400 public void test_plus_adjuster_Duration_zero() { 1401 ZonedDateTime t = TEST_DATE_TIME.plus(Duration.ZERO); 1402 assertEquals(t, TEST_DATE_TIME); 1403 } 1404 1405 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 1406 public void test_plus_adjuster_null() { 1407 TEST_DATE_TIME.plus((TemporalAdder) null); 1408 } 1409 1410 //----------------------------------------------------------------------- 1411 // plus(long,TemporalUnit) 1412 //----------------------------------------------------------------------- 1413 @Test(groups={"tck"}, dataProvider="plusDays") 1414 public void test_plus_longUnit_days(ZonedDateTime base, long amount, ZonedDateTime expected) { 1415 assertEquals(base.plus(amount, DAYS), expected); 1416 } 1417 1418 @Test(groups={"tck"}, dataProvider="plusTime") 1419 public void test_plus_longUnit_hours(ZonedDateTime base, long amount, ZonedDateTime expected) { 1420 assertEquals(base.plus(amount, HOURS), expected); 1421 } 1422 1423 @Test(groups={"tck"}, dataProvider="plusTime") 1424 public void test_plus_longUnit_minutes(ZonedDateTime base, long amount, ZonedDateTime expected) { 1425 assertEquals(base.plus(amount * 60, MINUTES), expected); 1426 } 1427 1428 @Test(groups={"tck"}, dataProvider="plusTime") 1429 public void test_plus_longUnit_seconds(ZonedDateTime base, long amount, ZonedDateTime expected) { 1430 assertEquals(base.plus(amount * 3600, SECONDS), expected); 1431 } 1432 1433 @Test(groups={"tck"}, dataProvider="plusTime") 1434 public void test_plus_longUnit_nanos(ZonedDateTime base, long amount, ZonedDateTime expected) { 1435 assertEquals(base.plus(amount * 3600_000_000_000L, NANOS), expected); 1436 } 1437 1438 @Test(groups={"tck"}, expectedExceptions=NullPointerException.class) 1439 public void test_plus_longUnit_null() { 1440 TEST_DATE_TIME_PARIS.plus(0, null); 1441 } 1442 1443 //----------------------------------------------------------------------- 1444 // plusYears() 1445 //----------------------------------------------------------------------- 1446 @Test(groups={"tck"}) 1447 public void test_plusYears() { 1448 LocalDateTime ldt = LocalDateTime.of(2008, 6, 30, 23, 30, 59, 0); 1449 ZonedDateTime base = ZonedDateTime.of(ldt, ZONE_0100); 1450 ZonedDateTime test = base.plusYears(1); 1451 assertEquals(test, ZonedDateTime.of(ldt.plusYears(1), ZONE_0100)); 1452 } 1453 1454 @Test(groups={"tck"}) 1455 public void test_plusYears_zero() { 1456 LocalDateTime ldt = LocalDateTime.of(2008, 6, 30, 23, 30, 59, 0); 1457 ZonedDateTime base = ZonedDateTime.of(ldt, ZONE_0100); 1458 ZonedDateTime test = base.plusYears(0); 1459 assertEquals(test, base); 1460 } 1461 1462 //----------------------------------------------------------------------- 1463 // plusMonths() 1464 //----------------------------------------------------------------------- 1465 @Test(groups={"tck"}) 1466 public void test_plusMonths() { 1467 LocalDateTime ldt = LocalDateTime.of(2008, 6, 30, 23, 30, 59, 0); 1468 ZonedDateTime base = ZonedDateTime.of(ldt, ZONE_0100); 1469 ZonedDateTime test = base.plusMonths(1); 1470 assertEquals(test, ZonedDateTime.of(ldt.plusMonths(1), ZONE_0100)); 1471 } 1472 1473 @Test(groups={"tck"}) 1474 public void test_plusMonths_zero() { 1475 LocalDateTime ldt = LocalDateTime.of(2008, 6, 30, 23, 30, 59, 0); 1476 ZonedDateTime base = ZonedDateTime.of(ldt, ZONE_0100); 1477 ZonedDateTime test = base.plusMonths(0); 1478 assertEquals(test, base); 1479 } 1480 1481 //----------------------------------------------------------------------- 1482 // plusWeeks() 1483 //----------------------------------------------------------------------- 1484 @Test(groups={"tck"}) 1485 public void test_plusWeeks() { 1486 LocalDateTime ldt = LocalDateTime.of(2008, 6, 30, 23, 30, 59, 0); 1487 ZonedDateTime base = ZonedDateTime.of(ldt, ZONE_0100); 1488 ZonedDateTime test = base.plusWeeks(1); 1489 assertEquals(test, ZonedDateTime.of(ldt.plusWeeks(1), ZONE_0100)); 1490 } 1491 1492 @Test(groups={"tck"}) 1493 public void test_plusWeeks_zero() { 1494 LocalDateTime ldt = LocalDateTime.of(2008, 6, 30, 23, 30, 59, 0); 1495 ZonedDateTime base = ZonedDateTime.of(ldt, ZONE_0100); 1496 ZonedDateTime test = base.plusWeeks(0); 1497 assertEquals(test, base); 1498 } 1499 1500 //----------------------------------------------------------------------- 1501 // plusDays() 1502 //----------------------------------------------------------------------- 1503 @Test(groups={"tck"}, dataProvider="plusDays") 1504 public void test_plusDays(ZonedDateTime base, long amount, ZonedDateTime expected) { 1505 assertEquals(base.plusDays(amount), expected); 1506 } 1507 1508 //----------------------------------------------------------------------- 1509 // plusHours() 1510 //----------------------------------------------------------------------- 1511 @Test(groups={"tck"}, dataProvider="plusTime") 1512 public void test_plusHours(ZonedDateTime base, long amount, ZonedDateTime expected) { 1513 assertEquals(base.plusHours(amount), expected); 1514 } 1515 1516 //----------------------------------------------------------------------- 1517 // plusMinutes() 1518 //----------------------------------------------------------------------- 1519 @Test(groups={"tck"}, dataProvider="plusTime") 1520 public void test_plusMinutes(ZonedDateTime base, long amount, ZonedDateTime expected) { 1521 assertEquals(base.plusMinutes(amount * 60), expected); 1522 } 1523 1524 @Test(groups={"tck"}) 1525 public void test_plusMinutes_minutes() { 1526 LocalDateTime ldt = LocalDateTime.of(2008, 6, 30, 23, 30, 59, 0); 1527 ZonedDateTime base = ZonedDateTime.of(ldt, ZONE_0100); 1528 ZonedDateTime test = base.plusMinutes(30); 1529 assertEquals(test, ZonedDateTime.of(ldt.plusMinutes(30), ZONE_0100)); 1530 } 1531 1532 //----------------------------------------------------------------------- 1533 // plusSeconds() 1534 //----------------------------------------------------------------------- 1535 @Test(groups={"tck"}, dataProvider="plusTime") 1536 public void test_plusSeconds(ZonedDateTime base, long amount, ZonedDateTime expected) { 1537 assertEquals(base.plusSeconds(amount * 3600), expected); 1538 } 1539 1540 @Test(groups={"tck"}) 1541 public void test_plusSeconds_seconds() { 1542 LocalDateTime ldt = LocalDateTime.of(2008, 6, 30, 23, 30, 59, 0); 1543 ZonedDateTime base = ZonedDateTime.of(ldt, ZONE_0100); 1544 ZonedDateTime test = base.plusSeconds(1); 1545 assertEquals(test, ZonedDateTime.of(ldt.plusSeconds(1), ZONE_0100)); 1546 } 1547 1548 //----------------------------------------------------------------------- 1549 // plusNanos() 1550 //----------------------------------------------------------------------- 1551 @Test(groups={"tck"}, dataProvider="plusTime") 1552 public void test_plusNanos(ZonedDateTime base, long amount, ZonedDateTime expected) { 1553 assertEquals(base.plusNanos(amount * 3600_000_000_000L), expected); 1554 } 1555 1556 @Test(groups={"tck"}) 1557 public void test_plusNanos_nanos() { 1558 LocalDateTime ldt = LocalDateTime.of(2008, 6, 30, 23, 30, 59, 0); 1559 ZonedDateTime base = ZonedDateTime.of(ldt, ZONE_0100); 1560 ZonedDateTime test = base.plusNanos(1); 1561 assertEquals(test, ZonedDateTime.of(ldt.plusNanos(1), ZONE_0100)); 1562 } 1563 1564 //----------------------------------------------------------------------- 1565 // minus(adjuster) 1566 //----------------------------------------------------------------------- 1567 @Test(groups={"tck"}, dataProvider="plusDays") 1568 public void test_minus_adjuster_Period_days(ZonedDateTime base, long amount, ZonedDateTime expected) { 1569 assertEquals(base.minus(Period.of(-amount, DAYS)), expected); 1570 } 1571 1572 @Test(groups={"tck"}, dataProvider="plusTime") 1573 public void test_minus_adjuster_Period_hours(ZonedDateTime base, long amount, ZonedDateTime expected) { 1574 assertEquals(base.minus(Period.of(-amount, HOURS)), expected); 1575 } 1576 1577 @Test(groups={"tck"}, dataProvider="plusTime") 1578 public void test_minus_adjuster_Duration_hours(ZonedDateTime base, long amount, ZonedDateTime expected) { 1579 assertEquals(base.minus(Duration.ofHours(-amount)), expected); 1580 } 1581 1582 @Test(groups={"tck"}) 1583 public void test_minus_adjuster() { 1584 MockSimplePeriod period = MockSimplePeriod.of(7, ChronoUnit.MONTHS); 1585 ZonedDateTime t = ZonedDateTime.of(LocalDateTime.of(2008, 6, 1, 12, 30, 59, 500), ZONE_0100); 1586 ZonedDateTime expected = ZonedDateTime.of(LocalDateTime.of(2007, 11, 1, 12, 30, 59, 500), ZONE_0100); 1587 assertEquals(t.minus(period), expected); 1588 } 1589 1590 @Test(groups={"tck"}) 1591 public void test_minus_adjuster_Duration() { 1592 Duration duration = Duration.ofSeconds(4L * 60 * 60 + 5L * 60 + 6L); 1593 ZonedDateTime t = ZonedDateTime.of(LocalDateTime.of(2008, 6, 1, 12, 30, 59, 500), ZONE_0100); 1594 ZonedDateTime expected = ZonedDateTime.of(LocalDateTime.of(2008, 6, 1, 8, 25, 53, 500), ZONE_0100); 1595 assertEquals(t.minus(duration), expected); 1596 } 1597 1598 @Test(groups={"tck"}) 1599 public void test_minus_adjuster_Period_zero() { 1600 ZonedDateTime t = TEST_DATE_TIME.minus(MockSimplePeriod.ZERO_DAYS); 1601 assertEquals(t, TEST_DATE_TIME); 1602 } 1603 1604 @Test(groups={"tck"}) 1605 public void test_minus_adjuster_Duration_zero() { 1606 ZonedDateTime t = TEST_DATE_TIME.minus(Duration.ZERO); 1607 assertEquals(t, TEST_DATE_TIME); 1608 } 1609 1610 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 1611 public void test_minus_adjuster_null() { 1612 TEST_DATE_TIME.minus((TemporalSubtractor) null); 1613 } 1614 1615 //----------------------------------------------------------------------- 1616 // minusYears() 1617 //----------------------------------------------------------------------- 1618 @Test(groups={"tck"}) 1619 public void test_minusYears() { 1620 LocalDateTime ldt = LocalDateTime.of(2008, 6, 30, 23, 30, 59, 0); 1621 ZonedDateTime base = ZonedDateTime.of(ldt, ZONE_0100); 1622 ZonedDateTime test = base.minusYears(1); 1623 assertEquals(test, ZonedDateTime.of(ldt.minusYears(1), ZONE_0100)); 1624 } 1625 1626 @Test(groups={"tck"}) 1627 public void test_minusYears_zero() { 1628 LocalDateTime ldt = LocalDateTime.of(2008, 6, 30, 23, 30, 59, 0); 1629 ZonedDateTime base = ZonedDateTime.of(ldt, ZONE_0100); 1630 ZonedDateTime test = base.minusYears(0); 1631 assertEquals(test, base); 1632 } 1633 1634 //----------------------------------------------------------------------- 1635 // minusMonths() 1636 //----------------------------------------------------------------------- 1637 @Test(groups={"tck"}) 1638 public void test_minusMonths() { 1639 LocalDateTime ldt = LocalDateTime.of(2008, 6, 30, 23, 30, 59, 0); 1640 ZonedDateTime base = ZonedDateTime.of(ldt, ZONE_0100); 1641 ZonedDateTime test = base.minusMonths(1); 1642 assertEquals(test, ZonedDateTime.of(ldt.minusMonths(1), ZONE_0100)); 1643 } 1644 1645 @Test(groups={"tck"}) 1646 public void test_minusMonths_zero() { 1647 LocalDateTime ldt = LocalDateTime.of(2008, 6, 30, 23, 30, 59, 0); 1648 ZonedDateTime base = ZonedDateTime.of(ldt, ZONE_0100); 1649 ZonedDateTime test = base.minusMonths(0); 1650 assertEquals(test, base); 1651 } 1652 1653 //----------------------------------------------------------------------- 1654 // minusWeeks() 1655 //----------------------------------------------------------------------- 1656 @Test(groups={"tck"}) 1657 public void test_minusWeeks() { 1658 LocalDateTime ldt = LocalDateTime.of(2008, 6, 30, 23, 30, 59, 0); 1659 ZonedDateTime base = ZonedDateTime.of(ldt, ZONE_0100); 1660 ZonedDateTime test = base.minusWeeks(1); 1661 assertEquals(test, ZonedDateTime.of(ldt.minusWeeks(1), ZONE_0100)); 1662 } 1663 1664 @Test(groups={"tck"}) 1665 public void test_minusWeeks_zero() { 1666 LocalDateTime ldt = LocalDateTime.of(2008, 6, 30, 23, 30, 59, 0); 1667 ZonedDateTime base = ZonedDateTime.of(ldt, ZONE_0100); 1668 ZonedDateTime test = base.minusWeeks(0); 1669 assertEquals(test, base); 1670 } 1671 1672 //----------------------------------------------------------------------- 1673 // minusDays() 1674 //----------------------------------------------------------------------- 1675 @Test(groups={"tck"}, dataProvider="plusDays") 1676 public void test_minusDays(ZonedDateTime base, long amount, ZonedDateTime expected) { 1677 assertEquals(base.minusDays(-amount), expected); 1678 } 1679 1680 //----------------------------------------------------------------------- 1681 // minusHours() 1682 //----------------------------------------------------------------------- 1683 @Test(groups={"tck"}, dataProvider="plusTime") 1684 public void test_minusHours(ZonedDateTime base, long amount, ZonedDateTime expected) { 1685 assertEquals(base.minusHours(-amount), expected); 1686 } 1687 1688 //----------------------------------------------------------------------- 1689 // minusMinutes() 1690 //----------------------------------------------------------------------- 1691 @Test(groups={"tck"}, dataProvider="plusTime") 1692 public void test_minusMinutes(ZonedDateTime base, long amount, ZonedDateTime expected) { 1693 assertEquals(base.minusMinutes(-amount * 60), expected); 1694 } 1695 1696 @Test(groups={"tck"}) 1697 public void test_minusMinutes_minutes() { 1698 LocalDateTime ldt = LocalDateTime.of(2008, 6, 30, 23, 30, 59, 0); 1699 ZonedDateTime base = ZonedDateTime.of(ldt, ZONE_0100); 1700 ZonedDateTime test = base.minusMinutes(30); 1701 assertEquals(test, ZonedDateTime.of(ldt.minusMinutes(30), ZONE_0100)); 1702 } 1703 1704 //----------------------------------------------------------------------- 1705 // minusSeconds() 1706 //----------------------------------------------------------------------- 1707 @Test(groups={"tck"}, dataProvider="plusTime") 1708 public void test_minusSeconds(ZonedDateTime base, long amount, ZonedDateTime expected) { 1709 assertEquals(base.minusSeconds(-amount * 3600), expected); 1710 } 1711 1712 @Test(groups={"tck"}) 1713 public void test_minusSeconds_seconds() { 1714 LocalDateTime ldt = LocalDateTime.of(2008, 6, 30, 23, 30, 59, 0); 1715 ZonedDateTime base = ZonedDateTime.of(ldt, ZONE_0100); 1716 ZonedDateTime test = base.minusSeconds(1); 1717 assertEquals(test, ZonedDateTime.of(ldt.minusSeconds(1), ZONE_0100)); 1718 } 1719 1720 //----------------------------------------------------------------------- 1721 // minusNanos() 1722 //----------------------------------------------------------------------- 1723 @Test(groups={"tck"}, dataProvider="plusTime") 1724 public void test_minusNanos(ZonedDateTime base, long amount, ZonedDateTime expected) { 1725 assertEquals(base.minusNanos(-amount * 3600_000_000_000L), expected); 1726 } 1727 1728 @Test(groups={"tck"}) 1729 public void test_minusNanos_nanos() { 1730 LocalDateTime ldt = LocalDateTime.of(2008, 6, 30, 23, 30, 59, 0); 1731 ZonedDateTime base = ZonedDateTime.of(ldt, ZONE_0100); 1732 ZonedDateTime test = base.minusNanos(1); 1733 assertEquals(test, ZonedDateTime.of(ldt.minusNanos(1), ZONE_0100)); 1734 } 1735 1736 //----------------------------------------------------------------------- 1737 // periodUntil(Temporal,TemporalUnit) 1738 //----------------------------------------------------------------------- 1739 // TODO: more tests for period between two different zones 1740 // compare results to OffsetDateTime.periodUntil, especially wrt dates 1741 1742 @Test(groups={"tck"}, dataProvider="plusDays") 1743 public void test_periodUntil_days(ZonedDateTime base, long expected, ZonedDateTime end) { 1744 assertEquals(base.periodUntil(end, DAYS), expected); 1745 } 1746 1747 @Test(groups={"tck"}, dataProvider="plusTime") 1748 public void test_periodUntil_hours(ZonedDateTime base, long expected, ZonedDateTime end) { 1749 assertEquals(base.periodUntil(end, HOURS), expected); 1750 } 1751 1752 @Test(groups={"tck"}, dataProvider="plusTime") 1753 public void test_periodUntil_minutes(ZonedDateTime base, long expected, ZonedDateTime end) { 1754 assertEquals(base.periodUntil(end, MINUTES), expected * 60); 1755 } 1756 1757 @Test(groups={"tck"}, dataProvider="plusTime") 1758 public void test_periodUntil_seconds(ZonedDateTime base, long expected, ZonedDateTime end) { 1759 assertEquals(base.periodUntil(end, SECONDS), expected * 3600); 1760 } 1761 1762 @Test(groups={"tck"}, dataProvider="plusTime") 1763 public void test_periodUntil_nanos(ZonedDateTime base, long expected, ZonedDateTime end) { 1764 assertEquals(base.periodUntil(end, NANOS), expected * 3600_000_000_000L); 1765 } 1766 1767 @Test(groups={"tck"}) 1768 public void test_periodUntil_parisLondon() { 1769 ZonedDateTime midnightLondon = LocalDate.of(2012, 6, 28).atStartOfDay(ZONE_LONDON); 1770 ZonedDateTime midnightParis1 = LocalDate.of(2012, 6, 29).atStartOfDay(ZONE_PARIS); 1771 ZonedDateTime oneAm1 = LocalDateTime.of(2012, 6, 29, 1, 0).atZone(ZONE_PARIS); 1772 ZonedDateTime midnightParis2 = LocalDate.of(2012, 6, 30).atStartOfDay(ZONE_PARIS); 1773 1774 assertEquals(midnightLondon.periodUntil(midnightParis1, HOURS), 23); 1775 assertEquals(midnightLondon.periodUntil(oneAm1, HOURS), 24); 1776 assertEquals(midnightLondon.periodUntil(midnightParis2, HOURS), 23 + 24); 1777 1778 assertEquals(midnightLondon.periodUntil(midnightParis1, DAYS), 0); 1779 assertEquals(midnightLondon.periodUntil(oneAm1, DAYS), 1); 1780 assertEquals(midnightLondon.periodUntil(midnightParis2, DAYS), 1); 1781 } 1782 1783 @Test(groups={"tck"}) 1784 public void test_periodUntil_gap() { 1785 ZonedDateTime before = TEST_PARIS_GAP_2008_03_30_02_30.withHour(0).withMinute(0).atZone(ZONE_PARIS); 1786 ZonedDateTime after = TEST_PARIS_GAP_2008_03_30_02_30.withHour(0).withMinute(0).plusDays(1).atZone(ZONE_PARIS); 1787 1788 assertEquals(before.periodUntil(after, HOURS), 23); 1789 assertEquals(before.periodUntil(after, DAYS), 1); 1790 } 1791 1792 @Test(groups={"tck"}) 1793 public void test_periodUntil_overlap() { 1794 ZonedDateTime before = TEST_PARIS_OVERLAP_2008_10_26_02_30.withHour(0).withMinute(0).atZone(ZONE_PARIS); 1795 ZonedDateTime after = TEST_PARIS_OVERLAP_2008_10_26_02_30.withHour(0).withMinute(0).plusDays(1).atZone(ZONE_PARIS); 1796 1797 assertEquals(before.periodUntil(after, HOURS), 25); 1798 assertEquals(before.periodUntil(after, DAYS), 1); 1799 } 1800 1801 @Test(groups={"tck"}, expectedExceptions=DateTimeException.class) 1802 public void test_periodUntil_differentType() { 1803 TEST_DATE_TIME_PARIS.periodUntil(TEST_LOCAL_2008_06_30_11_30_59_500, DAYS); 1804 } 1805 1806 @Test(groups={"tck"}, expectedExceptions=NullPointerException.class) 1807 public void test_periodUntil_nullTemporal() { 1808 TEST_DATE_TIME_PARIS.periodUntil(null, DAYS); 1809 } 1810 1811 @Test(groups={"tck"}, expectedExceptions=NullPointerException.class) 1812 public void test_periodUntil_nullUnit() { 1813 TEST_DATE_TIME_PARIS.periodUntil(TEST_DATE_TIME_PARIS, null); 1814 } 1815 1816 //----------------------------------------------------------------------- 1817 // toOffsetDateTime() 1818 //----------------------------------------------------------------------- 1819 @Test(groups={"tck"}) 1820 public void test_toOffsetDateTime() { 1821 assertEquals(TEST_DATE_TIME.toOffsetDateTime(), OffsetDateTime.of(TEST_DATE_TIME.getDateTime(), TEST_DATE_TIME.getOffset())); 1822 } 1823 1824 //----------------------------------------------------------------------- 1825 // toInstant() 1826 //----------------------------------------------------------------------- 1827 @DataProvider(name="toInstant") 1828 Object[][] data_toInstant() { 1829 return new Object[][] { 1830 {LocalDateTime.of(1970, 1, 1, 0, 0, 0, 0), 0L, 0}, 1831 {LocalDateTime.of(1970, 1, 1, 0, 0, 0, 1), 0L, 1}, 1832 {LocalDateTime.of(1970, 1, 1, 0, 0, 0, 999_999_999), 0L, 999_999_999}, 1833 {LocalDateTime.of(1970, 1, 1, 0, 0, 1, 0), 1L, 0}, 1834 {LocalDateTime.of(1970, 1, 1, 0, 0, 1, 1), 1L, 1}, 1835 {LocalDateTime.of(1969, 12, 31, 23, 59, 59, 999999999), -1L, 999_999_999}, 1836 {LocalDateTime.of(1970, 1, 2, 0, 0), 24L * 60L * 60L, 0}, 1837 {LocalDateTime.of(1969, 12, 31, 0, 0), -24L * 60L * 60L, 0}, 1838 }; 1839 } 1840 1841 @Test(groups={"tck"}, dataProvider="toInstant") 1842 public void test_toInstant_UTC(LocalDateTime ldt, long expectedEpSec, int expectedNos) { 1843 ZonedDateTime dt = ldt.atZone(ZoneOffset.UTC); 1844 Instant test = dt.toInstant(); 1845 assertEquals(test.getEpochSecond(), expectedEpSec); 1846 assertEquals(test.getNano(), expectedNos); 1847 } 1848 1849 @Test(groups={"tck"}, dataProvider="toInstant") 1850 public void test_toInstant_P0100(LocalDateTime ldt, long expectedEpSec, int expectedNos) { 1851 ZonedDateTime dt = ldt.atZone(ZONE_0100); 1852 Instant test = dt.toInstant(); 1853 assertEquals(test.getEpochSecond(), expectedEpSec - 3600); 1854 assertEquals(test.getNano(), expectedNos); 1855 } 1856 1857 @Test(groups={"tck"}, dataProvider="toInstant") 1858 public void test_toInstant_M0100(LocalDateTime ldt, long expectedEpSec, int expectedNos) { 1859 ZonedDateTime dt = ldt.atZone(ZONE_M0100); 1860 Instant test = dt.toInstant(); 1861 assertEquals(test.getEpochSecond(), expectedEpSec + 3600); 1862 assertEquals(test.getNano(), expectedNos); 1863 } 1864 1865 //----------------------------------------------------------------------- 1866 // toEpochSecond() 1867 //----------------------------------------------------------------------- 1868 @Test(groups={"tck"}) 1869 public void test_toEpochSecond_afterEpoch() { 1870 LocalDateTime ldt = LocalDateTime.of(1970, 1, 1, 0, 0).plusHours(1); 1871 for (int i = 0; i < 100000; i++) { 1872 ZonedDateTime a = ZonedDateTime.of(ldt, ZONE_PARIS); 1873 assertEquals(a.toEpochSecond(), i); 1874 ldt = ldt.plusSeconds(1); 1875 } 1876 } 1877 1878 @Test(groups={"tck"}) 1879 public void test_toEpochSecond_beforeEpoch() { 1880 LocalDateTime ldt = LocalDateTime.of(1970, 1, 1, 0, 0).plusHours(1); 1881 for (int i = 0; i < 100000; i++) { 1882 ZonedDateTime a = ZonedDateTime.of(ldt, ZONE_PARIS); 1883 assertEquals(a.toEpochSecond(), -i); 1884 ldt = ldt.minusSeconds(1); 1885 } 1886 } 1887 1888 @Test(groups={"tck"}, dataProvider="toInstant") 1889 public void test_toEpochSecond_UTC(LocalDateTime ldt, long expectedEpSec, int expectedNos) { 1890 ZonedDateTime dt = ldt.atZone(ZoneOffset.UTC); 1891 assertEquals(dt.toEpochSecond(), expectedEpSec); 1892 } 1893 1894 @Test(groups={"tck"}, dataProvider="toInstant") 1895 public void test_toEpochSecond_P0100(LocalDateTime ldt, long expectedEpSec, int expectedNos) { 1896 ZonedDateTime dt = ldt.atZone(ZONE_0100); 1897 assertEquals(dt.toEpochSecond(), expectedEpSec - 3600); 1898 } 1899 1900 @Test(groups={"tck"}, dataProvider="toInstant") 1901 public void test_toEpochSecond_M0100(LocalDateTime ldt, long expectedEpSec, int expectedNos) { 1902 ZonedDateTime dt = ldt.atZone(ZONE_M0100); 1903 assertEquals(dt.toEpochSecond(), expectedEpSec + 3600); 1904 } 1905 1906 //----------------------------------------------------------------------- 1907 // compareTo() 1908 //----------------------------------------------------------------------- 1909 @Test(groups={"tck"}) 1910 public void test_compareTo_time1() { 1911 ZonedDateTime a = ZonedDateTime.of(LocalDateTime.of(2008, 6, 30, 11, 30, 39), ZONE_0100); 1912 ZonedDateTime b = ZonedDateTime.of(LocalDateTime.of(2008, 6, 30, 11, 30, 41), ZONE_0100); // a is before b due to time 1913 assertEquals(a.compareTo(b) < 0, true); 1914 assertEquals(b.compareTo(a) > 0, true); 1915 assertEquals(a.compareTo(a) == 0, true); 1916 assertEquals(b.compareTo(b) == 0, true); 1917 } 1918 1919 @Test(groups={"tck"}) 1920 public void test_compareTo_time2() { 1921 ZonedDateTime a = ZonedDateTime.of(LocalDateTime.of(2008, 6, 30, 11, 30, 40, 4), ZONE_0100); 1922 ZonedDateTime b = ZonedDateTime.of(LocalDateTime.of(2008, 6, 30, 11, 30, 40, 5), ZONE_0100); // a is before b due to time 1923 assertEquals(a.compareTo(b) < 0, true); 1924 assertEquals(b.compareTo(a) > 0, true); 1925 assertEquals(a.compareTo(a) == 0, true); 1926 assertEquals(b.compareTo(b) == 0, true); 1927 } 1928 1929 @Test(groups={"tck"}) 1930 public void test_compareTo_offset1() { 1931 ZonedDateTime a = ZonedDateTime.of(LocalDateTime.of(2008, 6, 30, 11, 30, 41), ZONE_0200); 1932 ZonedDateTime b = ZonedDateTime.of(LocalDateTime.of(2008, 6, 30, 11, 30, 39), ZONE_0100); // a is before b due to offset 1933 assertEquals(a.compareTo(b) < 0, true); 1934 assertEquals(b.compareTo(a) > 0, true); 1935 assertEquals(a.compareTo(a) == 0, true); 1936 assertEquals(b.compareTo(b) == 0, true); 1937 } 1938 1939 @Test(groups={"tck"}) 1940 public void test_compareTo_offset2() { 1941 ZonedDateTime a = ZonedDateTime.of(LocalDateTime.of(2008, 6, 30, 11, 30, 40, 5), ZoneId.of("UTC+01:01")); 1942 ZonedDateTime b = ZonedDateTime.of(LocalDateTime.of(2008, 6, 30, 11, 30, 40, 4), ZONE_0100); // a is before b due to offset 1943 assertEquals(a.compareTo(b) < 0, true); 1944 assertEquals(b.compareTo(a) > 0, true); 1945 assertEquals(a.compareTo(a) == 0, true); 1946 assertEquals(b.compareTo(b) == 0, true); 1947 } 1948 1949 @Test(groups={"tck"}) 1950 public void test_compareTo_both() { 1951 ZonedDateTime a = ZonedDateTime.of(LocalDateTime.of(2008, 6, 30, 11, 50), ZONE_0200); 1952 ZonedDateTime b = ZonedDateTime.of(LocalDateTime.of(2008, 6, 30, 11, 20), ZONE_0100); // a is before b on instant scale 1953 assertEquals(a.compareTo(b) < 0, true); 1954 assertEquals(b.compareTo(a) > 0, true); 1955 assertEquals(a.compareTo(a) == 0, true); 1956 assertEquals(b.compareTo(b) == 0, true); 1957 } 1958 1959 @Test(groups={"tck"}) 1960 public void test_compareTo_bothNanos() { 1961 ZonedDateTime a = ZonedDateTime.of(LocalDateTime.of(2008, 6, 30, 11, 20, 40, 5), ZONE_0200); 1962 ZonedDateTime b = ZonedDateTime.of(LocalDateTime.of(2008, 6, 30, 10, 20, 40, 6), ZONE_0100); // a is before b on instant scale 1963 assertEquals(a.compareTo(b) < 0, true); 1964 assertEquals(b.compareTo(a) > 0, true); 1965 assertEquals(a.compareTo(a) == 0, true); 1966 assertEquals(b.compareTo(b) == 0, true); 1967 } 1968 1969 @Test(groups={"tck"}) 1970 public void test_compareTo_hourDifference() { 1971 ZonedDateTime a = ZonedDateTime.of(LocalDateTime.of(2008, 6, 30, 10, 0), ZONE_0100); 1972 ZonedDateTime b = ZonedDateTime.of(LocalDateTime.of(2008, 6, 30, 11, 0), ZONE_0200); // a is before b despite being same time-line time 1973 assertEquals(a.compareTo(b) < 0, true); 1974 assertEquals(b.compareTo(a) > 0, true); 1975 assertEquals(a.compareTo(a) == 0, true); 1976 assertEquals(b.compareTo(b) == 0, true); 1977 } 1978 1979 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 1980 public void test_compareTo_null() { 1981 LocalDateTime ldt = LocalDateTime.of(2008, 6, 30, 23, 30, 59, 0); 1982 ZonedDateTime a = ZonedDateTime.of(ldt, ZONE_0100); 1983 a.compareTo(null); 1984 } 1985 1986 //----------------------------------------------------------------------- 1987 // isBefore() 1988 //----------------------------------------------------------------------- 1989 @DataProvider(name="IsBefore") 1990 Object[][] data_isBefore() { 1991 return new Object[][] { 1992 {11, 30, ZONE_0100, 11, 31, ZONE_0100, true}, // a is before b due to time 1993 {11, 30, ZONE_0200, 11, 30, ZONE_0100, true}, // a is before b due to offset 1994 {11, 30, ZONE_0200, 10, 30, ZONE_0100, false}, // a is equal b due to same instant 1995 }; 1996 } 1997 1998 @Test(dataProvider="IsBefore", groups={"tck"}) 1999 public void test_isBefore(int hour1, int minute1, ZoneId zone1, int hour2, int minute2, ZoneId zone2, boolean expected) { 2000 ZonedDateTime a = ZonedDateTime.of(LocalDateTime.of(2008, 6, 30, hour1, minute1), zone1); 2001 ZonedDateTime b = ZonedDateTime.of(LocalDateTime.of(2008, 6, 30, hour2, minute2), zone2); 2002 assertEquals(a.isBefore(b), expected); 2003 assertEquals(b.isBefore(a), false); 2004 assertEquals(a.isBefore(a), false); 2005 assertEquals(b.isBefore(b), false); 2006 } 2007 2008 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 2009 public void test_isBefore_null() { 2010 LocalDateTime ldt = LocalDateTime.of(2008, 6, 30, 23, 30, 59, 0); 2011 ZonedDateTime a = ZonedDateTime.of(ldt, ZONE_0100); 2012 a.isBefore(null); 2013 } 2014 2015 //----------------------------------------------------------------------- 2016 // isAfter() 2017 //----------------------------------------------------------------------- 2018 @DataProvider(name="IsAfter") 2019 Object[][] data_isAfter() { 2020 return new Object[][] { 2021 {11, 31, ZONE_0100, 11, 30, ZONE_0100, true}, // a is after b due to time 2022 {11, 30, ZONE_0100, 11, 30, ZONE_0200, true}, // a is after b due to offset 2023 {11, 30, ZONE_0200, 10, 30, ZONE_0100, false}, // a is equal b due to same instant 2024 }; 2025 } 2026 2027 @Test(dataProvider="IsAfter", groups={"tck"}) 2028 public void test_isAfter(int hour1, int minute1, ZoneId zone1, int hour2, int minute2, ZoneId zone2, boolean expected) { 2029 ZonedDateTime a = ZonedDateTime.of(LocalDateTime.of(2008, 6, 30, hour1, minute1), zone1); 2030 ZonedDateTime b = ZonedDateTime.of(LocalDateTime.of(2008, 6, 30, hour2, minute2), zone2); 2031 assertEquals(a.isAfter(b), expected); 2032 assertEquals(b.isAfter(a), false); 2033 assertEquals(a.isAfter(a), false); 2034 assertEquals(b.isAfter(b), false); 2035 } 2036 2037 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 2038 public void test_isAfter_null() { 2039 LocalDateTime ldt = LocalDateTime.of(2008, 6, 30, 23, 30, 59, 0); 2040 ZonedDateTime a = ZonedDateTime.of(ldt, ZONE_0100); 2041 a.isAfter(null); 2042 } 2043 2044 //----------------------------------------------------------------------- 2045 // equals() / hashCode() 2046 //----------------------------------------------------------------------- 2047 @Test(dataProvider="sampleTimes", groups={"tck"}) 2048 public void test_equals_true(int y, int o, int d, int h, int m, int s, int n, ZoneId ignored) { 2049 ZonedDateTime a = ZonedDateTime.of(dateTime(y, o, d, h, m, s, n), ZONE_0100); 2050 ZonedDateTime b = ZonedDateTime.of(dateTime(y, o, d, h, m, s, n), ZONE_0100); 2051 assertEquals(a.equals(b), true); 2052 assertEquals(a.hashCode() == b.hashCode(), true); 2053 } 2054 @Test(dataProvider="sampleTimes", groups={"tck"}) 2055 public void test_equals_false_year_differs(int y, int o, int d, int h, int m, int s, int n, ZoneId ignored) { 2056 ZonedDateTime a = ZonedDateTime.of(dateTime(y, o, d, h, m, s, n), ZONE_0100); 2057 ZonedDateTime b = ZonedDateTime.of(dateTime(y + 1, o, d, h, m, s, n), ZONE_0100); 2058 assertEquals(a.equals(b), false); 2059 } 2060 @Test(dataProvider="sampleTimes", groups={"tck"}) 2061 public void test_equals_false_hour_differs(int y, int o, int d, int h, int m, int s, int n, ZoneId ignored) { 2062 h = (h == 23 ? 22 : h); 2063 ZonedDateTime a = ZonedDateTime.of(dateTime(y, o, d, h, m, s, n), ZONE_0100); 2064 ZonedDateTime b = ZonedDateTime.of(dateTime(y, o, d, h + 1, m, s, n), ZONE_0100); 2065 assertEquals(a.equals(b), false); 2066 } 2067 @Test(dataProvider="sampleTimes", groups={"tck"}) 2068 public void test_equals_false_minute_differs(int y, int o, int d, int h, int m, int s, int n, ZoneId ignored) { 2069 m = (m == 59 ? 58 : m); 2070 ZonedDateTime a = ZonedDateTime.of(dateTime(y, o, d, h, m, s, n), ZONE_0100); 2071 ZonedDateTime b = ZonedDateTime.of(dateTime(y, o, d, h, m + 1, s, n), ZONE_0100); 2072 assertEquals(a.equals(b), false); 2073 } 2074 @Test(dataProvider="sampleTimes", groups={"tck"}) 2075 public void test_equals_false_second_differs(int y, int o, int d, int h, int m, int s, int n, ZoneId ignored) { 2076 s = (s == 59 ? 58 : s); 2077 ZonedDateTime a = ZonedDateTime.of(dateTime(y, o, d, h, m, s, n), ZONE_0100); 2078 ZonedDateTime b = ZonedDateTime.of(dateTime(y, o, d, h, m, s + 1, n), ZONE_0100); 2079 assertEquals(a.equals(b), false); 2080 } 2081 @Test(dataProvider="sampleTimes", groups={"tck"}) 2082 public void test_equals_false_nano_differs(int y, int o, int d, int h, int m, int s, int n, ZoneId ignored) { 2083 n = (n == 999999999 ? 999999998 : n); 2084 ZonedDateTime a = ZonedDateTime.of(dateTime(y, o, d, h, m, s, n), ZONE_0100); 2085 ZonedDateTime b = ZonedDateTime.of(dateTime(y, o, d, h, m, s, n + 1), ZONE_0100); 2086 assertEquals(a.equals(b), false); 2087 } 2088 @Test(dataProvider="sampleTimes", groups={"tck"}) 2089 public void test_equals_false_offset_differs(int y, int o, int d, int h, int m, int s, int n, ZoneId ignored) { 2090 ZonedDateTime a = ZonedDateTime.of(dateTime(y, o, d, h, m, s, n), ZONE_0100); 2091 ZonedDateTime b = ZonedDateTime.of(dateTime(y, o, d, h, m, s, n), ZONE_0200); 2092 assertEquals(a.equals(b), false); 2093 } 2094 2095 @Test(groups={"tck"}) 2096 public void test_equals_itself_true() { 2097 assertEquals(TEST_DATE_TIME.equals(TEST_DATE_TIME), true); 2098 } 2099 2100 @Test(groups={"tck"}) 2101 public void test_equals_string_false() { 2102 assertEquals(TEST_DATE_TIME.equals("2007-07-15"), false); 2103 } 2104 2105 //----------------------------------------------------------------------- 2106 // toString() 2107 //----------------------------------------------------------------------- 2108 @DataProvider(name="sampleToString") 2109 Object[][] provider_sampleToString() { 2110 return new Object[][] { 2111 {2008, 6, 30, 11, 30, 59, 0, "Z", "2008-06-30T11:30:59Z"}, 2112 {2008, 6, 30, 11, 30, 59, 0, "+01:00", "2008-06-30T11:30:59+01:00"}, 2113 {2008, 6, 30, 11, 30, 59, 999000000, "Z", "2008-06-30T11:30:59.999Z"}, 2114 {2008, 6, 30, 11, 30, 59, 999000000, "+01:00", "2008-06-30T11:30:59.999+01:00"}, 2115 {2008, 6, 30, 11, 30, 59, 999000, "Z", "2008-06-30T11:30:59.000999Z"}, 2116 {2008, 6, 30, 11, 30, 59, 999000, "+01:00", "2008-06-30T11:30:59.000999+01:00"}, 2117 {2008, 6, 30, 11, 30, 59, 999, "Z", "2008-06-30T11:30:59.000000999Z"}, 2118 {2008, 6, 30, 11, 30, 59, 999, "+01:00", "2008-06-30T11:30:59.000000999+01:00"}, 2119 2120 {2008, 6, 30, 11, 30, 59, 999, "Europe/London", "2008-06-30T11:30:59.000000999+01:00[Europe/London]"}, 2121 {2008, 6, 30, 11, 30, 59, 999, "Europe/Paris", "2008-06-30T11:30:59.000000999+02:00[Europe/Paris]"}, 2122 }; 2123 } 2124 2125 @Test(dataProvider="sampleToString", groups={"tck"}) 2126 public void test_toString(int y, int o, int d, int h, int m, int s, int n, String zoneId, String expected) { 2127 ZonedDateTime t = ZonedDateTime.of(dateTime(y, o, d, h, m, s, n), ZoneId.of(zoneId)); 2128 String str = t.toString(); 2129 assertEquals(str, expected); 2130 } 2131 2132 //----------------------------------------------------------------------- 2133 // toString(DateTimeFormatter) 2134 //----------------------------------------------------------------------- 2135 @Test(groups={"tck"}) 2136 public void test_toString_formatter() { 2137 DateTimeFormatter f = DateTimeFormatters.pattern("y M d H m s"); 2138 String t = ZonedDateTime.of(dateTime(2010, 12, 3, 11, 30), ZONE_PARIS).toString(f); 2139 assertEquals(t, "2010 12 3 11 30 0"); 2140 } 2141 2142 @Test(expectedExceptions=NullPointerException.class, groups={"tck"}) 2143 public void test_toString_formatter_null() { 2144 ZonedDateTime.of(dateTime(2010, 12, 3, 11, 30), ZONE_PARIS).toString(null); 2145 } 2146 2147 //------------------------------------------------------------------------- 2148 private static LocalDateTime dateTime( 2149 int year, int month, int dayOfMonth, 2150 int hour, int minute) { 2151 return LocalDateTime.of(year, month, dayOfMonth, hour, minute); 2152 } 2153 2154 private static LocalDateTime dateTime( 2155 int year, int month, int dayOfMonth, 2156 int hour, int minute, int second, int nanoOfSecond) { 2157 return LocalDateTime.of(year, month, dayOfMonth, hour, minute, second, nanoOfSecond); 2158 } 2159 2160 private static ZonedDateTime dateTime( 2161 int year, int month, int dayOfMonth, 2162 int hour, int minute, int second, int nanoOfSecond, ZoneOffset offset, ZoneId zoneId) { 2163 return ZonedDateTime.ofStrict(LocalDateTime.of(year, month, dayOfMonth, hour, minute, second, nanoOfSecond), offset, zoneId); 2164 } 2165 2166 }