71 import static java.time.temporal.ChronoField.DAY_OF_YEAR;
72 import static java.time.temporal.ChronoField.EPOCH_DAY;
73 import static java.time.temporal.ChronoField.EPOCH_MONTH;
74 import static java.time.temporal.ChronoField.ERA;
75 import static java.time.temporal.ChronoField.HOUR_OF_AMPM;
76 import static java.time.temporal.ChronoField.HOUR_OF_DAY;
77 import static java.time.temporal.ChronoField.MICRO_OF_DAY;
78 import static java.time.temporal.ChronoField.MICRO_OF_SECOND;
79 import static java.time.temporal.ChronoField.MILLI_OF_DAY;
80 import static java.time.temporal.ChronoField.MILLI_OF_SECOND;
81 import static java.time.temporal.ChronoField.MINUTE_OF_DAY;
82 import static java.time.temporal.ChronoField.MINUTE_OF_HOUR;
83 import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
84 import static java.time.temporal.ChronoField.NANO_OF_DAY;
85 import static java.time.temporal.ChronoField.NANO_OF_SECOND;
86 import static java.time.temporal.ChronoField.SECOND_OF_DAY;
87 import static java.time.temporal.ChronoField.SECOND_OF_MINUTE;
88 import static java.time.temporal.ChronoField.YEAR;
89 import static java.time.temporal.ChronoField.YEAR_OF_ERA;
90 import static java.time.temporal.ChronoUnit.DAYS;
91 import static java.time.temporal.ChronoUnit.NANOS;
92 import static java.time.temporal.ChronoUnit.SECONDS;
93 import static org.testng.Assert.assertEquals;
94 import static org.testng.Assert.assertFalse;
95 import static org.testng.Assert.assertSame;
96 import static org.testng.Assert.assertTrue;
97
98 import java.io.ByteArrayOutputStream;
99 import java.io.DataOutputStream;
100 import java.io.IOException;
101 import java.util.ArrayList;
102 import java.util.Arrays;
103 import java.util.Iterator;
104 import java.util.List;
105
106 import java.time.Clock;
107 import java.time.DateTimeException;
108 import java.time.DayOfWeek;
109 import java.time.Instant;
110 import java.time.LocalDate;
111 import java.time.LocalDateTime;
112 import java.time.LocalTime;
113 import java.time.Month;
114 import java.time.Period;
115 import java.time.ZoneId;
116 import java.time.ZoneOffset;
117 import java.time.ZonedDateTime;
118 import java.time.format.DateTimeFormatter;
119 import java.time.format.DateTimeFormatters;
120 import java.time.format.DateTimeParseException;
121 import java.time.temporal.ChronoField;
122 import java.time.temporal.ChronoUnit;
123 import java.time.temporal.ISOChrono;
124 import java.time.temporal.JulianFields;
125 import java.time.temporal.OffsetDateTime;
126 import java.time.temporal.Queries;
127 import java.time.temporal.Temporal;
128 import java.time.temporal.TemporalAccessor;
129 import java.time.temporal.TemporalAdder;
130 import java.time.temporal.TemporalAdjuster;
131 import java.time.temporal.TemporalField;
132 import java.time.temporal.TemporalQuery;
133 import java.time.temporal.TemporalSubtractor;
134 import java.time.temporal.TemporalUnit;
135 import java.time.temporal.Year;
136
137 import org.testng.annotations.BeforeMethod;
138 import org.testng.annotations.DataProvider;
139 import org.testng.annotations.Test;
140 import test.java.time.MockSimplePeriod;
141
142 /**
143 * Test LocalDateTime.
144 */
145 @Test
146 public class TCKLocalDateTime extends AbstractDateTimeTest {
147
148 private static final ZoneOffset OFFSET_PONE = ZoneOffset.ofHours(1);
149 private static final ZoneOffset OFFSET_PTWO = ZoneOffset.ofHours(2);
150 private static final ZoneOffset OFFSET_MTWO = ZoneOffset.ofHours(-2);
151 private static final ZoneId ZONE_PARIS = ZoneId.of("Europe/Paris");
152 private static final ZoneId ZONE_GAZA = ZoneId.of("Asia/Gaza");
153
154 private LocalDateTime TEST_2007_07_15_12_30_40_987654321 = LocalDateTime.of(2007, 7, 15, 12, 30, 40, 987654321);
155 private LocalDateTime MAX_DATE_TIME;
156 private LocalDateTime MIN_DATE_TIME;
157 private Instant MAX_INSTANT;
158 private Instant MIN_INSTANT;
159
160 @BeforeMethod(groups={"implementation","tck"})
263 //-----------------------------------------------------------------------
264 // constants
265 //-----------------------------------------------------------------------
266 @Test
267 public void constant_MIN() {
268 check(LocalDateTime.MIN, Year.MIN_VALUE, 1, 1, 0, 0, 0, 0);
269 }
270
271 @Test
272 public void constant_MAX() {
273 check(LocalDateTime.MAX, Year.MAX_VALUE, 12, 31, 23, 59, 59, 999999999);
274 }
275
276 //-----------------------------------------------------------------------
277 // now()
278 //-----------------------------------------------------------------------
279 @Test(timeOut=30000, groups={"tck"}) // TODO: remove when time zone loading is faster
280 public void now() {
281 LocalDateTime expected = LocalDateTime.now(Clock.systemDefaultZone());
282 LocalDateTime test = LocalDateTime.now();
283 long diff = Math.abs(test.getTime().toNanoOfDay() - expected.getTime().toNanoOfDay());
284 if (diff >= 100000000) {
285 // may be date change
286 expected = LocalDateTime.now(Clock.systemDefaultZone());
287 test = LocalDateTime.now();
288 diff = Math.abs(test.getTime().toNanoOfDay() - expected.getTime().toNanoOfDay());
289 }
290 assertTrue(diff < 100000000); // less than 0.1 secs
291 }
292
293 //-----------------------------------------------------------------------
294 // now(ZoneId)
295 //-----------------------------------------------------------------------
296 @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
297 public void now_ZoneId_nullZoneId() {
298 LocalDateTime.now((ZoneId) null);
299 }
300
301 @Test(groups={"tck"})
302 public void now_ZoneId() {
303 ZoneId zone = ZoneId.of("UTC+01:02:03");
304 LocalDateTime expected = LocalDateTime.now(Clock.system(zone));
305 LocalDateTime test = LocalDateTime.now(zone);
306 for (int i = 0; i < 100; i++) {
307 if (expected.equals(test)) {
308 return;
347 assertEquals(test.getMonth(), Month.JANUARY);
348 assertEquals(test.getDayOfMonth(), (i < 24 * 60 * 60) ? 1 : 2);
349 assertEquals(test.getHour(), (i / (60 * 60)) % 24);
350 assertEquals(test.getMinute(), (i / 60) % 60);
351 assertEquals(test.getSecond(), i % 60);
352 assertEquals(test.getNano(), 123456789);
353 }
354 }
355
356 @Test(groups={"tck"})
357 public void now_Clock_allSecsInDay_beforeEpoch() {
358 LocalTime expected = LocalTime.MIDNIGHT.plusNanos(123456789L);
359 for (int i =-1; i >= -(24 * 60 * 60); i--) {
360 Instant instant = Instant.ofEpochSecond(i).plusNanos(123456789L);
361 Clock clock = Clock.fixed(instant, ZoneOffset.UTC);
362 LocalDateTime test = LocalDateTime.now(clock);
363 assertEquals(test.getYear(), 1969);
364 assertEquals(test.getMonth(), Month.DECEMBER);
365 assertEquals(test.getDayOfMonth(), 31);
366 expected = expected.minusSeconds(1);
367 assertEquals(test.getTime(), expected);
368 }
369 }
370
371 //-----------------------------------------------------------------------
372 @Test(groups={"tck"})
373 public void now_Clock_maxYear() {
374 Clock clock = Clock.fixed(MAX_INSTANT, ZoneOffset.UTC);
375 LocalDateTime test = LocalDateTime.now(clock);
376 assertEquals(test, MAX_DATE_TIME);
377 }
378
379 @Test(expectedExceptions=DateTimeException.class, groups={"tck"})
380 public void now_Clock_tooBig() {
381 Clock clock = Clock.fixed(MAX_INSTANT.plusSeconds(24 * 60 * 60), ZoneOffset.UTC);
382 LocalDateTime.now(clock);
383 }
384
385 @Test(groups={"tck"})
386 public void now_Clock_minYear() {
387 Clock clock = Clock.fixed(MIN_INSTANT, ZoneOffset.UTC);
898 @Test(expectedExceptions=DateTimeParseException.class, groups={"tck"})
899 public void factory_parse_illegalValue() {
900 LocalDateTime.parse("2008-06-32T11:15");
901 }
902
903 @Test(expectedExceptions=DateTimeParseException.class, groups={"tck"})
904 public void factory_parse_invalidValue() {
905 LocalDateTime.parse("2008-06-31T11:15");
906 }
907
908 @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
909 public void factory_parse_nullText() {
910 LocalDateTime.parse((String) null);
911 }
912
913 //-----------------------------------------------------------------------
914 // parse(DateTimeFormatter)
915 //-----------------------------------------------------------------------
916 @Test(groups={"tck"})
917 public void factory_parse_formatter() {
918 DateTimeFormatter f = DateTimeFormatters.pattern("y M d H m s");
919 LocalDateTime test = LocalDateTime.parse("2010 12 3 11 30 45", f);
920 assertEquals(test, LocalDateTime.of(2010, 12, 3, 11, 30, 45));
921 }
922
923 @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
924 public void factory_parse_formatter_nullText() {
925 DateTimeFormatter f = DateTimeFormatters.pattern("y M d H m s");
926 LocalDateTime.parse((String) null, f);
927 }
928
929 @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
930 public void factory_parse_formatter_nullFormatter() {
931 LocalDateTime.parse("ANY", null);
932 }
933
934 //-----------------------------------------------------------------------
935 // get(TemporalField)
936 //-----------------------------------------------------------------------
937 @Test
938 public void test_get_TemporalField() {
939 LocalDateTime test = LocalDateTime.of(2008, 6, 30, 12, 30, 40, 987654321);
940 assertEquals(test.get(ChronoField.YEAR), 2008);
941 assertEquals(test.get(ChronoField.MONTH_OF_YEAR), 6);
942 assertEquals(test.get(ChronoField.DAY_OF_MONTH), 30);
943 assertEquals(test.get(ChronoField.DAY_OF_WEEK), 1);
944 assertEquals(test.get(ChronoField.DAY_OF_YEAR), 182);
945
954 @Test
955 public void test_getLong_TemporalField() {
956 LocalDateTime test = LocalDateTime.of(2008, 6, 30, 12, 30, 40, 987654321);
957 assertEquals(test.getLong(ChronoField.YEAR), 2008);
958 assertEquals(test.getLong(ChronoField.MONTH_OF_YEAR), 6);
959 assertEquals(test.getLong(ChronoField.DAY_OF_MONTH), 30);
960 assertEquals(test.getLong(ChronoField.DAY_OF_WEEK), 1);
961 assertEquals(test.getLong(ChronoField.DAY_OF_YEAR), 182);
962
963 assertEquals(test.getLong(ChronoField.HOUR_OF_DAY), 12);
964 assertEquals(test.getLong(ChronoField.MINUTE_OF_HOUR), 30);
965 assertEquals(test.getLong(ChronoField.SECOND_OF_MINUTE), 40);
966 assertEquals(test.getLong(ChronoField.NANO_OF_SECOND), 987654321);
967 assertEquals(test.getLong(ChronoField.HOUR_OF_AMPM), 0);
968 assertEquals(test.getLong(ChronoField.AMPM_OF_DAY), 1);
969 }
970
971 //-----------------------------------------------------------------------
972 // query(TemporalQuery)
973 //-----------------------------------------------------------------------
974 @Test
975 public void test_query_chrono() {
976 assertEquals(TEST_2007_07_15_12_30_40_987654321.query(Queries.chrono()), ISOChrono.INSTANCE);
977 assertEquals(Queries.chrono().queryFrom(TEST_2007_07_15_12_30_40_987654321), ISOChrono.INSTANCE);
978 }
979
980 @Test
981 public void test_query_zoneId() {
982 assertEquals(TEST_2007_07_15_12_30_40_987654321.query(Queries.zoneId()), null);
983 assertEquals(Queries.zoneId().queryFrom(TEST_2007_07_15_12_30_40_987654321), null);
984 }
985
986 @Test
987 public void test_query_precision() {
988 assertEquals(TEST_2007_07_15_12_30_40_987654321.query(Queries.precision()), NANOS);
989 assertEquals(Queries.precision().queryFrom(TEST_2007_07_15_12_30_40_987654321), NANOS);
990 }
991
992 @Test
993 public void test_query_offset() {
994 assertEquals(TEST_2007_07_15_12_30_40_987654321.query(Queries.offset()), null);
995 assertEquals(Queries.offset().queryFrom(TEST_2007_07_15_12_30_40_987654321), null);
996 }
997
998 @Test
999 public void test_query_zone() {
1000 assertEquals(TEST_2007_07_15_12_30_40_987654321.query(Queries.zone()), null);
1001 assertEquals(Queries.zone().queryFrom(TEST_2007_07_15_12_30_40_987654321), null);
1002 }
1003
1004 @Test(expectedExceptions=NullPointerException.class)
1005 public void test_query_null() {
1006 TEST_2007_07_15_12_30_40_987654321.query(null);
1007 }
1008
1009 //-----------------------------------------------------------------------
1010 @DataProvider(name="sampleDates")
1011 Object[][] provider_sampleDates() {
1012 return new Object[][] {
1013 {2008, 7, 5},
1014 {2007, 7, 5},
1015 {2006, 7, 5},
1016 {2005, 7, 5},
1017 {2004, 1, 1},
1018 {-1, 1, 2},
1019 };
1020 }
1021
1048 public void test_get_dates(int y, int m, int d) {
1049 LocalDateTime a = LocalDateTime.of(y, m, d, 12, 30);
1050 assertEquals(a.getYear(), y);
1051 assertEquals(a.getMonth(), Month.of(m));
1052 assertEquals(a.getDayOfMonth(), d);
1053 }
1054
1055 @Test(dataProvider="sampleDates", groups={"tck"})
1056 public void test_getDOY(int y, int m, int d) {
1057 LocalDateTime a = LocalDateTime.of(y, m, d, 12 ,30);
1058 int total = 0;
1059 for (int i = 1; i < m; i++) {
1060 total += Month.of(i).length(isIsoLeap(y));
1061 }
1062 int doy = total + d;
1063 assertEquals(a.getDayOfYear(), doy);
1064 }
1065
1066 @Test(dataProvider="sampleTimes", groups={"tck"})
1067 public void test_get_times(int h, int m, int s, int ns) {
1068 LocalDateTime a = LocalDateTime.of(TEST_2007_07_15_12_30_40_987654321.getDate(), LocalTime.of(h, m, s, ns));
1069 assertEquals(a.getHour(), h);
1070 assertEquals(a.getMinute(), m);
1071 assertEquals(a.getSecond(), s);
1072 assertEquals(a.getNano(), ns);
1073 }
1074
1075 //-----------------------------------------------------------------------
1076 // getDayOfWeek()
1077 //-----------------------------------------------------------------------
1078 @Test(groups={"tck"})
1079 public void test_getDayOfWeek() {
1080 DayOfWeek dow = DayOfWeek.MONDAY;
1081 for (Month month : Month.values()) {
1082 int length = month.length(false);
1083 for (int i = 1; i <= length; i++) {
1084 LocalDateTime d = LocalDateTime.of(LocalDate.of(2007, month, i),
1085 TEST_2007_07_15_12_30_40_987654321.getTime());
1086 assertSame(d.getDayOfWeek(), dow);
1087 dow = dow.plus(1);
1088 }
1089 }
1090 }
1091
1092 //-----------------------------------------------------------------------
1093 // with()
1094 //-----------------------------------------------------------------------
1095 @Test(groups={"tck"})
1096 public void test_with_adjustment() {
1097 final LocalDateTime sample = LocalDateTime.of(2012, 3, 4, 23, 5);
1098 TemporalAdjuster adjuster = new TemporalAdjuster() {
1099 @Override
1100 public Temporal adjustInto(Temporal dateTime) {
1101 return sample;
1102 }
1103 };
1104 assertEquals(TEST_2007_07_15_12_30_40_987654321.with(adjuster), sample);
1105 }
1280 public void test_withNanoOfSecond_nanoTooHigh() {
1281 TEST_2007_07_15_12_30_40_987654321.withNano(1000000000);
1282 }
1283
1284 //-----------------------------------------------------------------------
1285 // truncatedTo(TemporalUnit)
1286 //-----------------------------------------------------------------------
1287 @Test(groups={"tck"})
1288 public void test_truncatedTo_normal() {
1289 assertEquals(TEST_2007_07_15_12_30_40_987654321.truncatedTo(NANOS), TEST_2007_07_15_12_30_40_987654321);
1290 assertEquals(TEST_2007_07_15_12_30_40_987654321.truncatedTo(SECONDS), TEST_2007_07_15_12_30_40_987654321.withNano(0));
1291 assertEquals(TEST_2007_07_15_12_30_40_987654321.truncatedTo(DAYS), TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT));
1292 }
1293
1294 @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
1295 public void test_truncatedTo_null() {
1296 TEST_2007_07_15_12_30_40_987654321.truncatedTo(null);
1297 }
1298
1299 //-----------------------------------------------------------------------
1300 // plus(adjuster)
1301 //-----------------------------------------------------------------------
1302 @Test(groups={"tck"})
1303 public void test_plus_adjuster() {
1304 Period p = Period.ofTime(0, 0, 62, 3);
1305 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plus(p);
1306 assertEquals(t, LocalDateTime.of(2007, 7, 15, 12, 31, 42, 987654324));
1307 }
1308
1309 @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
1310 public void test_plus_adjuster_null() {
1311 TEST_2007_07_15_12_30_40_987654321.plus((TemporalAdder) null);
1312 }
1313
1314 //-----------------------------------------------------------------------
1315 // plus(Period)
1316 //-----------------------------------------------------------------------
1317 @Test(groups={"tck"})
1318 public void test_plus_Period_positiveMonths() {
1319 MockSimplePeriod period = MockSimplePeriod.of(7, ChronoUnit.MONTHS);
1320 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plus(period);
1321 assertEquals(t, LocalDateTime.of(2008, 2, 15, 12, 30, 40, 987654321));
1322 }
1323
1324 @Test(groups={"tck"})
1325 public void test_plus_Period_negativeDays() {
1326 MockSimplePeriod period = MockSimplePeriod.of(-25, ChronoUnit.DAYS);
1327 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plus(period);
1328 assertEquals(t, LocalDateTime.of(2007, 6, 20, 12, 30, 40, 987654321));
1329 }
1330
1331 @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
1332 public void test_plus_Period_null() {
1333 TEST_2007_07_15_12_30_40_987654321.plus((MockSimplePeriod) null);
1334 }
1335
1336 @Test(expectedExceptions=DateTimeException.class, groups={"tck"})
1337 public void test_plus_Period_invalidTooLarge() {
1338 MockSimplePeriod period = MockSimplePeriod.of(1, ChronoUnit.YEARS);
1339 LocalDateTime.of(Year.MAX_VALUE, 1, 1, 0, 0).plus(period);
1340 }
1341
1342 @Test(expectedExceptions=DateTimeException.class, groups={"tck"})
1343 public void test_plus_Period_invalidTooSmall() {
1344 MockSimplePeriod period = MockSimplePeriod.of(-1, ChronoUnit.YEARS);
1345 LocalDateTime.of(Year.MIN_VALUE, 1, 1, 0, 0).plus(period);
1346 }
1347
1348 //-----------------------------------------------------------------------
1349 // plus(long,TemporalUnit)
1350 //-----------------------------------------------------------------------
1351 @Test(groups={"tck"})
1352 public void test_plus_longTemporalUnit_positiveMonths() {
1353 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plus(7, ChronoUnit.MONTHS);
1354 assertEquals(t, LocalDateTime.of(2008, 2, 15, 12, 30, 40, 987654321));
1355 }
1356
1357 @Test(groups={"tck"})
1358 public void test_plus_longTemporalUnit_negativeDays() {
1359 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plus(-25, ChronoUnit.DAYS);
1360 assertEquals(t, LocalDateTime.of(2007, 6, 20, 12, 30, 40, 987654321));
1361 }
1362
1363 @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
1364 public void test_plus_longTemporalUnit_null() {
1365 TEST_2007_07_15_12_30_40_987654321.plus(1, (TemporalUnit) null);
1366 }
1367
1680 public void test_plusDays_invalidTooSmall() {
1681 createDateMidnight(Year.MIN_VALUE, 1, 1).plusDays(-1);
1682 }
1683
1684 @Test(expectedExceptions=ArithmeticException.class, groups={"tck"})
1685 public void test_plusDays_overflowTooLarge() {
1686 createDateMidnight(Year.MAX_VALUE, 12, 31).plusDays(Long.MAX_VALUE);
1687 }
1688
1689 @Test(expectedExceptions=ArithmeticException.class, groups={"tck"})
1690 public void test_plusDays_overflowTooSmall() {
1691 createDateMidnight(Year.MIN_VALUE, 1, 1).plusDays(Long.MIN_VALUE);
1692 }
1693
1694 //-----------------------------------------------------------------------
1695 // plusHours()
1696 //-----------------------------------------------------------------------
1697 @Test(groups={"tck"})
1698 public void test_plusHours_one() {
1699 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
1700 LocalDate d = t.getDate();
1701
1702 for (int i = 0; i < 50; i++) {
1703 t = t.plusHours(1);
1704
1705 if ((i + 1) % 24 == 0) {
1706 d = d.plusDays(1);
1707 }
1708
1709 assertEquals(t.getDate(), d);
1710 assertEquals(t.getHour(), (i + 1) % 24);
1711 }
1712 }
1713
1714 @Test(groups={"tck"})
1715 public void test_plusHours_fromZero() {
1716 LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
1717 LocalDate d = base.getDate().minusDays(3);
1718 LocalTime t = LocalTime.of(21, 0);
1719
1720 for (int i = -50; i < 50; i++) {
1721 LocalDateTime dt = base.plusHours(i);
1722 t = t.plusHours(1);
1723
1724 if (t.getHour() == 0) {
1725 d = d.plusDays(1);
1726 }
1727
1728 assertEquals(dt.getDate(), d);
1729 assertEquals(dt.getTime(), t);
1730 }
1731 }
1732
1733 @Test(groups={"tck"})
1734 public void test_plusHours_fromOne() {
1735 LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.of(1, 0));
1736 LocalDate d = base.getDate().minusDays(3);
1737 LocalTime t = LocalTime.of(22, 0);
1738
1739 for (int i = -50; i < 50; i++) {
1740 LocalDateTime dt = base.plusHours(i);
1741
1742 t = t.plusHours(1);
1743
1744 if (t.getHour() == 0) {
1745 d = d.plusDays(1);
1746 }
1747
1748 assertEquals(dt.getDate(), d);
1749 assertEquals(dt.getTime(), t);
1750 }
1751 }
1752
1753 //-----------------------------------------------------------------------
1754 // plusMinutes()
1755 //-----------------------------------------------------------------------
1756 @Test(groups={"tck"})
1757 public void test_plusMinutes_one() {
1758 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
1759 LocalDate d = t.getDate();
1760
1761 int hour = 0;
1762 int min = 0;
1763
1764 for (int i = 0; i < 70; i++) {
1765 t = t.plusMinutes(1);
1766 min++;
1767 if (min == 60) {
1768 hour++;
1769 min = 0;
1770 }
1771
1772 assertEquals(t.getDate(), d);
1773 assertEquals(t.getHour(), hour);
1774 assertEquals(t.getMinute(), min);
1775 }
1776 }
1777
1778 @Test(groups={"tck"})
1779 public void test_plusMinutes_fromZero() {
1780 LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
1781 LocalDate d = base.getDate().minusDays(1);
1782 LocalTime t = LocalTime.of(22, 49);
1783
1784 for (int i = -70; i < 70; i++) {
1785 LocalDateTime dt = base.plusMinutes(i);
1786 t = t.plusMinutes(1);
1787
1788 if (t == LocalTime.MIDNIGHT) {
1789 d = d.plusDays(1);
1790 }
1791
1792 assertEquals(dt.getDate(), d, String.valueOf(i));
1793 assertEquals(dt.getTime(), t, String.valueOf(i));
1794 }
1795 }
1796
1797 @Test(groups={"tck"})
1798 public void test_plusMinutes_noChange_oneDay() {
1799 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plusMinutes(24 * 60);
1800 assertEquals(t.getDate(), TEST_2007_07_15_12_30_40_987654321.getDate().plusDays(1));
1801 }
1802
1803 //-----------------------------------------------------------------------
1804 // plusSeconds()
1805 //-----------------------------------------------------------------------
1806 @Test(groups={"tck"})
1807 public void test_plusSeconds_one() {
1808 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
1809 LocalDate d = t.getDate();
1810
1811 int hour = 0;
1812 int min = 0;
1813 int sec = 0;
1814
1815 for (int i = 0; i < 3700; i++) {
1816 t = t.plusSeconds(1);
1817 sec++;
1818 if (sec == 60) {
1819 min++;
1820 sec = 0;
1821 }
1822 if (min == 60) {
1823 hour++;
1824 min = 0;
1825 }
1826
1827 assertEquals(t.getDate(), d);
1828 assertEquals(t.getHour(), hour);
1829 assertEquals(t.getMinute(), min);
1830 assertEquals(t.getSecond(), sec);
1831 }
1832 }
1833
1834 @DataProvider(name="plusSeconds_fromZero")
1835 Iterator<Object[]> plusSeconds_fromZero() {
1836 return new Iterator<Object[]>() {
1837 int delta = 30;
1838
1839 int i = -3660;
1840 LocalDate date = TEST_2007_07_15_12_30_40_987654321.getDate().minusDays(1);
1841 int hour = 22;
1842 int min = 59;
1843 int sec = 0;
1844
1845 public boolean hasNext() {
1846 return i <= 3660;
1847 }
1848
1849 public Object[] next() {
1850 final Object[] ret = new Object[] {i, date, hour, min, sec};
1851 i += delta;
1852 sec += delta;
1853
1854 if (sec >= 60) {
1855 min++;
1856 sec -= 60;
1857
1858 if (min == 60) {
1859 hour++;
1860 min = 0;
1866 }
1867
1868 if (i == 0) {
1869 date = date.plusDays(1);
1870 }
1871
1872 return ret;
1873 }
1874
1875 public void remove() {
1876 throw new UnsupportedOperationException();
1877 }
1878 };
1879 }
1880
1881 @Test(dataProvider="plusSeconds_fromZero", groups={"tck"})
1882 public void test_plusSeconds_fromZero(int seconds, LocalDate date, int hour, int min, int sec) {
1883 LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
1884 LocalDateTime t = base.plusSeconds(seconds);
1885
1886 assertEquals(date, t.getDate());
1887 assertEquals(hour, t.getHour());
1888 assertEquals(min, t.getMinute());
1889 assertEquals(sec, t.getSecond());
1890 }
1891
1892 @Test(groups={"tck"})
1893 public void test_plusSeconds_noChange_oneDay() {
1894 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plusSeconds(24 * 60 * 60);
1895 assertEquals(t.getDate(), TEST_2007_07_15_12_30_40_987654321.getDate().plusDays(1));
1896 }
1897
1898 //-----------------------------------------------------------------------
1899 // plusNanos()
1900 //-----------------------------------------------------------------------
1901 @Test(groups={"tck"})
1902 public void test_plusNanos_halfABillion() {
1903 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
1904 LocalDate d = t.getDate();
1905
1906 int hour = 0;
1907 int min = 0;
1908 int sec = 0;
1909 int nanos = 0;
1910
1911 for (long i = 0; i < 3700 * 1000000000L; i+= 500000000) {
1912 t = t.plusNanos(500000000);
1913 nanos += 500000000;
1914 if (nanos == 1000000000) {
1915 sec++;
1916 nanos = 0;
1917 }
1918 if (sec == 60) {
1919 min++;
1920 sec = 0;
1921 }
1922 if (min == 60) {
1923 hour++;
1924 min = 0;
1925 }
1926
1927 assertEquals(t.getDate(), d, String.valueOf(i));
1928 assertEquals(t.getHour(), hour);
1929 assertEquals(t.getMinute(), min);
1930 assertEquals(t.getSecond(), sec);
1931 assertEquals(t.getNano(), nanos);
1932 }
1933 }
1934
1935 @DataProvider(name="plusNanos_fromZero")
1936 Iterator<Object[]> plusNanos_fromZero() {
1937 return new Iterator<Object[]>() {
1938 long delta = 7500000000L;
1939
1940 long i = -3660 * 1000000000L;
1941 LocalDate date = TEST_2007_07_15_12_30_40_987654321.getDate().minusDays(1);
1942 int hour = 22;
1943 int min = 59;
1944 int sec = 0;
1945 long nanos = 0;
1946
1947 public boolean hasNext() {
1948 return i <= 3660 * 1000000000L;
1949 }
1950
1951 public Object[] next() {
1952 final Object[] ret = new Object[] {i, date, hour, min, sec, (int)nanos};
1953 i += delta;
1954 nanos += delta;
1955
1956 if (nanos >= 1000000000L) {
1957 sec += nanos / 1000000000L;
1958 nanos %= 1000000000L;
1959
1960 if (sec >= 60) {
1961 min++;
1970 date = date.plusDays(1);
1971 }
1972 }
1973 }
1974 }
1975
1976 return ret;
1977 }
1978
1979 public void remove() {
1980 throw new UnsupportedOperationException();
1981 }
1982 };
1983 }
1984
1985 @Test(dataProvider="plusNanos_fromZero", groups={"tck"})
1986 public void test_plusNanos_fromZero(long nanoseconds, LocalDate date, int hour, int min, int sec, int nanos) {
1987 LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
1988 LocalDateTime t = base.plusNanos(nanoseconds);
1989
1990 assertEquals(date, t.getDate());
1991 assertEquals(hour, t.getHour());
1992 assertEquals(min, t.getMinute());
1993 assertEquals(sec, t.getSecond());
1994 assertEquals(nanos, t.getNano());
1995 }
1996
1997 @Test(groups={"tck"})
1998 public void test_plusNanos_noChange_oneDay() {
1999 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plusNanos(24 * 60 * 60 * 1000000000L);
2000 assertEquals(t.getDate(), TEST_2007_07_15_12_30_40_987654321.getDate().plusDays(1));
2001 }
2002
2003 //-----------------------------------------------------------------------
2004 // minus(adjuster)
2005 //-----------------------------------------------------------------------
2006 @Test(groups={"tck"})
2007 public void test_minus_adjuster() {
2008 Period p = Period.ofTime(0, 0, 62, 3);
2009 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minus(p);
2010 assertEquals(t, LocalDateTime.of(2007, 7, 15, 12, 29, 38, 987654318));
2011 }
2012
2013 @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
2014 public void test_minus_adjuster_null() {
2015 TEST_2007_07_15_12_30_40_987654321.minus((TemporalSubtractor) null);
2016 }
2017
2018 //-----------------------------------------------------------------------
2019 // minus(Period)
2020 //-----------------------------------------------------------------------
2021 @Test(groups={"tck"})
2022 public void test_minus_Period_positiveMonths() {
2023 MockSimplePeriod period = MockSimplePeriod.of(7, ChronoUnit.MONTHS);
2024 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minus(period);
2025 assertEquals(t, LocalDateTime.of(2006, 12, 15, 12, 30, 40, 987654321));
2026 }
2027
2028 @Test(groups={"tck"})
2029 public void test_minus_Period_negativeDays() {
2030 MockSimplePeriod period = MockSimplePeriod.of(-25, ChronoUnit.DAYS);
2031 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minus(period);
2032 assertEquals(t, LocalDateTime.of(2007, 8, 9, 12, 30, 40, 987654321));
2033 }
2034
2035 @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
2036 public void test_minus_Period_null() {
2037 TEST_2007_07_15_12_30_40_987654321.minus((MockSimplePeriod) null);
2038 }
2039
2040 @Test(expectedExceptions=DateTimeException.class, groups={"tck"})
2041 public void test_minus_Period_invalidTooLarge() {
2042 MockSimplePeriod period = MockSimplePeriod.of(-1, ChronoUnit.YEARS);
2043 LocalDateTime.of(Year.MAX_VALUE, 1, 1, 0, 0).minus(period);
2044 }
2045
2046 @Test(expectedExceptions=DateTimeException.class, groups={"tck"})
2047 public void test_minus_Period_invalidTooSmall() {
2048 MockSimplePeriod period = MockSimplePeriod.of(1, ChronoUnit.YEARS);
2049 LocalDateTime.of(Year.MIN_VALUE, 1, 1, 0, 0).minus(period);
2050 }
2051
2052 //-----------------------------------------------------------------------
2053 // minus(long,TemporalUnit)
2054 //-----------------------------------------------------------------------
2055 @Test(groups={"tck"})
2056 public void test_minus_longTemporalUnit_positiveMonths() {
2057 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minus(7, ChronoUnit.MONTHS);
2058 assertEquals(t, LocalDateTime.of(2006, 12, 15, 12, 30, 40, 987654321));
2059 }
2060
2061 @Test(groups={"tck"})
2062 public void test_minus_longTemporalUnit_negativeDays() {
2063 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minus(-25, ChronoUnit.DAYS);
2064 assertEquals(t, LocalDateTime.of(2007, 8, 9, 12, 30, 40, 987654321));
2065 }
2066
2067 @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
2068 public void test_minus_longTemporalUnit_null() {
2069 TEST_2007_07_15_12_30_40_987654321.minus(1, (TemporalUnit) null);
2070 }
2071
2384 public void test_minusDays_invalidTooSmall() {
2385 createDateMidnight(Year.MIN_VALUE, 1, 1).minusDays(1);
2386 }
2387
2388 @Test(expectedExceptions=ArithmeticException.class, groups={"tck"})
2389 public void test_minusDays_overflowTooLarge() {
2390 createDateMidnight(Year.MAX_VALUE, 12, 31).minusDays(Long.MIN_VALUE);
2391 }
2392
2393 @Test(expectedExceptions=ArithmeticException.class, groups={"tck"})
2394 public void test_minusDays_overflowTooSmall() {
2395 createDateMidnight(Year.MIN_VALUE, 1, 1).minusDays(Long.MAX_VALUE);
2396 }
2397
2398 //-----------------------------------------------------------------------
2399 // minusHours()
2400 //-----------------------------------------------------------------------
2401 @Test(groups={"tck"})
2402 public void test_minusHours_one() {
2403 LocalDateTime t =TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
2404 LocalDate d = t.getDate();
2405
2406 for (int i = 0; i < 50; i++) {
2407 t = t.minusHours(1);
2408
2409 if (i % 24 == 0) {
2410 d = d.minusDays(1);
2411 }
2412
2413 assertEquals(t.getDate(), d);
2414 assertEquals(t.getHour(), (((-i + 23) % 24) + 24) % 24);
2415 }
2416 }
2417
2418 @Test(groups={"tck"})
2419 public void test_minusHours_fromZero() {
2420 LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
2421 LocalDate d = base.getDate().plusDays(2);
2422 LocalTime t = LocalTime.of(3, 0);
2423
2424 for (int i = -50; i < 50; i++) {
2425 LocalDateTime dt = base.minusHours(i);
2426 t = t.minusHours(1);
2427
2428 if (t.getHour() == 23) {
2429 d = d.minusDays(1);
2430 }
2431
2432 assertEquals(dt.getDate(), d, String.valueOf(i));
2433 assertEquals(dt.getTime(), t);
2434 }
2435 }
2436
2437 @Test(groups={"tck"})
2438 public void test_minusHours_fromOne() {
2439 LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.of(1, 0));
2440 LocalDate d = base.getDate().plusDays(2);
2441 LocalTime t = LocalTime.of(4, 0);
2442
2443 for (int i = -50; i < 50; i++) {
2444 LocalDateTime dt = base.minusHours(i);
2445
2446 t = t.minusHours(1);
2447
2448 if (t.getHour() == 23) {
2449 d = d.minusDays(1);
2450 }
2451
2452 assertEquals(dt.getDate(), d, String.valueOf(i));
2453 assertEquals(dt.getTime(), t);
2454 }
2455 }
2456
2457 //-----------------------------------------------------------------------
2458 // minusMinutes()
2459 //-----------------------------------------------------------------------
2460 @Test(groups={"tck"})
2461 public void test_minusMinutes_one() {
2462 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
2463 LocalDate d = t.getDate().minusDays(1);
2464
2465 int hour = 0;
2466 int min = 0;
2467
2468 for (int i = 0; i < 70; i++) {
2469 t = t.minusMinutes(1);
2470 min--;
2471 if (min == -1) {
2472 hour--;
2473 min = 59;
2474
2475 if (hour == -1) {
2476 hour = 23;
2477 }
2478 }
2479 assertEquals(t.getDate(), d);
2480 assertEquals(t.getHour(), hour);
2481 assertEquals(t.getMinute(), min);
2482 }
2483 }
2484
2485 @Test(groups={"tck"})
2486 public void test_minusMinutes_fromZero() {
2487 LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
2488 LocalDate d = base.getDate().minusDays(1);
2489 LocalTime t = LocalTime.of(22, 49);
2490
2491 for (int i = 70; i > -70; i--) {
2492 LocalDateTime dt = base.minusMinutes(i);
2493 t = t.plusMinutes(1);
2494
2495 if (t == LocalTime.MIDNIGHT) {
2496 d = d.plusDays(1);
2497 }
2498
2499 assertEquals(dt.getDate(), d);
2500 assertEquals(dt.getTime(), t);
2501 }
2502 }
2503
2504 @Test(groups={"tck"})
2505 public void test_minusMinutes_noChange_oneDay() {
2506 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minusMinutes(24 * 60);
2507 assertEquals(t.getDate(), TEST_2007_07_15_12_30_40_987654321.getDate().minusDays(1));
2508 }
2509
2510 //-----------------------------------------------------------------------
2511 // minusSeconds()
2512 //-----------------------------------------------------------------------
2513 @Test(groups={"tck"})
2514 public void test_minusSeconds_one() {
2515 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
2516 LocalDate d = t.getDate().minusDays(1);
2517
2518 int hour = 0;
2519 int min = 0;
2520 int sec = 0;
2521
2522 for (int i = 0; i < 3700; i++) {
2523 t = t.minusSeconds(1);
2524 sec--;
2525 if (sec == -1) {
2526 min--;
2527 sec = 59;
2528
2529 if (min == -1) {
2530 hour--;
2531 min = 59;
2532
2533 if (hour == -1) {
2534 hour = 23;
2535 }
2536 }
2537 }
2538
2539 assertEquals(t.getDate(), d);
2540 assertEquals(t.getHour(), hour);
2541 assertEquals(t.getMinute(), min);
2542 assertEquals(t.getSecond(), sec);
2543 }
2544 }
2545
2546 @DataProvider(name="minusSeconds_fromZero")
2547 Iterator<Object[]> minusSeconds_fromZero() {
2548 return new Iterator<Object[]>() {
2549 int delta = 30;
2550
2551 int i = 3660;
2552 LocalDate date = TEST_2007_07_15_12_30_40_987654321.getDate().minusDays(1);
2553 int hour = 22;
2554 int min = 59;
2555 int sec = 0;
2556
2557 public boolean hasNext() {
2558 return i >= -3660;
2559 }
2560
2561 public Object[] next() {
2562 final Object[] ret = new Object[] {i, date, hour, min, sec};
2563 i -= delta;
2564 sec += delta;
2565
2566 if (sec >= 60) {
2567 min++;
2568 sec -= 60;
2569
2570 if (min == 60) {
2571 hour++;
2572 min = 0;
2578 }
2579
2580 if (i == 0) {
2581 date = date.plusDays(1);
2582 }
2583
2584 return ret;
2585 }
2586
2587 public void remove() {
2588 throw new UnsupportedOperationException();
2589 }
2590 };
2591 }
2592
2593 @Test(dataProvider="minusSeconds_fromZero", groups={"tck"})
2594 public void test_minusSeconds_fromZero(int seconds, LocalDate date, int hour, int min, int sec) {
2595 LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
2596 LocalDateTime t = base.minusSeconds(seconds);
2597
2598 assertEquals(date, t.getDate());
2599 assertEquals(hour, t.getHour());
2600 assertEquals(min, t.getMinute());
2601 assertEquals(sec, t.getSecond());
2602 }
2603
2604 //-----------------------------------------------------------------------
2605 // minusNanos()
2606 //-----------------------------------------------------------------------
2607 @Test(groups={"tck"})
2608 public void test_minusNanos_halfABillion() {
2609 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
2610 LocalDate d = t.getDate().minusDays(1);
2611
2612 int hour = 0;
2613 int min = 0;
2614 int sec = 0;
2615 int nanos = 0;
2616
2617 for (long i = 0; i < 3700 * 1000000000L; i+= 500000000) {
2618 t = t.minusNanos(500000000);
2619 nanos -= 500000000;
2620
2621 if (nanos < 0) {
2622 sec--;
2623 nanos += 1000000000;
2624
2625 if (sec == -1) {
2626 min--;
2627 sec += 60;
2628
2629 if (min == -1) {
2630 hour--;
2631 min += 60;
2632
2633 if (hour == -1) {
2634 hour += 24;
2635 }
2636 }
2637 }
2638 }
2639
2640 assertEquals(t.getDate(), d);
2641 assertEquals(t.getHour(), hour);
2642 assertEquals(t.getMinute(), min);
2643 assertEquals(t.getSecond(), sec);
2644 assertEquals(t.getNano(), nanos);
2645 }
2646 }
2647
2648 @DataProvider(name="minusNanos_fromZero")
2649 Iterator<Object[]> minusNanos_fromZero() {
2650 return new Iterator<Object[]>() {
2651 long delta = 7500000000L;
2652
2653 long i = 3660 * 1000000000L;
2654 LocalDate date = TEST_2007_07_15_12_30_40_987654321.getDate().minusDays(1);
2655 int hour = 22;
2656 int min = 59;
2657 int sec = 0;
2658 long nanos = 0;
2659
2660 public boolean hasNext() {
2661 return i >= -3660 * 1000000000L;
2662 }
2663
2664 public Object[] next() {
2665 final Object[] ret = new Object[] {i, date, hour, min, sec, (int)nanos};
2666 i -= delta;
2667 nanos += delta;
2668
2669 if (nanos >= 1000000000L) {
2670 sec += nanos / 1000000000L;
2671 nanos %= 1000000000L;
2672
2673 if (sec >= 60) {
2674 min++;
2683 date = date.plusDays(1);
2684 }
2685 }
2686 }
2687 }
2688
2689 return ret;
2690 }
2691
2692 public void remove() {
2693 throw new UnsupportedOperationException();
2694 }
2695 };
2696 }
2697
2698 @Test(dataProvider="minusNanos_fromZero", groups={"tck"})
2699 public void test_minusNanos_fromZero(long nanoseconds, LocalDate date, int hour, int min, int sec, int nanos) {
2700 LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
2701 LocalDateTime t = base.minusNanos(nanoseconds);
2702
2703 assertEquals(date, t.getDate());
2704 assertEquals(hour, t.getHour());
2705 assertEquals(min, t.getMinute());
2706 assertEquals(sec, t.getSecond());
2707 assertEquals(nanos, t.getNano());
2708 }
2709
2710 //-----------------------------------------------------------------------
2711 // atOffset()
2712 //-----------------------------------------------------------------------
2713 @Test(groups={"tck"})
2714 public void test_atOffset() {
2715 LocalDateTime t = LocalDateTime.of(2008, 6, 30, 11, 30);
2716 assertEquals(t.atOffset(OFFSET_PTWO), OffsetDateTime.of(LocalDateTime.of(2008, 6, 30, 11, 30), OFFSET_PTWO));
2717 }
2718
2719 @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
2720 public void test_atOffset_nullZoneOffset() {
2721 LocalDateTime t = LocalDateTime.of(2008, 6, 30, 11, 30);
2722 t.atOffset((ZoneOffset) null);
2723 }
3015 {2008, 7, 5, 2, 1, 0, 0, "2008-07-05T02:01"},
3016 {2007, 12, 31, 23, 59, 1, 0, "2007-12-31T23:59:01"},
3017 {999, 12, 31, 23, 59, 59, 990000000, "0999-12-31T23:59:59.990"},
3018 {-1, 1, 2, 23, 59, 59, 999990000, "-0001-01-02T23:59:59.999990"},
3019 {-2008, 1, 2, 23, 59, 59, 999999990, "-2008-01-02T23:59:59.999999990"},
3020 };
3021 }
3022
3023 @Test(dataProvider="sampleToString", groups={"tck"})
3024 public void test_toString(int y, int m, int d, int h, int mi, int s, int n, String expected) {
3025 LocalDateTime t = LocalDateTime.of(y, m, d, h, mi, s, n);
3026 String str = t.toString();
3027 assertEquals(str, expected);
3028 }
3029
3030 //-----------------------------------------------------------------------
3031 // toString(DateTimeFormatter)
3032 //-----------------------------------------------------------------------
3033 @Test(groups={"tck"})
3034 public void test_toString_formatter() {
3035 DateTimeFormatter f = DateTimeFormatters.pattern("y M d H m s");
3036 String t = LocalDateTime.of(2010, 12, 3, 11, 30, 45).toString(f);
3037 assertEquals(t, "2010 12 3 11 30 45");
3038 }
3039
3040 @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
3041 public void test_toString_formatter_null() {
3042 LocalDateTime.of(2010, 12, 3, 11, 30, 45).toString(null);
3043 }
3044
3045 }
|
71 import static java.time.temporal.ChronoField.DAY_OF_YEAR;
72 import static java.time.temporal.ChronoField.EPOCH_DAY;
73 import static java.time.temporal.ChronoField.EPOCH_MONTH;
74 import static java.time.temporal.ChronoField.ERA;
75 import static java.time.temporal.ChronoField.HOUR_OF_AMPM;
76 import static java.time.temporal.ChronoField.HOUR_OF_DAY;
77 import static java.time.temporal.ChronoField.MICRO_OF_DAY;
78 import static java.time.temporal.ChronoField.MICRO_OF_SECOND;
79 import static java.time.temporal.ChronoField.MILLI_OF_DAY;
80 import static java.time.temporal.ChronoField.MILLI_OF_SECOND;
81 import static java.time.temporal.ChronoField.MINUTE_OF_DAY;
82 import static java.time.temporal.ChronoField.MINUTE_OF_HOUR;
83 import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
84 import static java.time.temporal.ChronoField.NANO_OF_DAY;
85 import static java.time.temporal.ChronoField.NANO_OF_SECOND;
86 import static java.time.temporal.ChronoField.SECOND_OF_DAY;
87 import static java.time.temporal.ChronoField.SECOND_OF_MINUTE;
88 import static java.time.temporal.ChronoField.YEAR;
89 import static java.time.temporal.ChronoField.YEAR_OF_ERA;
90 import static java.time.temporal.ChronoUnit.DAYS;
91 import static java.time.temporal.ChronoUnit.MONTHS;
92 import static java.time.temporal.ChronoUnit.NANOS;
93 import static java.time.temporal.ChronoUnit.SECONDS;
94 import static java.time.temporal.ChronoUnit.YEARS;
95 import static org.testng.Assert.assertEquals;
96 import static org.testng.Assert.assertFalse;
97 import static org.testng.Assert.assertSame;
98 import static org.testng.Assert.assertTrue;
99
100 import java.io.ByteArrayOutputStream;
101 import java.io.DataOutputStream;
102 import java.time.Clock;
103 import java.time.DateTimeException;
104 import java.time.DayOfWeek;
105 import java.time.Instant;
106 import java.time.LocalDate;
107 import java.time.LocalDateTime;
108 import java.time.LocalTime;
109 import java.time.Month;
110 import java.time.OffsetDateTime;
111 import java.time.Year;
112 import java.time.ZoneId;
113 import java.time.ZoneOffset;
114 import java.time.ZonedDateTime;
115 import java.time.chrono.IsoChronology;
116 import java.time.format.DateTimeFormatter;
117 import java.time.format.DateTimeParseException;
118 import java.time.temporal.ChronoField;
119 import java.time.temporal.ChronoUnit;
120 import java.time.temporal.JulianFields;
121 import java.time.temporal.Queries;
122 import java.time.temporal.Temporal;
123 import java.time.temporal.TemporalAccessor;
124 import java.time.temporal.TemporalAdjuster;
125 import java.time.temporal.TemporalAmount;
126 import java.time.temporal.TemporalField;
127 import java.time.temporal.TemporalQuery;
128 import java.time.temporal.TemporalUnit;
129 import java.util.ArrayList;
130 import java.util.Arrays;
131 import java.util.Iterator;
132 import java.util.List;
133
134 import org.testng.annotations.BeforeMethod;
135 import org.testng.annotations.DataProvider;
136 import org.testng.annotations.Test;
137
138 /**
139 * Test LocalDateTime.
140 */
141 @Test
142 public class TCKLocalDateTime extends AbstractDateTimeTest {
143
144 private static final ZoneOffset OFFSET_PONE = ZoneOffset.ofHours(1);
145 private static final ZoneOffset OFFSET_PTWO = ZoneOffset.ofHours(2);
146 private static final ZoneOffset OFFSET_MTWO = ZoneOffset.ofHours(-2);
147 private static final ZoneId ZONE_PARIS = ZoneId.of("Europe/Paris");
148 private static final ZoneId ZONE_GAZA = ZoneId.of("Asia/Gaza");
149
150 private LocalDateTime TEST_2007_07_15_12_30_40_987654321 = LocalDateTime.of(2007, 7, 15, 12, 30, 40, 987654321);
151 private LocalDateTime MAX_DATE_TIME;
152 private LocalDateTime MIN_DATE_TIME;
153 private Instant MAX_INSTANT;
154 private Instant MIN_INSTANT;
155
156 @BeforeMethod(groups={"implementation","tck"})
259 //-----------------------------------------------------------------------
260 // constants
261 //-----------------------------------------------------------------------
262 @Test
263 public void constant_MIN() {
264 check(LocalDateTime.MIN, Year.MIN_VALUE, 1, 1, 0, 0, 0, 0);
265 }
266
267 @Test
268 public void constant_MAX() {
269 check(LocalDateTime.MAX, Year.MAX_VALUE, 12, 31, 23, 59, 59, 999999999);
270 }
271
272 //-----------------------------------------------------------------------
273 // now()
274 //-----------------------------------------------------------------------
275 @Test(timeOut=30000, groups={"tck"}) // TODO: remove when time zone loading is faster
276 public void now() {
277 LocalDateTime expected = LocalDateTime.now(Clock.systemDefaultZone());
278 LocalDateTime test = LocalDateTime.now();
279 long diff = Math.abs(test.toLocalTime().toNanoOfDay() - expected.toLocalTime().toNanoOfDay());
280 if (diff >= 100000000) {
281 // may be date change
282 expected = LocalDateTime.now(Clock.systemDefaultZone());
283 test = LocalDateTime.now();
284 diff = Math.abs(test.toLocalTime().toNanoOfDay() - expected.toLocalTime().toNanoOfDay());
285 }
286 assertTrue(diff < 100000000); // less than 0.1 secs
287 }
288
289 //-----------------------------------------------------------------------
290 // now(ZoneId)
291 //-----------------------------------------------------------------------
292 @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
293 public void now_ZoneId_nullZoneId() {
294 LocalDateTime.now((ZoneId) null);
295 }
296
297 @Test(groups={"tck"})
298 public void now_ZoneId() {
299 ZoneId zone = ZoneId.of("UTC+01:02:03");
300 LocalDateTime expected = LocalDateTime.now(Clock.system(zone));
301 LocalDateTime test = LocalDateTime.now(zone);
302 for (int i = 0; i < 100; i++) {
303 if (expected.equals(test)) {
304 return;
343 assertEquals(test.getMonth(), Month.JANUARY);
344 assertEquals(test.getDayOfMonth(), (i < 24 * 60 * 60) ? 1 : 2);
345 assertEquals(test.getHour(), (i / (60 * 60)) % 24);
346 assertEquals(test.getMinute(), (i / 60) % 60);
347 assertEquals(test.getSecond(), i % 60);
348 assertEquals(test.getNano(), 123456789);
349 }
350 }
351
352 @Test(groups={"tck"})
353 public void now_Clock_allSecsInDay_beforeEpoch() {
354 LocalTime expected = LocalTime.MIDNIGHT.plusNanos(123456789L);
355 for (int i =-1; i >= -(24 * 60 * 60); i--) {
356 Instant instant = Instant.ofEpochSecond(i).plusNanos(123456789L);
357 Clock clock = Clock.fixed(instant, ZoneOffset.UTC);
358 LocalDateTime test = LocalDateTime.now(clock);
359 assertEquals(test.getYear(), 1969);
360 assertEquals(test.getMonth(), Month.DECEMBER);
361 assertEquals(test.getDayOfMonth(), 31);
362 expected = expected.minusSeconds(1);
363 assertEquals(test.toLocalTime(), expected);
364 }
365 }
366
367 //-----------------------------------------------------------------------
368 @Test(groups={"tck"})
369 public void now_Clock_maxYear() {
370 Clock clock = Clock.fixed(MAX_INSTANT, ZoneOffset.UTC);
371 LocalDateTime test = LocalDateTime.now(clock);
372 assertEquals(test, MAX_DATE_TIME);
373 }
374
375 @Test(expectedExceptions=DateTimeException.class, groups={"tck"})
376 public void now_Clock_tooBig() {
377 Clock clock = Clock.fixed(MAX_INSTANT.plusSeconds(24 * 60 * 60), ZoneOffset.UTC);
378 LocalDateTime.now(clock);
379 }
380
381 @Test(groups={"tck"})
382 public void now_Clock_minYear() {
383 Clock clock = Clock.fixed(MIN_INSTANT, ZoneOffset.UTC);
894 @Test(expectedExceptions=DateTimeParseException.class, groups={"tck"})
895 public void factory_parse_illegalValue() {
896 LocalDateTime.parse("2008-06-32T11:15");
897 }
898
899 @Test(expectedExceptions=DateTimeParseException.class, groups={"tck"})
900 public void factory_parse_invalidValue() {
901 LocalDateTime.parse("2008-06-31T11:15");
902 }
903
904 @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
905 public void factory_parse_nullText() {
906 LocalDateTime.parse((String) null);
907 }
908
909 //-----------------------------------------------------------------------
910 // parse(DateTimeFormatter)
911 //-----------------------------------------------------------------------
912 @Test(groups={"tck"})
913 public void factory_parse_formatter() {
914 DateTimeFormatter f = DateTimeFormatter.ofPattern("y M d H m s");
915 LocalDateTime test = LocalDateTime.parse("2010 12 3 11 30 45", f);
916 assertEquals(test, LocalDateTime.of(2010, 12, 3, 11, 30, 45));
917 }
918
919 @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
920 public void factory_parse_formatter_nullText() {
921 DateTimeFormatter f = DateTimeFormatter.ofPattern("y M d H m s");
922 LocalDateTime.parse((String) null, f);
923 }
924
925 @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
926 public void factory_parse_formatter_nullFormatter() {
927 LocalDateTime.parse("ANY", null);
928 }
929
930 //-----------------------------------------------------------------------
931 // get(TemporalField)
932 //-----------------------------------------------------------------------
933 @Test
934 public void test_get_TemporalField() {
935 LocalDateTime test = LocalDateTime.of(2008, 6, 30, 12, 30, 40, 987654321);
936 assertEquals(test.get(ChronoField.YEAR), 2008);
937 assertEquals(test.get(ChronoField.MONTH_OF_YEAR), 6);
938 assertEquals(test.get(ChronoField.DAY_OF_MONTH), 30);
939 assertEquals(test.get(ChronoField.DAY_OF_WEEK), 1);
940 assertEquals(test.get(ChronoField.DAY_OF_YEAR), 182);
941
950 @Test
951 public void test_getLong_TemporalField() {
952 LocalDateTime test = LocalDateTime.of(2008, 6, 30, 12, 30, 40, 987654321);
953 assertEquals(test.getLong(ChronoField.YEAR), 2008);
954 assertEquals(test.getLong(ChronoField.MONTH_OF_YEAR), 6);
955 assertEquals(test.getLong(ChronoField.DAY_OF_MONTH), 30);
956 assertEquals(test.getLong(ChronoField.DAY_OF_WEEK), 1);
957 assertEquals(test.getLong(ChronoField.DAY_OF_YEAR), 182);
958
959 assertEquals(test.getLong(ChronoField.HOUR_OF_DAY), 12);
960 assertEquals(test.getLong(ChronoField.MINUTE_OF_HOUR), 30);
961 assertEquals(test.getLong(ChronoField.SECOND_OF_MINUTE), 40);
962 assertEquals(test.getLong(ChronoField.NANO_OF_SECOND), 987654321);
963 assertEquals(test.getLong(ChronoField.HOUR_OF_AMPM), 0);
964 assertEquals(test.getLong(ChronoField.AMPM_OF_DAY), 1);
965 }
966
967 //-----------------------------------------------------------------------
968 // query(TemporalQuery)
969 //-----------------------------------------------------------------------
970 @DataProvider(name="query")
971 Object[][] data_query() {
972 return new Object[][] {
973 {TEST_2007_07_15_12_30_40_987654321, Queries.chronology(), IsoChronology.INSTANCE},
974 {TEST_2007_07_15_12_30_40_987654321, Queries.zoneId(), null},
975 {TEST_2007_07_15_12_30_40_987654321, Queries.precision(), ChronoUnit.NANOS},
976 {TEST_2007_07_15_12_30_40_987654321, Queries.zone(), null},
977 {TEST_2007_07_15_12_30_40_987654321, Queries.offset(), null},
978 {TEST_2007_07_15_12_30_40_987654321, Queries.localDate(), LocalDate.of(2007, 7, 15)},
979 {TEST_2007_07_15_12_30_40_987654321, Queries.localTime(), LocalTime.of(12, 30, 40, 987654321)},
980 };
981 }
982
983 @Test(dataProvider="query")
984 public <T> void test_query(TemporalAccessor temporal, TemporalQuery<T> query, T expected) {
985 assertEquals(temporal.query(query), expected);
986 }
987
988 @Test(dataProvider="query")
989 public <T> void test_queryFrom(TemporalAccessor temporal, TemporalQuery<T> query, T expected) {
990 assertEquals(query.queryFrom(temporal), expected);
991 }
992
993 @Test(expectedExceptions=NullPointerException.class)
994 public void test_query_null() {
995 TEST_2007_07_15_12_30_40_987654321.query(null);
996 }
997
998 //-----------------------------------------------------------------------
999 @DataProvider(name="sampleDates")
1000 Object[][] provider_sampleDates() {
1001 return new Object[][] {
1002 {2008, 7, 5},
1003 {2007, 7, 5},
1004 {2006, 7, 5},
1005 {2005, 7, 5},
1006 {2004, 1, 1},
1007 {-1, 1, 2},
1008 };
1009 }
1010
1037 public void test_get_dates(int y, int m, int d) {
1038 LocalDateTime a = LocalDateTime.of(y, m, d, 12, 30);
1039 assertEquals(a.getYear(), y);
1040 assertEquals(a.getMonth(), Month.of(m));
1041 assertEquals(a.getDayOfMonth(), d);
1042 }
1043
1044 @Test(dataProvider="sampleDates", groups={"tck"})
1045 public void test_getDOY(int y, int m, int d) {
1046 LocalDateTime a = LocalDateTime.of(y, m, d, 12 ,30);
1047 int total = 0;
1048 for (int i = 1; i < m; i++) {
1049 total += Month.of(i).length(isIsoLeap(y));
1050 }
1051 int doy = total + d;
1052 assertEquals(a.getDayOfYear(), doy);
1053 }
1054
1055 @Test(dataProvider="sampleTimes", groups={"tck"})
1056 public void test_get_times(int h, int m, int s, int ns) {
1057 LocalDateTime a = LocalDateTime.of(TEST_2007_07_15_12_30_40_987654321.toLocalDate(), LocalTime.of(h, m, s, ns));
1058 assertEquals(a.getHour(), h);
1059 assertEquals(a.getMinute(), m);
1060 assertEquals(a.getSecond(), s);
1061 assertEquals(a.getNano(), ns);
1062 }
1063
1064 //-----------------------------------------------------------------------
1065 // getDayOfWeek()
1066 //-----------------------------------------------------------------------
1067 @Test(groups={"tck"})
1068 public void test_getDayOfWeek() {
1069 DayOfWeek dow = DayOfWeek.MONDAY;
1070 for (Month month : Month.values()) {
1071 int length = month.length(false);
1072 for (int i = 1; i <= length; i++) {
1073 LocalDateTime d = LocalDateTime.of(LocalDate.of(2007, month, i),
1074 TEST_2007_07_15_12_30_40_987654321.toLocalTime());
1075 assertSame(d.getDayOfWeek(), dow);
1076 dow = dow.plus(1);
1077 }
1078 }
1079 }
1080
1081 //-----------------------------------------------------------------------
1082 // with()
1083 //-----------------------------------------------------------------------
1084 @Test(groups={"tck"})
1085 public void test_with_adjustment() {
1086 final LocalDateTime sample = LocalDateTime.of(2012, 3, 4, 23, 5);
1087 TemporalAdjuster adjuster = new TemporalAdjuster() {
1088 @Override
1089 public Temporal adjustInto(Temporal dateTime) {
1090 return sample;
1091 }
1092 };
1093 assertEquals(TEST_2007_07_15_12_30_40_987654321.with(adjuster), sample);
1094 }
1269 public void test_withNanoOfSecond_nanoTooHigh() {
1270 TEST_2007_07_15_12_30_40_987654321.withNano(1000000000);
1271 }
1272
1273 //-----------------------------------------------------------------------
1274 // truncatedTo(TemporalUnit)
1275 //-----------------------------------------------------------------------
1276 @Test(groups={"tck"})
1277 public void test_truncatedTo_normal() {
1278 assertEquals(TEST_2007_07_15_12_30_40_987654321.truncatedTo(NANOS), TEST_2007_07_15_12_30_40_987654321);
1279 assertEquals(TEST_2007_07_15_12_30_40_987654321.truncatedTo(SECONDS), TEST_2007_07_15_12_30_40_987654321.withNano(0));
1280 assertEquals(TEST_2007_07_15_12_30_40_987654321.truncatedTo(DAYS), TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT));
1281 }
1282
1283 @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
1284 public void test_truncatedTo_null() {
1285 TEST_2007_07_15_12_30_40_987654321.truncatedTo(null);
1286 }
1287
1288 //-----------------------------------------------------------------------
1289 // plus(TemporalAmount)
1290 //-----------------------------------------------------------------------
1291 @Test
1292 public void test_plus_TemporalAmount_positiveMonths() {
1293 MockSimplePeriod period = MockSimplePeriod.of(7, MONTHS);
1294 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plus(period);
1295 assertEquals(t, LocalDateTime.of(2008, 2, 15, 12, 30, 40, 987654321));
1296 }
1297
1298 @Test
1299 public void test_plus_TemporalAmount_negativeDays() {
1300 MockSimplePeriod period = MockSimplePeriod.of(-25, DAYS);
1301 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plus(period);
1302 assertEquals(t, LocalDateTime.of(2007, 6, 20, 12, 30, 40, 987654321));
1303 }
1304
1305 @Test(expectedExceptions=DateTimeException.class)
1306 public void test_plus_TemporalAmount_invalidTooLarge() {
1307 MockSimplePeriod period = MockSimplePeriod.of(1, YEARS);
1308 LocalDateTime.of(Year.MAX_VALUE, 1, 1, 0, 0).plus(period);
1309 }
1310
1311 @Test(expectedExceptions=DateTimeException.class)
1312 public void test_plus_TemporalAmount_invalidTooSmall() {
1313 MockSimplePeriod period = MockSimplePeriod.of(-1, YEARS);
1314 LocalDateTime.of(Year.MIN_VALUE, 1, 1, 0, 0).plus(period);
1315 }
1316
1317 @Test(expectedExceptions=NullPointerException.class)
1318 public void test_plus_TemporalAmount_null() {
1319 TEST_2007_07_15_12_30_40_987654321.plus((TemporalAmount) null);
1320 }
1321
1322 //-----------------------------------------------------------------------
1323 // plus(long,TemporalUnit)
1324 //-----------------------------------------------------------------------
1325 @Test(groups={"tck"})
1326 public void test_plus_longTemporalUnit_positiveMonths() {
1327 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plus(7, ChronoUnit.MONTHS);
1328 assertEquals(t, LocalDateTime.of(2008, 2, 15, 12, 30, 40, 987654321));
1329 }
1330
1331 @Test(groups={"tck"})
1332 public void test_plus_longTemporalUnit_negativeDays() {
1333 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plus(-25, ChronoUnit.DAYS);
1334 assertEquals(t, LocalDateTime.of(2007, 6, 20, 12, 30, 40, 987654321));
1335 }
1336
1337 @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
1338 public void test_plus_longTemporalUnit_null() {
1339 TEST_2007_07_15_12_30_40_987654321.plus(1, (TemporalUnit) null);
1340 }
1341
1654 public void test_plusDays_invalidTooSmall() {
1655 createDateMidnight(Year.MIN_VALUE, 1, 1).plusDays(-1);
1656 }
1657
1658 @Test(expectedExceptions=ArithmeticException.class, groups={"tck"})
1659 public void test_plusDays_overflowTooLarge() {
1660 createDateMidnight(Year.MAX_VALUE, 12, 31).plusDays(Long.MAX_VALUE);
1661 }
1662
1663 @Test(expectedExceptions=ArithmeticException.class, groups={"tck"})
1664 public void test_plusDays_overflowTooSmall() {
1665 createDateMidnight(Year.MIN_VALUE, 1, 1).plusDays(Long.MIN_VALUE);
1666 }
1667
1668 //-----------------------------------------------------------------------
1669 // plusHours()
1670 //-----------------------------------------------------------------------
1671 @Test(groups={"tck"})
1672 public void test_plusHours_one() {
1673 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
1674 LocalDate d = t.toLocalDate();
1675
1676 for (int i = 0; i < 50; i++) {
1677 t = t.plusHours(1);
1678
1679 if ((i + 1) % 24 == 0) {
1680 d = d.plusDays(1);
1681 }
1682
1683 assertEquals(t.toLocalDate(), d);
1684 assertEquals(t.getHour(), (i + 1) % 24);
1685 }
1686 }
1687
1688 @Test(groups={"tck"})
1689 public void test_plusHours_fromZero() {
1690 LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
1691 LocalDate d = base.toLocalDate().minusDays(3);
1692 LocalTime t = LocalTime.of(21, 0);
1693
1694 for (int i = -50; i < 50; i++) {
1695 LocalDateTime dt = base.plusHours(i);
1696 t = t.plusHours(1);
1697
1698 if (t.getHour() == 0) {
1699 d = d.plusDays(1);
1700 }
1701
1702 assertEquals(dt.toLocalDate(), d);
1703 assertEquals(dt.toLocalTime(), t);
1704 }
1705 }
1706
1707 @Test(groups={"tck"})
1708 public void test_plusHours_fromOne() {
1709 LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.of(1, 0));
1710 LocalDate d = base.toLocalDate().minusDays(3);
1711 LocalTime t = LocalTime.of(22, 0);
1712
1713 for (int i = -50; i < 50; i++) {
1714 LocalDateTime dt = base.plusHours(i);
1715
1716 t = t.plusHours(1);
1717
1718 if (t.getHour() == 0) {
1719 d = d.plusDays(1);
1720 }
1721
1722 assertEquals(dt.toLocalDate(), d);
1723 assertEquals(dt.toLocalTime(), t);
1724 }
1725 }
1726
1727 //-----------------------------------------------------------------------
1728 // plusMinutes()
1729 //-----------------------------------------------------------------------
1730 @Test(groups={"tck"})
1731 public void test_plusMinutes_one() {
1732 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
1733 LocalDate d = t.toLocalDate();
1734
1735 int hour = 0;
1736 int min = 0;
1737
1738 for (int i = 0; i < 70; i++) {
1739 t = t.plusMinutes(1);
1740 min++;
1741 if (min == 60) {
1742 hour++;
1743 min = 0;
1744 }
1745
1746 assertEquals(t.toLocalDate(), d);
1747 assertEquals(t.getHour(), hour);
1748 assertEquals(t.getMinute(), min);
1749 }
1750 }
1751
1752 @Test(groups={"tck"})
1753 public void test_plusMinutes_fromZero() {
1754 LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
1755 LocalDate d = base.toLocalDate().minusDays(1);
1756 LocalTime t = LocalTime.of(22, 49);
1757
1758 for (int i = -70; i < 70; i++) {
1759 LocalDateTime dt = base.plusMinutes(i);
1760 t = t.plusMinutes(1);
1761
1762 if (t == LocalTime.MIDNIGHT) {
1763 d = d.plusDays(1);
1764 }
1765
1766 assertEquals(dt.toLocalDate(), d, String.valueOf(i));
1767 assertEquals(dt.toLocalTime(), t, String.valueOf(i));
1768 }
1769 }
1770
1771 @Test(groups={"tck"})
1772 public void test_plusMinutes_noChange_oneDay() {
1773 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plusMinutes(24 * 60);
1774 assertEquals(t.toLocalDate(), TEST_2007_07_15_12_30_40_987654321.toLocalDate().plusDays(1));
1775 }
1776
1777 //-----------------------------------------------------------------------
1778 // plusSeconds()
1779 //-----------------------------------------------------------------------
1780 @Test(groups={"tck"})
1781 public void test_plusSeconds_one() {
1782 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
1783 LocalDate d = t.toLocalDate();
1784
1785 int hour = 0;
1786 int min = 0;
1787 int sec = 0;
1788
1789 for (int i = 0; i < 3700; i++) {
1790 t = t.plusSeconds(1);
1791 sec++;
1792 if (sec == 60) {
1793 min++;
1794 sec = 0;
1795 }
1796 if (min == 60) {
1797 hour++;
1798 min = 0;
1799 }
1800
1801 assertEquals(t.toLocalDate(), d);
1802 assertEquals(t.getHour(), hour);
1803 assertEquals(t.getMinute(), min);
1804 assertEquals(t.getSecond(), sec);
1805 }
1806 }
1807
1808 @DataProvider(name="plusSeconds_fromZero")
1809 Iterator<Object[]> plusSeconds_fromZero() {
1810 return new Iterator<Object[]>() {
1811 int delta = 30;
1812
1813 int i = -3660;
1814 LocalDate date = TEST_2007_07_15_12_30_40_987654321.toLocalDate().minusDays(1);
1815 int hour = 22;
1816 int min = 59;
1817 int sec = 0;
1818
1819 public boolean hasNext() {
1820 return i <= 3660;
1821 }
1822
1823 public Object[] next() {
1824 final Object[] ret = new Object[] {i, date, hour, min, sec};
1825 i += delta;
1826 sec += delta;
1827
1828 if (sec >= 60) {
1829 min++;
1830 sec -= 60;
1831
1832 if (min == 60) {
1833 hour++;
1834 min = 0;
1840 }
1841
1842 if (i == 0) {
1843 date = date.plusDays(1);
1844 }
1845
1846 return ret;
1847 }
1848
1849 public void remove() {
1850 throw new UnsupportedOperationException();
1851 }
1852 };
1853 }
1854
1855 @Test(dataProvider="plusSeconds_fromZero", groups={"tck"})
1856 public void test_plusSeconds_fromZero(int seconds, LocalDate date, int hour, int min, int sec) {
1857 LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
1858 LocalDateTime t = base.plusSeconds(seconds);
1859
1860 assertEquals(date, t.toLocalDate());
1861 assertEquals(hour, t.getHour());
1862 assertEquals(min, t.getMinute());
1863 assertEquals(sec, t.getSecond());
1864 }
1865
1866 @Test(groups={"tck"})
1867 public void test_plusSeconds_noChange_oneDay() {
1868 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plusSeconds(24 * 60 * 60);
1869 assertEquals(t.toLocalDate(), TEST_2007_07_15_12_30_40_987654321.toLocalDate().plusDays(1));
1870 }
1871
1872 //-----------------------------------------------------------------------
1873 // plusNanos()
1874 //-----------------------------------------------------------------------
1875 @Test(groups={"tck"})
1876 public void test_plusNanos_halfABillion() {
1877 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
1878 LocalDate d = t.toLocalDate();
1879
1880 int hour = 0;
1881 int min = 0;
1882 int sec = 0;
1883 int nanos = 0;
1884
1885 for (long i = 0; i < 3700 * 1000000000L; i+= 500000000) {
1886 t = t.plusNanos(500000000);
1887 nanos += 500000000;
1888 if (nanos == 1000000000) {
1889 sec++;
1890 nanos = 0;
1891 }
1892 if (sec == 60) {
1893 min++;
1894 sec = 0;
1895 }
1896 if (min == 60) {
1897 hour++;
1898 min = 0;
1899 }
1900
1901 assertEquals(t.toLocalDate(), d, String.valueOf(i));
1902 assertEquals(t.getHour(), hour);
1903 assertEquals(t.getMinute(), min);
1904 assertEquals(t.getSecond(), sec);
1905 assertEquals(t.getNano(), nanos);
1906 }
1907 }
1908
1909 @DataProvider(name="plusNanos_fromZero")
1910 Iterator<Object[]> plusNanos_fromZero() {
1911 return new Iterator<Object[]>() {
1912 long delta = 7500000000L;
1913
1914 long i = -3660 * 1000000000L;
1915 LocalDate date = TEST_2007_07_15_12_30_40_987654321.toLocalDate().minusDays(1);
1916 int hour = 22;
1917 int min = 59;
1918 int sec = 0;
1919 long nanos = 0;
1920
1921 public boolean hasNext() {
1922 return i <= 3660 * 1000000000L;
1923 }
1924
1925 public Object[] next() {
1926 final Object[] ret = new Object[] {i, date, hour, min, sec, (int)nanos};
1927 i += delta;
1928 nanos += delta;
1929
1930 if (nanos >= 1000000000L) {
1931 sec += nanos / 1000000000L;
1932 nanos %= 1000000000L;
1933
1934 if (sec >= 60) {
1935 min++;
1944 date = date.plusDays(1);
1945 }
1946 }
1947 }
1948 }
1949
1950 return ret;
1951 }
1952
1953 public void remove() {
1954 throw new UnsupportedOperationException();
1955 }
1956 };
1957 }
1958
1959 @Test(dataProvider="plusNanos_fromZero", groups={"tck"})
1960 public void test_plusNanos_fromZero(long nanoseconds, LocalDate date, int hour, int min, int sec, int nanos) {
1961 LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
1962 LocalDateTime t = base.plusNanos(nanoseconds);
1963
1964 assertEquals(date, t.toLocalDate());
1965 assertEquals(hour, t.getHour());
1966 assertEquals(min, t.getMinute());
1967 assertEquals(sec, t.getSecond());
1968 assertEquals(nanos, t.getNano());
1969 }
1970
1971 @Test(groups={"tck"})
1972 public void test_plusNanos_noChange_oneDay() {
1973 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.plusNanos(24 * 60 * 60 * 1000000000L);
1974 assertEquals(t.toLocalDate(), TEST_2007_07_15_12_30_40_987654321.toLocalDate().plusDays(1));
1975 }
1976
1977 //-----------------------------------------------------------------------
1978 // minus(TemporalAmount)
1979 //-----------------------------------------------------------------------
1980 @Test
1981 public void test_minus_TemporalAmount_positiveMonths() {
1982 MockSimplePeriod period = MockSimplePeriod.of(7, MONTHS);
1983 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minus(period);
1984 assertEquals(t, LocalDateTime.of(2006, 12, 15, 12, 30, 40, 987654321));
1985 }
1986
1987 @Test
1988 public void test_minus_TemporalAmount_negativeDays() {
1989 MockSimplePeriod period = MockSimplePeriod.of(-25, DAYS);
1990 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minus(period);
1991 assertEquals(t, LocalDateTime.of(2007, 8, 9, 12, 30, 40, 987654321));
1992 }
1993
1994 @Test(expectedExceptions=DateTimeException.class)
1995 public void test_minus_TemporalAmount_invalidTooLarge() {
1996 MockSimplePeriod period = MockSimplePeriod.of(-1, YEARS);
1997 LocalDateTime.of(Year.MAX_VALUE, 1, 1, 0, 0).minus(period);
1998 }
1999
2000 @Test(expectedExceptions=DateTimeException.class)
2001 public void test_minus_TemporalAmount_invalidTooSmall() {
2002 MockSimplePeriod period = MockSimplePeriod.of(1, YEARS);
2003 LocalDateTime.of(Year.MIN_VALUE, 1, 1, 0, 0).minus(period);
2004 }
2005
2006 @Test(expectedExceptions=NullPointerException.class)
2007 public void test_minus_TemporalAmount_null() {
2008 TEST_2007_07_15_12_30_40_987654321.minus((TemporalAmount) null);
2009 }
2010
2011 //-----------------------------------------------------------------------
2012 // minus(long,TemporalUnit)
2013 //-----------------------------------------------------------------------
2014 @Test(groups={"tck"})
2015 public void test_minus_longTemporalUnit_positiveMonths() {
2016 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minus(7, ChronoUnit.MONTHS);
2017 assertEquals(t, LocalDateTime.of(2006, 12, 15, 12, 30, 40, 987654321));
2018 }
2019
2020 @Test(groups={"tck"})
2021 public void test_minus_longTemporalUnit_negativeDays() {
2022 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minus(-25, ChronoUnit.DAYS);
2023 assertEquals(t, LocalDateTime.of(2007, 8, 9, 12, 30, 40, 987654321));
2024 }
2025
2026 @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
2027 public void test_minus_longTemporalUnit_null() {
2028 TEST_2007_07_15_12_30_40_987654321.minus(1, (TemporalUnit) null);
2029 }
2030
2343 public void test_minusDays_invalidTooSmall() {
2344 createDateMidnight(Year.MIN_VALUE, 1, 1).minusDays(1);
2345 }
2346
2347 @Test(expectedExceptions=ArithmeticException.class, groups={"tck"})
2348 public void test_minusDays_overflowTooLarge() {
2349 createDateMidnight(Year.MAX_VALUE, 12, 31).minusDays(Long.MIN_VALUE);
2350 }
2351
2352 @Test(expectedExceptions=ArithmeticException.class, groups={"tck"})
2353 public void test_minusDays_overflowTooSmall() {
2354 createDateMidnight(Year.MIN_VALUE, 1, 1).minusDays(Long.MAX_VALUE);
2355 }
2356
2357 //-----------------------------------------------------------------------
2358 // minusHours()
2359 //-----------------------------------------------------------------------
2360 @Test(groups={"tck"})
2361 public void test_minusHours_one() {
2362 LocalDateTime t =TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
2363 LocalDate d = t.toLocalDate();
2364
2365 for (int i = 0; i < 50; i++) {
2366 t = t.minusHours(1);
2367
2368 if (i % 24 == 0) {
2369 d = d.minusDays(1);
2370 }
2371
2372 assertEquals(t.toLocalDate(), d);
2373 assertEquals(t.getHour(), (((-i + 23) % 24) + 24) % 24);
2374 }
2375 }
2376
2377 @Test(groups={"tck"})
2378 public void test_minusHours_fromZero() {
2379 LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
2380 LocalDate d = base.toLocalDate().plusDays(2);
2381 LocalTime t = LocalTime.of(3, 0);
2382
2383 for (int i = -50; i < 50; i++) {
2384 LocalDateTime dt = base.minusHours(i);
2385 t = t.minusHours(1);
2386
2387 if (t.getHour() == 23) {
2388 d = d.minusDays(1);
2389 }
2390
2391 assertEquals(dt.toLocalDate(), d, String.valueOf(i));
2392 assertEquals(dt.toLocalTime(), t);
2393 }
2394 }
2395
2396 @Test(groups={"tck"})
2397 public void test_minusHours_fromOne() {
2398 LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.of(1, 0));
2399 LocalDate d = base.toLocalDate().plusDays(2);
2400 LocalTime t = LocalTime.of(4, 0);
2401
2402 for (int i = -50; i < 50; i++) {
2403 LocalDateTime dt = base.minusHours(i);
2404
2405 t = t.minusHours(1);
2406
2407 if (t.getHour() == 23) {
2408 d = d.minusDays(1);
2409 }
2410
2411 assertEquals(dt.toLocalDate(), d, String.valueOf(i));
2412 assertEquals(dt.toLocalTime(), t);
2413 }
2414 }
2415
2416 //-----------------------------------------------------------------------
2417 // minusMinutes()
2418 //-----------------------------------------------------------------------
2419 @Test(groups={"tck"})
2420 public void test_minusMinutes_one() {
2421 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
2422 LocalDate d = t.toLocalDate().minusDays(1);
2423
2424 int hour = 0;
2425 int min = 0;
2426
2427 for (int i = 0; i < 70; i++) {
2428 t = t.minusMinutes(1);
2429 min--;
2430 if (min == -1) {
2431 hour--;
2432 min = 59;
2433
2434 if (hour == -1) {
2435 hour = 23;
2436 }
2437 }
2438 assertEquals(t.toLocalDate(), d);
2439 assertEquals(t.getHour(), hour);
2440 assertEquals(t.getMinute(), min);
2441 }
2442 }
2443
2444 @Test(groups={"tck"})
2445 public void test_minusMinutes_fromZero() {
2446 LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
2447 LocalDate d = base.toLocalDate().minusDays(1);
2448 LocalTime t = LocalTime.of(22, 49);
2449
2450 for (int i = 70; i > -70; i--) {
2451 LocalDateTime dt = base.minusMinutes(i);
2452 t = t.plusMinutes(1);
2453
2454 if (t == LocalTime.MIDNIGHT) {
2455 d = d.plusDays(1);
2456 }
2457
2458 assertEquals(dt.toLocalDate(), d);
2459 assertEquals(dt.toLocalTime(), t);
2460 }
2461 }
2462
2463 @Test(groups={"tck"})
2464 public void test_minusMinutes_noChange_oneDay() {
2465 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.minusMinutes(24 * 60);
2466 assertEquals(t.toLocalDate(), TEST_2007_07_15_12_30_40_987654321.toLocalDate().minusDays(1));
2467 }
2468
2469 //-----------------------------------------------------------------------
2470 // minusSeconds()
2471 //-----------------------------------------------------------------------
2472 @Test(groups={"tck"})
2473 public void test_minusSeconds_one() {
2474 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
2475 LocalDate d = t.toLocalDate().minusDays(1);
2476
2477 int hour = 0;
2478 int min = 0;
2479 int sec = 0;
2480
2481 for (int i = 0; i < 3700; i++) {
2482 t = t.minusSeconds(1);
2483 sec--;
2484 if (sec == -1) {
2485 min--;
2486 sec = 59;
2487
2488 if (min == -1) {
2489 hour--;
2490 min = 59;
2491
2492 if (hour == -1) {
2493 hour = 23;
2494 }
2495 }
2496 }
2497
2498 assertEquals(t.toLocalDate(), d);
2499 assertEquals(t.getHour(), hour);
2500 assertEquals(t.getMinute(), min);
2501 assertEquals(t.getSecond(), sec);
2502 }
2503 }
2504
2505 @DataProvider(name="minusSeconds_fromZero")
2506 Iterator<Object[]> minusSeconds_fromZero() {
2507 return new Iterator<Object[]>() {
2508 int delta = 30;
2509
2510 int i = 3660;
2511 LocalDate date = TEST_2007_07_15_12_30_40_987654321.toLocalDate().minusDays(1);
2512 int hour = 22;
2513 int min = 59;
2514 int sec = 0;
2515
2516 public boolean hasNext() {
2517 return i >= -3660;
2518 }
2519
2520 public Object[] next() {
2521 final Object[] ret = new Object[] {i, date, hour, min, sec};
2522 i -= delta;
2523 sec += delta;
2524
2525 if (sec >= 60) {
2526 min++;
2527 sec -= 60;
2528
2529 if (min == 60) {
2530 hour++;
2531 min = 0;
2537 }
2538
2539 if (i == 0) {
2540 date = date.plusDays(1);
2541 }
2542
2543 return ret;
2544 }
2545
2546 public void remove() {
2547 throw new UnsupportedOperationException();
2548 }
2549 };
2550 }
2551
2552 @Test(dataProvider="minusSeconds_fromZero", groups={"tck"})
2553 public void test_minusSeconds_fromZero(int seconds, LocalDate date, int hour, int min, int sec) {
2554 LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
2555 LocalDateTime t = base.minusSeconds(seconds);
2556
2557 assertEquals(date, t.toLocalDate());
2558 assertEquals(hour, t.getHour());
2559 assertEquals(min, t.getMinute());
2560 assertEquals(sec, t.getSecond());
2561 }
2562
2563 //-----------------------------------------------------------------------
2564 // minusNanos()
2565 //-----------------------------------------------------------------------
2566 @Test(groups={"tck"})
2567 public void test_minusNanos_halfABillion() {
2568 LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
2569 LocalDate d = t.toLocalDate().minusDays(1);
2570
2571 int hour = 0;
2572 int min = 0;
2573 int sec = 0;
2574 int nanos = 0;
2575
2576 for (long i = 0; i < 3700 * 1000000000L; i+= 500000000) {
2577 t = t.minusNanos(500000000);
2578 nanos -= 500000000;
2579
2580 if (nanos < 0) {
2581 sec--;
2582 nanos += 1000000000;
2583
2584 if (sec == -1) {
2585 min--;
2586 sec += 60;
2587
2588 if (min == -1) {
2589 hour--;
2590 min += 60;
2591
2592 if (hour == -1) {
2593 hour += 24;
2594 }
2595 }
2596 }
2597 }
2598
2599 assertEquals(t.toLocalDate(), d);
2600 assertEquals(t.getHour(), hour);
2601 assertEquals(t.getMinute(), min);
2602 assertEquals(t.getSecond(), sec);
2603 assertEquals(t.getNano(), nanos);
2604 }
2605 }
2606
2607 @DataProvider(name="minusNanos_fromZero")
2608 Iterator<Object[]> minusNanos_fromZero() {
2609 return new Iterator<Object[]>() {
2610 long delta = 7500000000L;
2611
2612 long i = 3660 * 1000000000L;
2613 LocalDate date = TEST_2007_07_15_12_30_40_987654321.toLocalDate().minusDays(1);
2614 int hour = 22;
2615 int min = 59;
2616 int sec = 0;
2617 long nanos = 0;
2618
2619 public boolean hasNext() {
2620 return i >= -3660 * 1000000000L;
2621 }
2622
2623 public Object[] next() {
2624 final Object[] ret = new Object[] {i, date, hour, min, sec, (int)nanos};
2625 i -= delta;
2626 nanos += delta;
2627
2628 if (nanos >= 1000000000L) {
2629 sec += nanos / 1000000000L;
2630 nanos %= 1000000000L;
2631
2632 if (sec >= 60) {
2633 min++;
2642 date = date.plusDays(1);
2643 }
2644 }
2645 }
2646 }
2647
2648 return ret;
2649 }
2650
2651 public void remove() {
2652 throw new UnsupportedOperationException();
2653 }
2654 };
2655 }
2656
2657 @Test(dataProvider="minusNanos_fromZero", groups={"tck"})
2658 public void test_minusNanos_fromZero(long nanoseconds, LocalDate date, int hour, int min, int sec, int nanos) {
2659 LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
2660 LocalDateTime t = base.minusNanos(nanoseconds);
2661
2662 assertEquals(date, t.toLocalDate());
2663 assertEquals(hour, t.getHour());
2664 assertEquals(min, t.getMinute());
2665 assertEquals(sec, t.getSecond());
2666 assertEquals(nanos, t.getNano());
2667 }
2668
2669 //-----------------------------------------------------------------------
2670 // atOffset()
2671 //-----------------------------------------------------------------------
2672 @Test(groups={"tck"})
2673 public void test_atOffset() {
2674 LocalDateTime t = LocalDateTime.of(2008, 6, 30, 11, 30);
2675 assertEquals(t.atOffset(OFFSET_PTWO), OffsetDateTime.of(LocalDateTime.of(2008, 6, 30, 11, 30), OFFSET_PTWO));
2676 }
2677
2678 @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
2679 public void test_atOffset_nullZoneOffset() {
2680 LocalDateTime t = LocalDateTime.of(2008, 6, 30, 11, 30);
2681 t.atOffset((ZoneOffset) null);
2682 }
2974 {2008, 7, 5, 2, 1, 0, 0, "2008-07-05T02:01"},
2975 {2007, 12, 31, 23, 59, 1, 0, "2007-12-31T23:59:01"},
2976 {999, 12, 31, 23, 59, 59, 990000000, "0999-12-31T23:59:59.990"},
2977 {-1, 1, 2, 23, 59, 59, 999990000, "-0001-01-02T23:59:59.999990"},
2978 {-2008, 1, 2, 23, 59, 59, 999999990, "-2008-01-02T23:59:59.999999990"},
2979 };
2980 }
2981
2982 @Test(dataProvider="sampleToString", groups={"tck"})
2983 public void test_toString(int y, int m, int d, int h, int mi, int s, int n, String expected) {
2984 LocalDateTime t = LocalDateTime.of(y, m, d, h, mi, s, n);
2985 String str = t.toString();
2986 assertEquals(str, expected);
2987 }
2988
2989 //-----------------------------------------------------------------------
2990 // toString(DateTimeFormatter)
2991 //-----------------------------------------------------------------------
2992 @Test(groups={"tck"})
2993 public void test_toString_formatter() {
2994 DateTimeFormatter f = DateTimeFormatter.ofPattern("y M d H m s");
2995 String t = LocalDateTime.of(2010, 12, 3, 11, 30, 45).toString(f);
2996 assertEquals(t, "2010 12 3 11 30 45");
2997 }
2998
2999 @Test(expectedExceptions=NullPointerException.class, groups={"tck"})
3000 public void test_toString_formatter_null() {
3001 LocalDateTime.of(2010, 12, 3, 11, 30, 45).toString(null);
3002 }
3003
3004 }
|