/** * Obtains an instance of {@code LocalDate} from a year and day-of-year. * <p> * The day-of-year must be valid for the year, otherwise an exception will be thrown. * * @param year the year to represent, from MIN_YEAR to MAX_YEAR * @param dayOfYear the day-of-year to represent, from 1 to 366 * @return the local date, not null * @throws DateTimeException if the value of any field is out of range * @throws DateTimeException if the day-of-year is invalid for the month-year */ public static LocalDate ofYearDay(int year, int dayOfYear) { YEAR.checkValidValue(year); DAY_OF_YEAR.checkValidValue(dayOfYear); boolean leap = ISOChrono.INSTANCE.isLeapYear(year); if (dayOfYear == 366 && leap == false) { throw new DateTimeException("Invalid date 'DayOfYear 366' as '" + year + "' is not a leap year"); } Month moy = Month.of((dayOfYear - 1) / 31 + 1); int monthEnd = moy.firstDayOfYear(leap) + moy.length(leap) - 1; if (dayOfYear > monthEnd) { moy = moy.plus(1); } int dom = dayOfYear - moy.firstDayOfYear(leap) + 1; return create(year, moy, dom); }
/** * Resolves the date, resolving days past the end of month. * * @param year the year to represent, validated from MIN_YEAR to MAX_YEAR * @param month the month-of-year to represent, validated from 1 to 12 * @param dayOfMonth the day-of-month to represent, validated from 1 to 31 * @return the resolved date, not null */ private static LocalDate resolvePreviousValid(int year, int month, int day) { switch (month) { case 2: day = Math.min(day, ISOChrono.INSTANCE.isLeapYear(year) ? 29 : 28); break; case 4: case 6: case 9: case 11: day = Math.min(day, 30); break; } return LocalDate.of(year, month, day); }
/** * Creates a transition instance for the specified year. * <p> * Calculations are performed using the ISO-8601 chronology. * * @param year the year to create a transition for, not null * @return the transition instance, not null */ public ZoneOffsetTransition createTransition(int year) { LocalDate date; if (this.dom < 0) { date = LocalDate.of(year, this.month, this.month.length(ISOChrono.INSTANCE.isLeapYear(year)) + 1 + this.dom); if (this.dow != null) { date = date.with(previousOrSame(this.dow)); } } else { date = LocalDate.of(year, this.month, this.dom); if (this.dow != null) { date = date.with(nextOrSame(this.dow)); } } if (this.timeEndOfDay) { date = date.plusDays(1); } LocalDateTime localDT = LocalDateTime.of(date, this.time); LocalDateTime transition = this.timeDefinition.createDateTime(localDT, this.standardOffset, this.offsetBefore); return new ZoneOffsetTransition(transition, this.offsetBefore, this.offsetAfter); }
private LocalDate toLocalDate() { LocalDate date; if (this.dayOfMonthIndicator < 0) { int monthLen = this.month.length(ISOChrono.INSTANCE.isLeapYear(this.year)); date = LocalDate.of(this.year, this.month, monthLen + 1 + this.dayOfMonthIndicator); if (this.dayOfWeek != null) { date = date.with(previousOrSame(this.dayOfWeek)); } } else { date = LocalDate.of(this.year, this.month, this.dayOfMonthIndicator); if (this.dayOfWeek != null) { date = date.with(nextOrSame(this.dayOfWeek)); } } if (this.timeEndOfDay) { date = date.plusDays(1); } return date; }
@DataProvider(name = "samples") Object[][] data_samples() { return new Object[][] { { ISOChrono.INSTANCE.date(1, 7, 8), LocalDate.of(1, 7, 8) }, { ISOChrono.INSTANCE.date(1, 7, 20), LocalDate.of(1, 7, 20) }, { ISOChrono.INSTANCE.date(1, 7, 21), LocalDate.of(1, 7, 21) }, { ISOChrono.INSTANCE.date(2, 7, 8), LocalDate.of(2, 7, 8) }, { ISOChrono.INSTANCE.date(3, 6, 27), LocalDate.of(3, 6, 27) }, { ISOChrono.INSTANCE.date(3, 5, 23), LocalDate.of(3, 5, 23) }, { ISOChrono.INSTANCE.date(4, 6, 16), LocalDate.of(4, 6, 16) }, { ISOChrono.INSTANCE.date(4, 7, 3), LocalDate.of(4, 7, 3) }, { ISOChrono.INSTANCE.date(4, 7, 4), LocalDate.of(4, 7, 4) }, { ISOChrono.INSTANCE.date(5, 1, 1), LocalDate.of(5, 1, 1) }, { ISOChrono.INSTANCE.date(1727, 3, 3), LocalDate.of(1727, 3, 3) }, { ISOChrono.INSTANCE.date(1728, 10, 28), LocalDate.of(1728, 10, 28) }, { ISOChrono.INSTANCE.date(2012, 10, 29), LocalDate.of(2012, 10, 29) }, }; }
@Test(groups = "tck") public void test_date_withEra() { int year = 5; int month = 5; int dayOfMonth = 5; ChronoLocalDate<ISOChrono> test = ISOChrono.INSTANCE.date(ERA_BCE, year, month, dayOfMonth); assertEquals(test.getEra(), ERA_BCE); assertEquals(test.get(ChronoField.YEAR_OF_ERA), year); assertEquals(test.get(ChronoField.MONTH_OF_YEAR), month); assertEquals(test.get(ChronoField.DAY_OF_MONTH), dayOfMonth); assertEquals(test.get(YEAR), 1 + (-1 * year)); assertEquals(test.get(ERA), 0); assertEquals(test.get(YEAR_OF_ERA), year); }
/** * Creates a local date from the year, month and day fields. * * @param year the year to represent, validated from MIN_YEAR to MAX_YEAR * @param month the month-of-year to represent, validated not null * @param dayOfMonth the day-of-month to represent, validated from 1 to 31 * @return the local date, not null * @throws DateTimeException if the day-of-month is invalid for the month-year */ private static LocalDate create(int year, Month month, int dayOfMonth) { if (dayOfMonth > 28 && dayOfMonth > month.length(ISOChrono.INSTANCE.isLeapYear(year))) { if (dayOfMonth == 29) { throw new DateTimeException("Invalid date 'February 29' as '" + year + "' is not a leap year"); } else { throw new DateTimeException("Invalid date '" + month.name() + " " + dayOfMonth + "'"); } } return new LocalDate(year, month.getValue(), dayOfMonth); }
@BeforeMethod(groups = "tck") public void setUp() { // Ensure each of the classes are initialized (until initialization is fixed) Chrono<?> c; c = HijrahChrono.INSTANCE; c = ISOChrono.INSTANCE; c = JapaneseChrono.INSTANCE; c = MinguoChrono.INSTANCE; c = ThaiBuddhistChrono.INSTANCE; c.toString(); // avoids variable being marked as unused }
@DataProvider(name = "calendarsystemtype") Object[][] data_CalendarType() { return new Object[][] { { HijrahChrono.INSTANCE, "islamicc" }, { ISOChrono.INSTANCE, "iso8601" }, { JapaneseChrono.INSTANCE, "japanese" }, { MinguoChrono.INSTANCE, "roc" }, { ThaiBuddhistChrono.INSTANCE, "buddhist" }, }; }
@Test(groups = { "tck" }) public void test_chrono_byName() { Chrono<ISOChrono> c = ISOChrono.INSTANCE; Chrono<?> test = Chrono.of("ISO"); Assert.assertNotNull(test, "The ISO calendar could not be found byName"); Assert.assertEquals(test.getId(), "ISO", "ID mismatch"); Assert.assertEquals(test.getCalendarType(), "iso8601", "Type mismatch"); Assert.assertEquals(test, c); }
@Test(groups = { "tck" }) public void test_adjust1() { ChronoLocalDate<ISOChrono> base = ISOChrono.INSTANCE.date(1728, 10, 28); ChronoLocalDate<ISOChrono> test = base.with(DateTimeAdjusters.lastDayOfMonth()); assertEquals(test, ISOChrono.INSTANCE.date(1728, 10, 31)); }
@Test(groups = { "tck" }) public void test_adjust2() { ChronoLocalDate<ISOChrono> base = ISOChrono.INSTANCE.date(1728, 12, 2); ChronoLocalDate<ISOChrono> test = base.with(DateTimeAdjusters.lastDayOfMonth()); assertEquals(test, ISOChrono.INSTANCE.date(1728, 12, 31)); }
@Test(groups = { "tck" }) public void test_adjust_toLocalDate() { ChronoLocalDate<ISOChrono> isoDate = ISOChrono.INSTANCE.date(1726, 1, 4); ChronoLocalDate<ISOChrono> test = isoDate.with(LocalDate.of(2012, 7, 6)); assertEquals(test, ISOChrono.INSTANCE.date(2012, 7, 6)); }
@Test(groups = { "tck" }) public void test_LocalDate_adjustToISODate() { ChronoLocalDate<ISOChrono> isoDate = ISOChrono.INSTANCE.date(1728, 10, 29); LocalDate test = LocalDate.MIN_DATE.with(isoDate); assertEquals(test, LocalDate.of(1728, 10, 29)); }
@Test(groups = { "tck" }) public void test_LocalDateTime_adjustToISODate() { ChronoLocalDate<ISOChrono> isoDate = ISOChrono.INSTANCE.date(1728, 10, 29); LocalDateTime test = LocalDateTime.MIN_DATE_TIME.with(isoDate); assertEquals(test, LocalDateTime.of(1728, 10, 29, 0, 0)); }
@DataProvider(name = "toString") Object[][] data_toString() { return new Object[][] { { ISOChrono.INSTANCE.date(1, 1, 1), "0001-01-01" }, { ISOChrono.INSTANCE.date(1728, 10, 28), "1728-10-28" }, { ISOChrono.INSTANCE.date(1728, 10, 29), "1728-10-29" }, { ISOChrono.INSTANCE.date(1727, 12, 5), "1727-12-05" }, { ISOChrono.INSTANCE.date(1727, 12, 6), "1727-12-06" }, }; }
@Test(groups = { "tck" }) public void test_query_chrono() { assertEquals(this.TEST_DATE_TIME.query(Query.CHRONO), ISOChrono.INSTANCE); }
@Test(groups = { "tck" }) public void test_query_chrono() { assertEquals(this.TEST_2008_6_30_11_30_59_000000500.query(Query.CHRONO), ISOChrono.INSTANCE); }
@Test(groups = { "tck" }) public void test_query_chrono() { assertEquals(this.TEST_2007_07_15_12_30_40_987654321.query(Query.CHRONO), ISOChrono.INSTANCE); }
@Test(groups = { "tck" }) public void test_query_chrono() { assertEquals(this.TEST_2007_07_15.query(Query.CHRONO), ISOChrono.INSTANCE); }
@Test(groups = { "tck" }) public void test_query_chrono() { assertEquals(this.TEST_2007_07_15_PONE.query(Query.CHRONO), ISOChrono.INSTANCE); }
@Test(groups = { "tck" }) public void test_query_chrono() { assertEquals(Month.JUNE.query(Query.CHRONO), ISOChrono.INSTANCE); }
@DataProvider(name = "calendars") Chrono[][] data_of_calendars() { return new Chrono[][] { { HijrahChrono.INSTANCE }, { ISOChrono.INSTANCE }, { JapaneseChrono.INSTANCE }, { MinguoChrono.INSTANCE }, { ThaiBuddhistChrono.INSTANCE }, }; }
@Test(groups = { "tck" }, dataProvider = "calendars") public void test_zonedDateTime_comparisons(Chrono chrono) { List<ChronoZonedDateTime<?>> dates = new ArrayList<>(); ChronoZonedDateTime<?> date = chrono.date(LocalDate.of(1900, 1, 1)).atTime(LocalTime.MIN_TIME) .atZone(ZoneOffset.UTC); // Insert dates in order, no duplicates dates.add(date.minus(100, ChronoUnit.YEARS)); dates.add(date.minus(1, ChronoUnit.YEARS)); dates.add(date.minus(1, ChronoUnit.MONTHS)); dates.add(date.minus(1, ChronoUnit.WEEKS)); dates.add(date.minus(1, ChronoUnit.DAYS)); dates.add(date.minus(1, ChronoUnit.HOURS)); dates.add(date.minus(1, ChronoUnit.MINUTES)); dates.add(date.minus(1, ChronoUnit.SECONDS)); dates.add(date.minus(1, ChronoUnit.NANOS)); dates.add(date); dates.add(date.plus(1, ChronoUnit.NANOS)); dates.add(date.plus(1, ChronoUnit.SECONDS)); dates.add(date.plus(1, ChronoUnit.MINUTES)); dates.add(date.plus(1, ChronoUnit.HOURS)); dates.add(date.plus(1, ChronoUnit.DAYS)); dates.add(date.plus(1, ChronoUnit.WEEKS)); dates.add(date.plus(1, ChronoUnit.MONTHS)); dates.add(date.plus(1, ChronoUnit.YEARS)); dates.add(date.plus(100, ChronoUnit.YEARS)); // Check these dates against the corresponding dates for every calendar for (Chrono[] clist : data_of_calendars()) { List<ChronoZonedDateTime<?>> otherDates = new ArrayList<>(); Chrono chrono2 = ISOChrono.INSTANCE; // clist[0]; for (ChronoZonedDateTime<?> d : dates) { otherDates.add(chrono2.date(d).atTime(d.getTime()).atZone(d.getZone())); } // Now compare the sequence of original dates with the sequence of converted dates for (int i = 0; i < dates.size(); i++) { ChronoZonedDateTime<?> a = dates.get(i); for (int j = 0; j < otherDates.size(); j++) { ChronoZonedDateTime<?> b = otherDates.get(j); int cmp = ChronoZonedDateTime.INSTANT_COMPARATOR.compare(a, b); if (i < j) { assertTrue(cmp < 0, a + " compare " + b); assertEquals(a.isBefore(b), true, a + " isBefore " + b); assertEquals(a.isAfter(b), false, a + " ifAfter " + b); assertEquals(a.isEqual(b), false, a + " isEqual " + b); } else if (i > j) { assertTrue(cmp > 0, a + " compare " + b); assertEquals(a.isBefore(b), false, a + " isBefore " + b); assertEquals(a.isAfter(b), true, a + " ifAfter " + b); assertEquals(a.isEqual(b), false, a + " isEqual " + b); } else { assertTrue(cmp == 0, a + " compare " + b); assertEquals(a.isBefore(b), false, a + " isBefore " + b); assertEquals(a.isAfter(b), false, a + " ifAfter " + b); assertEquals(a.isEqual(b), true, a + " isEqual " + b); } } } } }
@DataProvider(name = "calendars") Chrono[][] data_of_calendars() { return new Chrono[][] { { ISOChrono.INSTANCE }, }; }
@Test(groups = "tck") public void test_equals_false() { assertFalse(ThaiBuddhistChrono.INSTANCE.equals(ISOChrono.INSTANCE)); }
@Test(groups = "tck") public void test_equals_false() { assertFalse(HijrahChrono.INSTANCE.equals(ISOChrono.INSTANCE)); }
@DataProvider(name = "calendars") Chrono[][] data_of_calendars() { return new Chrono[][] { { HijrahChrono.INSTANCE }, { ISOChrono.INSTANCE }, { JapaneseChrono.INSTANCE }, { MinguoChrono.INSTANCE }, { ThaiBuddhistChrono.INSTANCE } }; }
@Test(groups = "tck") public void test_equals_false() { assertFalse(MinguoChrono.INSTANCE.equals(ISOChrono.INSTANCE)); }
@Test(groups = "tck") public void test_equals_false() { assertFalse(JapaneseChrono.INSTANCE.equals(ISOChrono.INSTANCE)); }
@Test(groups = "tck") public void instanceNotNull() { assertNotNull(ISOChrono.INSTANCE); }
@Test(groups = "tck") public void test_eraOf() { assertEquals(ISOChrono.INSTANCE.eraOf(0), ERA_BCE); assertEquals(ISOChrono.INSTANCE.eraOf(1), ERA_CE); }