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 test.java.time; 61 62 import static org.testng.Assert.assertEquals; 63 import static org.testng.Assert.assertSame; 64 import static org.testng.Assert.assertTrue; 65 66 import java.time.LocalDate; 67 import java.time.LocalDateTime; 68 import java.time.LocalTime; 69 import java.time.Period; 70 import java.time.temporal.ChronoUnit; 71 72 import org.testng.annotations.DataProvider; 73 import org.testng.annotations.Test; 74 75 /** 76 * Test LocalDateTime. 77 */ 78 @Test 79 public class TestLocalDateTime extends AbstractTest { 80 81 private LocalDateTime TEST_2007_07_15_12_30_40_987654321 = LocalDateTime.of(2007, 7, 15, 12, 30, 40, 987654321); 82 83 //----------------------------------------------------------------------- 84 @Test test_immutable()85 public void test_immutable() { 86 assertImmutable(LocalDateTime.class); 87 } 88 89 //----------------------------------------------------------------------- 90 @DataProvider(name="sampleDates") provider_sampleDates()91 Object[][] provider_sampleDates() { 92 return new Object[][] { 93 {2008, 7, 5}, 94 {2007, 7, 5}, 95 {2006, 7, 5}, 96 {2005, 7, 5}, 97 {2004, 1, 1}, 98 {-1, 1, 2}, 99 }; 100 } 101 102 @DataProvider(name="sampleTimes") provider_sampleTimes()103 Object[][] provider_sampleTimes() { 104 return new Object[][] { 105 {0, 0, 0, 0}, 106 {0, 0, 0, 1}, 107 {0, 0, 1, 0}, 108 {0, 0, 1, 1}, 109 {0, 1, 0, 0}, 110 {0, 1, 0, 1}, 111 {0, 1, 1, 0}, 112 {0, 1, 1, 1}, 113 {1, 0, 0, 0}, 114 {1, 0, 0, 1}, 115 {1, 0, 1, 0}, 116 {1, 0, 1, 1}, 117 {1, 1, 0, 0}, 118 {1, 1, 0, 1}, 119 {1, 1, 1, 0}, 120 {1, 1, 1, 1}, 121 }; 122 } 123 124 @Test test_withYear_int_noChange()125 public void test_withYear_int_noChange() { 126 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.withYear(2007); 127 assertSame(t.toLocalDate(), TEST_2007_07_15_12_30_40_987654321.toLocalDate()); 128 assertSame(t.toLocalTime(), TEST_2007_07_15_12_30_40_987654321.toLocalTime()); 129 } 130 131 @Test test_withMonth_int_noChange()132 public void test_withMonth_int_noChange() { 133 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.withMonth(7); 134 assertSame(t.toLocalDate(), TEST_2007_07_15_12_30_40_987654321.toLocalDate()); 135 assertSame(t.toLocalTime(), TEST_2007_07_15_12_30_40_987654321.toLocalTime()); 136 } 137 138 @Test test_withDayOfMonth_noChange()139 public void test_withDayOfMonth_noChange() { 140 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.withDayOfMonth(15); 141 assertSame(t, TEST_2007_07_15_12_30_40_987654321); 142 } 143 144 @Test test_withDayOfYear_noChange()145 public void test_withDayOfYear_noChange() { 146 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.withDayOfYear(31 + 28 + 31 + 30 + 31 + 30 + 15); 147 assertSame(t, TEST_2007_07_15_12_30_40_987654321); 148 } 149 150 @Test test_withHour_noChange()151 public void test_withHour_noChange() { 152 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.withHour(12); 153 assertSame(t, TEST_2007_07_15_12_30_40_987654321); 154 } 155 156 @Test test_withHour_toMidnight()157 public void test_withHour_toMidnight() { 158 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.of(1, 0)).withHour(0); 159 assertSame(t.toLocalTime(), LocalTime.MIDNIGHT); 160 } 161 162 @Test test_withHour_toMidday()163 public void test_withHour_toMidday() { 164 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.of(1, 0)).withHour(12); 165 assertSame(t.toLocalTime(), LocalTime.NOON); 166 } 167 168 @Test test_withMinute_noChange()169 public void test_withMinute_noChange() { 170 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.withMinute(30); 171 assertSame(t, TEST_2007_07_15_12_30_40_987654321); 172 } 173 174 @Test test_withMinute_toMidnight()175 public void test_withMinute_toMidnight() { 176 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.of(0, 1)).withMinute(0); 177 assertSame(t.toLocalTime(), LocalTime.MIDNIGHT); 178 } 179 180 @Test test_withMinute_toMidday()181 public void test_withMinute_toMidday() { 182 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.of(12, 1)).withMinute(0); 183 assertSame(t.toLocalTime(), LocalTime.NOON); 184 } 185 186 @Test test_withSecond_noChange()187 public void test_withSecond_noChange() { 188 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.withSecond(40); 189 assertSame(t, TEST_2007_07_15_12_30_40_987654321); 190 } 191 192 @Test test_withSecond_toMidnight()193 public void test_withSecond_toMidnight() { 194 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.of(0, 0, 1)).withSecond(0); 195 assertSame(t.toLocalTime(), LocalTime.MIDNIGHT); 196 } 197 198 @Test test_withSecond_toMidday()199 public void test_withSecond_toMidday() { 200 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.of(12, 0, 1)).withSecond(0); 201 assertSame(t.toLocalTime(), LocalTime.NOON); 202 } 203 204 @Test test_withNanoOfSecond_noChange()205 public void test_withNanoOfSecond_noChange() { 206 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.withNano(987654321); 207 assertSame(t, TEST_2007_07_15_12_30_40_987654321); 208 } 209 210 @Test test_withNanoOfSecond_toMidnight()211 public void test_withNanoOfSecond_toMidnight() { 212 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.of(0, 0, 0, 1)).withNano(0); 213 assertSame(t.toLocalTime(), LocalTime.MIDNIGHT); 214 } 215 216 @Test test_withNanoOfSecond_toMidday()217 public void test_withNanoOfSecond_toMidday() { 218 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.of(12, 0, 0, 1)).withNano(0); 219 assertSame(t.toLocalTime(), LocalTime.NOON); 220 } 221 222 @Test test_plus_adjuster_zero()223 public void test_plus_adjuster_zero() { 224 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plus(Period.ZERO); 225 assertSame(t, TEST_2007_07_15_12_30_40_987654321); 226 } 227 228 @Test test_plus_Period_zero()229 public void test_plus_Period_zero() { 230 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plus(MockSimplePeriod.ZERO_DAYS); 231 assertSame(t, TEST_2007_07_15_12_30_40_987654321); 232 } 233 234 @Test test_plus_longPeriodUnit_zero()235 public void test_plus_longPeriodUnit_zero() { 236 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plus(0, ChronoUnit.DAYS); 237 assertSame(t, TEST_2007_07_15_12_30_40_987654321); 238 } 239 240 @Test test_plusYears_int_noChange()241 public void test_plusYears_int_noChange() { 242 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plusYears(0); 243 assertSame(TEST_2007_07_15_12_30_40_987654321, t); 244 } 245 246 @Test test_plusMonths_int_noChange()247 public void test_plusMonths_int_noChange() { 248 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plusMonths(0); 249 assertSame(t, TEST_2007_07_15_12_30_40_987654321); 250 } 251 252 @Test test_plusWeeks_noChange()253 public void test_plusWeeks_noChange() { 254 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plusWeeks(0); 255 assertSame(t, TEST_2007_07_15_12_30_40_987654321); 256 } 257 258 @Test test_plusDays_noChange()259 public void test_plusDays_noChange() { 260 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plusDays(0); 261 assertSame(t, TEST_2007_07_15_12_30_40_987654321); 262 } 263 264 @Test test_plusHours_noChange()265 public void test_plusHours_noChange() { 266 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plusHours(0); 267 assertSame(t, TEST_2007_07_15_12_30_40_987654321); 268 } 269 270 @Test test_plusHours_toMidnight()271 public void test_plusHours_toMidnight() { 272 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.of(23, 0)).plusHours(1); 273 assertSame(t.toLocalTime(), LocalTime.MIDNIGHT); 274 } 275 276 @Test test_plusHours_toMidday()277 public void test_plusHours_toMidday() { 278 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.of(11, 0)).plusHours(1); 279 assertSame(t.toLocalTime(), LocalTime.NOON); 280 } 281 282 @Test test_plusMinutes_noChange()283 public void test_plusMinutes_noChange() { 284 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plusMinutes(0); 285 assertSame(t, TEST_2007_07_15_12_30_40_987654321); 286 } 287 288 @Test test_plusMinutes_noChange_oneDay_same()289 public void test_plusMinutes_noChange_oneDay_same() { 290 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plusMinutes(24 * 60); 291 assertSame(t.toLocalTime(), TEST_2007_07_15_12_30_40_987654321.toLocalTime()); 292 } 293 294 @Test test_plusMinutes_toMidnight()295 public void test_plusMinutes_toMidnight() { 296 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.of(23, 59)).plusMinutes(1); 297 assertSame(t.toLocalTime(), LocalTime.MIDNIGHT); 298 } 299 300 @Test test_plusMinutes_toMidday()301 public void test_plusMinutes_toMidday() { 302 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.of(11, 59)).plusMinutes(1); 303 assertSame(t.toLocalTime(), LocalTime.NOON); 304 } 305 306 @Test test_plusSeconds_noChange()307 public void test_plusSeconds_noChange() { 308 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plusSeconds(0); 309 assertSame(t, TEST_2007_07_15_12_30_40_987654321); 310 } 311 312 @Test test_plusSeconds_noChange_oneDay_same()313 public void test_plusSeconds_noChange_oneDay_same() { 314 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plusSeconds(24 * 60 * 60); 315 assertSame(t.toLocalTime(), TEST_2007_07_15_12_30_40_987654321.toLocalTime()); 316 } 317 318 @Test test_plusSeconds_toMidnight()319 public void test_plusSeconds_toMidnight() { 320 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.of(23, 59, 59)).plusSeconds(1); 321 assertSame(t.toLocalTime(), LocalTime.MIDNIGHT); 322 } 323 324 @Test test_plusSeconds_toMidday()325 public void test_plusSeconds_toMidday() { 326 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.of(11, 59, 59)).plusSeconds(1); 327 assertSame(t.toLocalTime(), LocalTime.NOON); 328 } 329 330 @Test test_plusNanos_noChange()331 public void test_plusNanos_noChange() { 332 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plusNanos(0); 333 assertSame(t, TEST_2007_07_15_12_30_40_987654321); 334 } 335 336 @Test test_plusNanos_noChange_oneDay_same()337 public void test_plusNanos_noChange_oneDay_same() { 338 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plusNanos(24 * 60 * 60 * 1000000000L); 339 assertSame(t.toLocalTime(), TEST_2007_07_15_12_30_40_987654321.toLocalTime()); 340 } 341 342 @Test test_plusNanos_toMidnight()343 public void test_plusNanos_toMidnight() { 344 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.of(23, 59, 59, 999999999)).plusNanos(1); 345 assertSame(t.toLocalTime(), LocalTime.MIDNIGHT); 346 } 347 348 @Test test_plusNanos_toMidday()349 public void test_plusNanos_toMidday() { 350 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.of(11, 59, 59, 999999999)).plusNanos(1); 351 assertSame(t.toLocalTime(), LocalTime.NOON); 352 } 353 354 @Test test_minus_adjuster_zero()355 public void test_minus_adjuster_zero() { 356 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minus(Period.ZERO); 357 assertSame(t, TEST_2007_07_15_12_30_40_987654321); 358 } 359 360 @Test test_minus_Period_zero()361 public void test_minus_Period_zero() { 362 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minus(MockSimplePeriod.ZERO_DAYS); 363 assertSame(t, TEST_2007_07_15_12_30_40_987654321); 364 } 365 366 @Test test_minus_longPeriodUnit_zero()367 public void test_minus_longPeriodUnit_zero() { 368 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minus(0, ChronoUnit.DAYS); 369 assertSame(t, TEST_2007_07_15_12_30_40_987654321); 370 } 371 372 @Test test_minusYears_int_noChange()373 public void test_minusYears_int_noChange() { 374 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minusYears(0); 375 assertSame(t, TEST_2007_07_15_12_30_40_987654321); 376 } 377 378 @Test test_minusMonths_int_noChange()379 public void test_minusMonths_int_noChange() { 380 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minusMonths(0); 381 assertSame(t, TEST_2007_07_15_12_30_40_987654321); 382 } 383 384 @Test test_minusWeeks_noChange()385 public void test_minusWeeks_noChange() { 386 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minusWeeks(0); 387 assertSame(t, TEST_2007_07_15_12_30_40_987654321); 388 } 389 390 @Test test_minusDays_noChange()391 public void test_minusDays_noChange() { 392 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minusDays(0); 393 assertSame(t, TEST_2007_07_15_12_30_40_987654321); 394 } 395 396 @Test test_minusHours_noChange()397 public void test_minusHours_noChange() { 398 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minusHours(0); 399 assertSame(t, TEST_2007_07_15_12_30_40_987654321); 400 } 401 402 @Test test_minusHours_toMidnight()403 public void test_minusHours_toMidnight() { 404 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.of(1, 0)).minusHours(1); 405 assertSame(t.toLocalTime(), LocalTime.MIDNIGHT); 406 } 407 408 @Test test_minusHours_toMidday()409 public void test_minusHours_toMidday() { 410 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.of(13, 0)).minusHours(1); 411 assertSame(t.toLocalTime(), LocalTime.NOON); 412 } 413 414 @Test test_minusMinutes_noChange()415 public void test_minusMinutes_noChange() { 416 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minusMinutes(0); 417 assertSame(t, TEST_2007_07_15_12_30_40_987654321); 418 } 419 420 @Test test_minusMinutes_noChange_oneDay_same()421 public void test_minusMinutes_noChange_oneDay_same() { 422 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minusMinutes(24 * 60); 423 assertSame(t.toLocalTime(), TEST_2007_07_15_12_30_40_987654321.toLocalTime()); 424 } 425 426 @Test test_minusMinutes_toMidnight()427 public void test_minusMinutes_toMidnight() { 428 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.of(0, 1)).minusMinutes(1); 429 assertSame(t.toLocalTime(), LocalTime.MIDNIGHT); 430 } 431 432 @Test test_minusMinutes_toMidday()433 public void test_minusMinutes_toMidday() { 434 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.of(12, 1)).minusMinutes(1); 435 assertSame(t.toLocalTime(), LocalTime.NOON); 436 } 437 438 @Test test_minusSeconds_noChange()439 public void test_minusSeconds_noChange() { 440 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minusSeconds(0); 441 assertSame(t, TEST_2007_07_15_12_30_40_987654321); 442 } 443 444 @Test test_minusSeconds_noChange_oneDay()445 public void test_minusSeconds_noChange_oneDay() { 446 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minusSeconds(24 * 60 * 60); 447 assertEquals(t.toLocalDate(), TEST_2007_07_15_12_30_40_987654321.toLocalDate().minusDays(1)); 448 assertSame(t.toLocalTime(), TEST_2007_07_15_12_30_40_987654321.toLocalTime()); 449 } 450 451 @Test test_minusSeconds_toMidnight()452 public void test_minusSeconds_toMidnight() { 453 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.of(0, 0, 1)).minusSeconds(1); 454 assertSame(t.toLocalTime(), LocalTime.MIDNIGHT); 455 } 456 457 @Test test_minusSeconds_toMidday()458 public void test_minusSeconds_toMidday() { 459 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.of(12, 0, 1)).minusSeconds(1); 460 assertSame(t.toLocalTime(), LocalTime.NOON); 461 } 462 463 @Test test_minusNanos_noChange()464 public void test_minusNanos_noChange() { 465 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minusNanos(0); 466 assertSame(t, TEST_2007_07_15_12_30_40_987654321); 467 } 468 469 @Test test_minusNanos_noChange_oneDay()470 public void test_minusNanos_noChange_oneDay() { 471 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minusNanos(24 * 60 * 60 * 1000000000L); 472 assertEquals(t.toLocalDate(), TEST_2007_07_15_12_30_40_987654321.toLocalDate().minusDays(1)); 473 assertSame(t.toLocalTime(), TEST_2007_07_15_12_30_40_987654321.toLocalTime()); 474 } 475 476 @Test test_minusNanos_toMidnight()477 public void test_minusNanos_toMidnight() { 478 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.of(0, 0, 0, 1)).minusNanos(1); 479 assertSame(t.toLocalTime(), LocalTime.MIDNIGHT); 480 } 481 482 @Test test_minusNanos_toMidday()483 public void test_minusNanos_toMidday() { 484 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.of(12, 0, 0, 1)).minusNanos(1); 485 assertSame(t.toLocalTime(), LocalTime.NOON); 486 } 487 488 //----------------------------------------------------------------------- 489 // toLocalDate() 490 //----------------------------------------------------------------------- 491 @Test(dataProvider="sampleDates") test_getDate(int year, int month, int day)492 public void test_getDate(int year, int month, int day) { 493 LocalDate d = LocalDate.of(year, month, day); 494 LocalDateTime dt = LocalDateTime.of(d, LocalTime.MIDNIGHT); 495 assertSame(dt.toLocalDate(), d); 496 } 497 498 //----------------------------------------------------------------------- 499 // toLocalTime() 500 //----------------------------------------------------------------------- 501 @Test(dataProvider="sampleTimes") test_getTime(int h, int m, int s, int ns)502 public void test_getTime(int h, int m, int s, int ns) { 503 LocalTime t = LocalTime.of(h, m, s, ns); 504 LocalDateTime dt = LocalDateTime.of(LocalDate.of(2011, 7, 30), t); 505 assertSame(dt.toLocalTime(), t); 506 } 507 test_comparisons_LocalDateTime(LocalDate... localDates)508 void test_comparisons_LocalDateTime(LocalDate... localDates) { 509 test_comparisons_LocalDateTime( 510 localDates, 511 LocalTime.MIDNIGHT, 512 LocalTime.of(0, 0, 0, 999999999), 513 LocalTime.of(0, 0, 59, 0), 514 LocalTime.of(0, 0, 59, 999999999), 515 LocalTime.of(0, 59, 0, 0), 516 LocalTime.of(0, 59, 59, 999999999), 517 LocalTime.NOON, 518 LocalTime.of(12, 0, 0, 999999999), 519 LocalTime.of(12, 0, 59, 0), 520 LocalTime.of(12, 0, 59, 999999999), 521 LocalTime.of(12, 59, 0, 0), 522 LocalTime.of(12, 59, 59, 999999999), 523 LocalTime.of(23, 0, 0, 0), 524 LocalTime.of(23, 0, 0, 999999999), 525 LocalTime.of(23, 0, 59, 0), 526 LocalTime.of(23, 0, 59, 999999999), 527 LocalTime.of(23, 59, 0, 0), 528 LocalTime.of(23, 59, 59, 999999999) 529 ); 530 } 531 test_comparisons_LocalDateTime(LocalDate[] localDates, LocalTime... localTimes)532 void test_comparisons_LocalDateTime(LocalDate[] localDates, LocalTime... localTimes) { 533 LocalDateTime[] localDateTimes = new LocalDateTime[localDates.length * localTimes.length]; 534 int i = 0; 535 536 for (LocalDate localDate : localDates) { 537 for (LocalTime localTime : localTimes) { 538 localDateTimes[i++] = LocalDateTime.of(localDate, localTime); 539 } 540 } 541 542 doTest_comparisons_LocalDateTime(localDateTimes); 543 } 544 doTest_comparisons_LocalDateTime(LocalDateTime[] localDateTimes)545 void doTest_comparisons_LocalDateTime(LocalDateTime[] localDateTimes) { 546 for (int i = 0; i < localDateTimes.length; i++) { 547 LocalDateTime a = localDateTimes[i]; 548 for (int j = 0; j < localDateTimes.length; j++) { 549 LocalDateTime b = localDateTimes[j]; 550 if (i < j) { 551 assertTrue(a.compareTo(b) < 0, a + " <=> " + b); 552 assertEquals(a.isBefore(b), true, a + " <=> " + b); 553 assertEquals(a.isAfter(b), false, a + " <=> " + b); 554 assertEquals(a.equals(b), false, a + " <=> " + b); 555 } else if (i > j) { 556 assertTrue(a.compareTo(b) > 0, a + " <=> " + b); 557 assertEquals(a.isBefore(b), false, a + " <=> " + b); 558 assertEquals(a.isAfter(b), true, a + " <=> " + b); 559 assertEquals(a.equals(b), false, a + " <=> " + b); 560 } else { 561 assertEquals(a.compareTo(b), 0, a + " <=> " + b); 562 assertEquals(a.isBefore(b), false, a + " <=> " + b); 563 assertEquals(a.isAfter(b), false, a + " <=> " + b); 564 assertEquals(a.equals(b), true, a + " <=> " + b); 565 } 566 } 567 } 568 } 569 570 } 571