@Test(dataProvider = "signStyle") public void test_signStyle(LocalDate localDate, SignStyle style, Class<?> expectedEx, String expectedStr) { DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder(); DateTimeFormatter formatter = builder.appendValue(ChronoField.YEAR, 2, 4, style) .toFormatter(); formatter = formatter.withZone(ZoneOffset.UTC); if (expectedEx == null) { String output = formatter.format(localDate); assertEquals(output, expectedStr); } else { try { formatter.format(localDate); fail(); } catch (Exception ex) { assertTrue(expectedEx.isInstance(ex)); } } }
@Test(dataProvider = "resolve_yd") public void test_resolve_yd_strict(int y, int d, MinguoDate expected, boolean smart, boolean strict) { Map<TemporalField, Long> fieldValues = new HashMap<>(); fieldValues.put(ChronoField.YEAR, (long) y); fieldValues.put(ChronoField.DAY_OF_YEAR, (long) d); if (strict) { MinguoDate date = MinguoChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.STRICT); assertEquals(date, expected); assertEquals(fieldValues.size(), 0); } else { try { MinguoChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.STRICT); fail("Should have failed"); } catch (DateTimeException ex) { // expected } } }
@Test(dataProvider = "resolve_ymd") public void test_resolve_ymd_strict(int y, int m, int d, LocalDate expected, Object smart, boolean strict) { Map<TemporalField, Long> fieldValues = new HashMap<>(); fieldValues.put(ChronoField.YEAR, (long) y); fieldValues.put(ChronoField.MONTH_OF_YEAR, (long) m); fieldValues.put(ChronoField.DAY_OF_MONTH, (long) d); if (strict) { LocalDate date = IsoChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.STRICT); assertEquals(date, expected); assertEquals(fieldValues.size(), 0); } else { try { IsoChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.STRICT); fail("Should have failed"); } catch (DateTimeException ex) { // expected } } }
@Test(dataProvider = "resolve_yd") public void test_resolve_yd_smart(int y, int d, JapaneseDate expected, boolean smart, boolean strict) { Map<TemporalField, Long> fieldValues = new HashMap<>(); fieldValues.put(ChronoField.YEAR, (long) y); fieldValues.put(ChronoField.DAY_OF_YEAR, (long) d); if (smart) { JapaneseDate date = JapaneseChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.SMART); assertEquals(date, expected); assertEquals(fieldValues.size(), 0); } else { try { JapaneseChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.SMART); fail("Should have failed"); } catch (DateTimeException ex) { // expected } } }
@Test(dataProvider = "resolve_yearOfEra") public void test_resolve_yearOfEra(ResolverStyle style, Integer e, Integer yoe, Integer y, ChronoField field, Integer expected) { Map<TemporalField, Long> fieldValues = new HashMap<>(); if (e != null) { fieldValues.put(ChronoField.ERA, (long) e); } if (yoe != null) { fieldValues.put(ChronoField.YEAR_OF_ERA, (long) yoe); } if (y != null) { fieldValues.put(ChronoField.YEAR, (long) y); } if (field != null) { MinguoDate date = MinguoChronology.INSTANCE.resolveDate(fieldValues, style); assertEquals(date, null); assertEquals(fieldValues.get(field), (Long) expected.longValue()); assertEquals(fieldValues.size(), 1); } else { try { MinguoChronology.INSTANCE.resolveDate(fieldValues, style); fail("Should have failed"); } catch (DateTimeException ex) { // expected } } }
private int get0(TemporalField field) { switch ((ChronoField) field) { case DAY_OF_WEEK: return getDayOfWeek().getValue(); case ALIGNED_DAY_OF_WEEK_IN_MONTH: return ((day - 1) % 7) + 1; case ALIGNED_DAY_OF_WEEK_IN_YEAR: return ((getDayOfYear() - 1) % 7) + 1; case DAY_OF_MONTH: return day; case DAY_OF_YEAR: return getDayOfYear(); case EPOCH_DAY: throw new UnsupportedTemporalTypeException("Invalid field 'EpochDay' for get() method, use getLong() instead"); case ALIGNED_WEEK_OF_MONTH: return ((day - 1) / 7) + 1; case ALIGNED_WEEK_OF_YEAR: return ((getDayOfYear() - 1) / 7) + 1; case MONTH_OF_YEAR: return month; case PROLEPTIC_MONTH: throw new UnsupportedTemporalTypeException("Invalid field 'ProlepticMonth' for get() method, use getLong() instead"); case YEAR_OF_ERA: return (year >= 1 ? year : 1 - year); case YEAR: return year; case ERA: return (year >= 1 ? 1 : 0); } throw new UnsupportedTemporalTypeException("Unsupported field: " + field); }
@Override public ValueRange range(TemporalField field) { if (field instanceof ChronoField) { if (isSupported(field)) { ChronoField f = (ChronoField) field; switch (f) { case DAY_OF_MONTH: return ValueRange.of(1, lengthOfMonth()); case DAY_OF_YEAR: return ValueRange.of(1, lengthOfYear()); case YEAR_OF_ERA: { Calendar jcal = Calendar.getInstance(JapaneseChronology.LOCALE); jcal.set(Calendar.ERA, era.getValue() + JapaneseEra.ERA_OFFSET); jcal.set(yearOfEra, isoDate.getMonthValue() - 1, isoDate.getDayOfMonth()); return ValueRange.of(1, jcal.getActualMaximum(Calendar.YEAR)); } } return getChronology().range(f); } throw new UnsupportedTemporalTypeException("Unsupported field: " + field); } return field.rangeRefinedBy(this); }
@Override public ValueRange range(ChronoField field) { checkCalendarInit(); if (field instanceof ChronoField) { ChronoField f = field; switch (f) { case DAY_OF_MONTH: return ValueRange.of(1, 1, getMinimumMonthLength(), getMaximumMonthLength()); case DAY_OF_YEAR: return ValueRange.of(1, getMaximumDayOfYear()); case ALIGNED_WEEK_OF_MONTH: return ValueRange.of(1, 5); case YEAR: case YEAR_OF_ERA: return ValueRange.of(getMinimumYear(), getMaximumYear()); case ERA: return ValueRange.of(1, 1); default: return field.range(); } } return field.range(); }
@Override public long getLong(TemporalField field) { if (field instanceof ChronoField) { switch ((ChronoField) field) { case PROLEPTIC_MONTH: return getProlepticMonth(); case YEAR_OF_ERA: { int prolepticYear = getProlepticYear(); return (prolepticYear >= 1 ? prolepticYear : 1 - prolepticYear); } case YEAR: return getProlepticYear(); case ERA: return (getProlepticYear() >= 1 ? 1 : 0); } return isoDate.getLong(field); } return field.getFrom(this); }
public void testCalculateDateTimeDateTime() { DaySimilarity sim = new DaySimilarity(300); DateTimeFormatter formatter = new DateTimeFormatterBuilder() .appendPattern("yyyy-MM-dd") .parseDefaulting(ChronoField.CLOCK_HOUR_OF_DAY, 0) .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0) .parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0) .toFormatter(Locale.ENGLISH); LocalDateTime dt1 = LocalDateTime.parse("2015-01-01", formatter); LocalDateTime dt2 = LocalDateTime.parse("2015-01-31", formatter); LocalDateTime dt3 = LocalDateTime.parse("2015-05-31", formatter); LocalDateTime dt4 = LocalDateTime.parse("2005-01-01", formatter); LocalDateTime dt5 = LocalDateTime.parse("1905-01-01", formatter); assertEquals(1.0, sim.calculate(dt1, dt1)); assertEquals(0.9, sim.calculate(dt1, dt2)); assertEquals(0.9, sim.calculate(dt2, dt1)); assertEquals(0.5, sim.calculate(dt1, dt3)); assertEquals(0.0, sim.calculate(dt1, dt4)); assertEquals(0.0, sim.calculate(dt1, dt5)); }
public void testCalculateDateTimeDateTime() { WeightedDateSimilarity sim = new WeightedDateSimilarity(0, 0, 1); sim.setYearRange(10); DateTimeFormatter formatter = new DateTimeFormatterBuilder() .appendPattern("yyyy-MM-dd") .parseDefaulting(ChronoField.CLOCK_HOUR_OF_DAY, 0) .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0) .parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0) .toFormatter(Locale.ENGLISH); LocalDateTime dt1 = LocalDateTime.parse("2015-01-01", formatter); LocalDateTime dt2 = LocalDateTime.parse("2014-01-01", formatter); LocalDateTime dt3 = LocalDateTime.parse("2010-01-01", formatter); LocalDateTime dt4 = LocalDateTime.parse("2005-01-01", formatter); LocalDateTime dt5 = LocalDateTime.parse("1905-01-01", formatter); assertEquals(1.0, sim.calculate(dt1, dt1)); assertEquals(0.9, sim.calculate(dt1, dt2)); assertEquals(0.9, sim.calculate(dt2, dt1)); assertEquals(0.5, sim.calculate(dt1, dt3)); assertEquals(0.0, sim.calculate(dt1, dt4)); assertEquals(0.0, sim.calculate(dt1, dt5)); }
@Override public ValueRange range(TemporalField field) { if (field instanceof ChronoField) { if (isSupported(field)) { ChronoField f = (ChronoField) field; switch (f) { case DAY_OF_MONTH: return ValueRange.of(1, lengthOfMonth()); case DAY_OF_YEAR: return ValueRange.of(1, lengthOfYear()); case ALIGNED_WEEK_OF_MONTH: return ValueRange.of(1, 5); // TODO // TODO does the limited range of valid years cause years to // start/end part way through? that would affect range } return getChronology().range(f); } throw new UnsupportedTemporalTypeException("Unsupported field: " + field); } return field.rangeRefinedBy(this); }
@Test public void test_getLong_TemporalField() { OffsetDateTime test = OffsetDateTime.of(LocalDate.of(2008, 6, 30), LocalTime.of(12, 30, 40, 987654321), OFFSET_PONE); assertEquals(test.getLong(ChronoField.YEAR), 2008); assertEquals(test.getLong(ChronoField.MONTH_OF_YEAR), 6); assertEquals(test.getLong(ChronoField.DAY_OF_MONTH), 30); assertEquals(test.getLong(ChronoField.DAY_OF_WEEK), 1); assertEquals(test.getLong(ChronoField.DAY_OF_YEAR), 182); assertEquals(test.getLong(ChronoField.HOUR_OF_DAY), 12); assertEquals(test.getLong(ChronoField.MINUTE_OF_HOUR), 30); assertEquals(test.getLong(ChronoField.SECOND_OF_MINUTE), 40); assertEquals(test.getLong(ChronoField.NANO_OF_SECOND), 987654321); assertEquals(test.getLong(ChronoField.HOUR_OF_AMPM), 0); assertEquals(test.getLong(ChronoField.AMPM_OF_DAY), 1); assertEquals(test.getLong(ChronoField.INSTANT_SECONDS), test.toEpochSecond()); assertEquals(test.getLong(ChronoField.OFFSET_SECONDS), 3600); }
@Test(dataProvider = "resolve_ymaa") public void test_resolve_ymaa_lenient(int y, int m, int w, int d, MinguoDate expected, boolean smart, boolean strict) { Map<TemporalField, Long> fieldValues = new HashMap<>(); fieldValues.put(ChronoField.YEAR, (long) y); fieldValues.put(ChronoField.MONTH_OF_YEAR, (long) m); fieldValues.put(ChronoField.ALIGNED_WEEK_OF_MONTH, (long) w); fieldValues.put(ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH, (long) d); MinguoDate date = MinguoChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.LENIENT); assertEquals(date, expected); assertEquals(fieldValues.size(), 0); }
@Test public void testSetGetDateTimeAt() { LocalDateTime date = LocalDateTime.now(); byte[] buffer = new byte[32]; Arrays.fill(buffer, (byte) 0); S7.setDateTimeAt(buffer, 0, date); LocalDateTime dateRet = S7.getDateTimeAt(buffer, 0); assertEquals(date.minusNanos(date.getLong(ChronoField.NANO_OF_SECOND)), dateRet); }
@Test(dataProvider = "fieldAndAccessor") public void test_supportedAccessor(ChronoField field, TemporalAccessor accessor, boolean isSupported, long value) { assertEquals(field.isSupportedBy(accessor), isSupported); if (isSupported) { assertEquals(field.getFrom(accessor), value); } }
private LocalDateTime getDateRelativeToToday(ChronoField field, int buffer) { LocalDateTime date = LocalDateTime.now(); // want a date with a buffer either side of it and also not including the date itself int gap = 2 * buffer + 1; int max = (int)field.range().getSmallestMaximum(); int rand = getRandomInt(1, max - gap); int value = rand >= date.get(field) - buffer ? rand + gap : rand; LocalDateTime relativeDate = date.with(field, value); // Adding logging to help with some spurious errors... log.info("Date: " + date + " rand: " + rand + " value: " + value + " Picked date: " + relativeDate); return relativeDate; }
@Override public ValueRange range(ChronoField field) { switch (field) { case DAY_OF_MONTH: return ValueRange.of(1, 5, 30); case ALIGNED_WEEK_OF_MONTH: return ValueRange.of(1, 1, 5); case MONTH_OF_YEAR: return ValueRange.of(1, 13); case PROLEPTIC_MONTH: return ValueRange.of(-1000, 1000); // TODO case YEAR_OF_ERA: return ValueRange.of(1, 999, 1000); // TODO case YEAR: return ValueRange.of(-1000, 1000); // TODO } return field.range(); }
@Override protected List<TemporalField> invalidFields() { List<TemporalField> list = new ArrayList<>(Arrays.<TemporalField>asList(ChronoField.values())); list.removeAll(validFields()); list.add(JulianFields.JULIAN_DAY); list.add(JulianFields.MODIFIED_JULIAN_DAY); list.add(JulianFields.RATA_DIE); return list; }
@Test public void test_isSupported_TemporalField() { assertEquals(TEST_2007_07_15_12_30_40_987654321.isSupported((TemporalField) null), false); assertEquals(TEST_2007_07_15_12_30_40_987654321.isSupported(ChronoField.NANO_OF_SECOND), true); assertEquals(TEST_2007_07_15_12_30_40_987654321.isSupported(ChronoField.NANO_OF_DAY), true); assertEquals(TEST_2007_07_15_12_30_40_987654321.isSupported(ChronoField.MICRO_OF_SECOND), true); assertEquals(TEST_2007_07_15_12_30_40_987654321.isSupported(ChronoField.MICRO_OF_DAY), true); assertEquals(TEST_2007_07_15_12_30_40_987654321.isSupported(ChronoField.MILLI_OF_SECOND), true); assertEquals(TEST_2007_07_15_12_30_40_987654321.isSupported(ChronoField.MILLI_OF_DAY), true); assertEquals(TEST_2007_07_15_12_30_40_987654321.isSupported(ChronoField.SECOND_OF_MINUTE), true); assertEquals(TEST_2007_07_15_12_30_40_987654321.isSupported(ChronoField.SECOND_OF_DAY), true); assertEquals(TEST_2007_07_15_12_30_40_987654321.isSupported(ChronoField.MINUTE_OF_HOUR), true); assertEquals(TEST_2007_07_15_12_30_40_987654321.isSupported(ChronoField.MINUTE_OF_DAY), true); assertEquals(TEST_2007_07_15_12_30_40_987654321.isSupported(ChronoField.HOUR_OF_AMPM), true); assertEquals(TEST_2007_07_15_12_30_40_987654321.isSupported(ChronoField.CLOCK_HOUR_OF_AMPM), true); assertEquals(TEST_2007_07_15_12_30_40_987654321.isSupported(ChronoField.HOUR_OF_DAY), true); assertEquals(TEST_2007_07_15_12_30_40_987654321.isSupported(ChronoField.CLOCK_HOUR_OF_DAY), true); assertEquals(TEST_2007_07_15_12_30_40_987654321.isSupported(ChronoField.AMPM_OF_DAY), true); assertEquals(TEST_2007_07_15_12_30_40_987654321.isSupported(ChronoField.DAY_OF_WEEK), true); assertEquals(TEST_2007_07_15_12_30_40_987654321.isSupported(ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH), true); assertEquals(TEST_2007_07_15_12_30_40_987654321.isSupported(ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR), true); assertEquals(TEST_2007_07_15_12_30_40_987654321.isSupported(ChronoField.DAY_OF_MONTH), true); assertEquals(TEST_2007_07_15_12_30_40_987654321.isSupported(ChronoField.DAY_OF_YEAR), true); assertEquals(TEST_2007_07_15_12_30_40_987654321.isSupported(ChronoField.EPOCH_DAY), true); assertEquals(TEST_2007_07_15_12_30_40_987654321.isSupported(ChronoField.ALIGNED_WEEK_OF_MONTH), true); assertEquals(TEST_2007_07_15_12_30_40_987654321.isSupported(ChronoField.ALIGNED_WEEK_OF_YEAR), true); assertEquals(TEST_2007_07_15_12_30_40_987654321.isSupported(ChronoField.MONTH_OF_YEAR), true); assertEquals(TEST_2007_07_15_12_30_40_987654321.isSupported(ChronoField.PROLEPTIC_MONTH), true); assertEquals(TEST_2007_07_15_12_30_40_987654321.isSupported(ChronoField.YEAR), true); assertEquals(TEST_2007_07_15_12_30_40_987654321.isSupported(ChronoField.YEAR_OF_ERA), true); assertEquals(TEST_2007_07_15_12_30_40_987654321.isSupported(ChronoField.ERA), true); assertEquals(TEST_2007_07_15_12_30_40_987654321.isSupported(ChronoField.INSTANT_SECONDS), false); assertEquals(TEST_2007_07_15_12_30_40_987654321.isSupported(ChronoField.OFFSET_SECONDS), false); }
@Test public void test_getLong_TemporalField() { LocalTime test = TEST_12_30_40_987654321; assertEquals(test.getLong(ChronoField.HOUR_OF_DAY), 12); assertEquals(test.getLong(ChronoField.MINUTE_OF_HOUR), 30); assertEquals(test.getLong(ChronoField.SECOND_OF_MINUTE), 40); assertEquals(test.getLong(ChronoField.NANO_OF_SECOND), 987654321); assertEquals(test.getLong(ChronoField.SECOND_OF_DAY), 12 * 3600 + 30 * 60 + 40); assertEquals(test.getLong(ChronoField.MINUTE_OF_DAY), 12 * 60 + 30); assertEquals(test.getLong(ChronoField.HOUR_OF_AMPM), 0); assertEquals(test.getLong(ChronoField.CLOCK_HOUR_OF_AMPM), 12); assertEquals(test.getLong(ChronoField.CLOCK_HOUR_OF_DAY), 12); assertEquals(test.getLong(ChronoField.AMPM_OF_DAY), 1); }
@Test public void test_isSupported_TemporalField() { assertEquals(TEST_2008.isSupported((TemporalField) null), false); assertEquals(TEST_2008.isSupported(ChronoField.NANO_OF_SECOND), false); assertEquals(TEST_2008.isSupported(ChronoField.NANO_OF_DAY), false); assertEquals(TEST_2008.isSupported(ChronoField.MICRO_OF_SECOND), false); assertEquals(TEST_2008.isSupported(ChronoField.MICRO_OF_DAY), false); assertEquals(TEST_2008.isSupported(ChronoField.MILLI_OF_SECOND), false); assertEquals(TEST_2008.isSupported(ChronoField.MILLI_OF_DAY), false); assertEquals(TEST_2008.isSupported(ChronoField.SECOND_OF_MINUTE), false); assertEquals(TEST_2008.isSupported(ChronoField.SECOND_OF_DAY), false); assertEquals(TEST_2008.isSupported(ChronoField.MINUTE_OF_HOUR), false); assertEquals(TEST_2008.isSupported(ChronoField.MINUTE_OF_DAY), false); assertEquals(TEST_2008.isSupported(ChronoField.HOUR_OF_AMPM), false); assertEquals(TEST_2008.isSupported(ChronoField.CLOCK_HOUR_OF_AMPM), false); assertEquals(TEST_2008.isSupported(ChronoField.HOUR_OF_DAY), false); assertEquals(TEST_2008.isSupported(ChronoField.CLOCK_HOUR_OF_DAY), false); assertEquals(TEST_2008.isSupported(ChronoField.AMPM_OF_DAY), false); assertEquals(TEST_2008.isSupported(ChronoField.DAY_OF_WEEK), false); assertEquals(TEST_2008.isSupported(ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH), false); assertEquals(TEST_2008.isSupported(ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR), false); assertEquals(TEST_2008.isSupported(ChronoField.DAY_OF_MONTH), false); assertEquals(TEST_2008.isSupported(ChronoField.DAY_OF_YEAR), false); assertEquals(TEST_2008.isSupported(ChronoField.EPOCH_DAY), false); assertEquals(TEST_2008.isSupported(ChronoField.ALIGNED_WEEK_OF_MONTH), false); assertEquals(TEST_2008.isSupported(ChronoField.ALIGNED_WEEK_OF_YEAR), false); assertEquals(TEST_2008.isSupported(ChronoField.MONTH_OF_YEAR), false); assertEquals(TEST_2008.isSupported(ChronoField.PROLEPTIC_MONTH), false); assertEquals(TEST_2008.isSupported(ChronoField.YEAR), true); assertEquals(TEST_2008.isSupported(ChronoField.YEAR_OF_ERA), true); assertEquals(TEST_2008.isSupported(ChronoField.ERA), true); assertEquals(TEST_2008.isSupported(ChronoField.INSTANT_SECONDS), false); assertEquals(TEST_2008.isSupported(ChronoField.OFFSET_SECONDS), false); }
@Test(dataProvider = "calendarNameAndType") public void test_dateEpochDay(String name, String alias) { Chronology chrono = Chronology.of(name); ChronoLocalDate date = chrono.dateNow(); long epochDay = date.getLong(ChronoField.EPOCH_DAY); ChronoLocalDate test = chrono.dateEpochDay(epochDay); assertEquals(test, date); }
@Test public void test_isSupported_TemporalField() { assertEquals(TEST_11_30_59_500_PONE.isSupported((TemporalField) null), false); assertEquals(TEST_11_30_59_500_PONE.isSupported(ChronoField.NANO_OF_SECOND), true); assertEquals(TEST_11_30_59_500_PONE.isSupported(ChronoField.NANO_OF_DAY), true); assertEquals(TEST_11_30_59_500_PONE.isSupported(ChronoField.MICRO_OF_SECOND), true); assertEquals(TEST_11_30_59_500_PONE.isSupported(ChronoField.MICRO_OF_DAY), true); assertEquals(TEST_11_30_59_500_PONE.isSupported(ChronoField.MILLI_OF_SECOND), true); assertEquals(TEST_11_30_59_500_PONE.isSupported(ChronoField.MILLI_OF_DAY), true); assertEquals(TEST_11_30_59_500_PONE.isSupported(ChronoField.SECOND_OF_MINUTE), true); assertEquals(TEST_11_30_59_500_PONE.isSupported(ChronoField.SECOND_OF_DAY), true); assertEquals(TEST_11_30_59_500_PONE.isSupported(ChronoField.MINUTE_OF_HOUR), true); assertEquals(TEST_11_30_59_500_PONE.isSupported(ChronoField.MINUTE_OF_DAY), true); assertEquals(TEST_11_30_59_500_PONE.isSupported(ChronoField.HOUR_OF_AMPM), true); assertEquals(TEST_11_30_59_500_PONE.isSupported(ChronoField.CLOCK_HOUR_OF_AMPM), true); assertEquals(TEST_11_30_59_500_PONE.isSupported(ChronoField.HOUR_OF_DAY), true); assertEquals(TEST_11_30_59_500_PONE.isSupported(ChronoField.CLOCK_HOUR_OF_DAY), true); assertEquals(TEST_11_30_59_500_PONE.isSupported(ChronoField.AMPM_OF_DAY), true); assertEquals(TEST_11_30_59_500_PONE.isSupported(ChronoField.DAY_OF_WEEK), false); assertEquals(TEST_11_30_59_500_PONE.isSupported(ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH), false); assertEquals(TEST_11_30_59_500_PONE.isSupported(ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR), false); assertEquals(TEST_11_30_59_500_PONE.isSupported(ChronoField.DAY_OF_MONTH), false); assertEquals(TEST_11_30_59_500_PONE.isSupported(ChronoField.DAY_OF_YEAR), false); assertEquals(TEST_11_30_59_500_PONE.isSupported(ChronoField.EPOCH_DAY), false); assertEquals(TEST_11_30_59_500_PONE.isSupported(ChronoField.ALIGNED_WEEK_OF_MONTH), false); assertEquals(TEST_11_30_59_500_PONE.isSupported(ChronoField.ALIGNED_WEEK_OF_YEAR), false); assertEquals(TEST_11_30_59_500_PONE.isSupported(ChronoField.MONTH_OF_YEAR), false); assertEquals(TEST_11_30_59_500_PONE.isSupported(ChronoField.PROLEPTIC_MONTH), false); assertEquals(TEST_11_30_59_500_PONE.isSupported(ChronoField.YEAR), false); assertEquals(TEST_11_30_59_500_PONE.isSupported(ChronoField.YEAR_OF_ERA), false); assertEquals(TEST_11_30_59_500_PONE.isSupported(ChronoField.ERA), false); assertEquals(TEST_11_30_59_500_PONE.isSupported(ChronoField.INSTANT_SECONDS), false); assertEquals(TEST_11_30_59_500_PONE.isSupported(ChronoField.OFFSET_SECONDS), true); }
@Test(dataProvider = "resolve_ymd") public void test_resolve_ymd_lenient(int y, int m, int d, MinguoDate expected, Object smart, boolean strict) { Map<TemporalField, Long> fieldValues = new HashMap<>(); fieldValues.put(ChronoField.YEAR, (long) y); fieldValues.put(ChronoField.MONTH_OF_YEAR, (long) m); fieldValues.put(ChronoField.DAY_OF_MONTH, (long) d); MinguoDate date = MinguoChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.LENIENT); assertEquals(date, expected); assertEquals(fieldValues.size(), 0); }
@Test(dataProvider = "resolverStyle") public void test_resolverStyle(String str, ResolverStyle style, Class<?> expectedEx, int year, int month, int day) { DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder(); builder.appendValue(ChronoField.YEAR_OF_ERA); builder.appendLiteral("/"); builder.appendValue(ChronoField.MONTH_OF_YEAR); builder.appendLiteral("/"); builder.appendValue(ChronoField.DAY_OF_MONTH); Map<Long, String> eraMap = new HashMap<Long, String>(); eraMap.put(1L, "CE"); eraMap.put(0L, "BCE"); DateTimeFormatter optionalFormatter = new DateTimeFormatterBuilder().appendLiteral(" ").appendText(ChronoField.ERA, eraMap).toFormatter(); DateTimeFormatter formatter = builder.appendOptional(optionalFormatter).toFormatter(); formatter = formatter.withResolverStyle(style); if (expectedEx == null) { TemporalAccessor accessor = formatter.parse(str); assertEquals(accessor.get(ChronoField.YEAR_OF_ERA), year); assertEquals(accessor.get(ChronoField.MONTH_OF_YEAR), month); assertEquals(accessor.get(ChronoField.DAY_OF_MONTH), day); } else { try { formatter.parse(str); fail(); } catch (Exception ex) { assertTrue(expectedEx.isInstance(ex)); } } }
@Test public void test_get_TemporalField() { LocalTime test = TEST_12_30_40_987654321; assertEquals(test.get(ChronoField.HOUR_OF_DAY), 12); assertEquals(test.get(ChronoField.MINUTE_OF_HOUR), 30); assertEquals(test.get(ChronoField.SECOND_OF_MINUTE), 40); assertEquals(test.get(ChronoField.NANO_OF_SECOND), 987654321); assertEquals(test.get(ChronoField.SECOND_OF_DAY), 12 * 3600 + 30 * 60 + 40); assertEquals(test.get(ChronoField.MINUTE_OF_DAY), 12 * 60 + 30); assertEquals(test.get(ChronoField.HOUR_OF_AMPM), 0); assertEquals(test.get(ChronoField.CLOCK_HOUR_OF_AMPM), 12); assertEquals(test.get(ChronoField.CLOCK_HOUR_OF_DAY), 12); assertEquals(test.get(ChronoField.AMPM_OF_DAY), 1); }
@Test(dataProvider = "resolve_styleByEra") public void test_resolve_yearOfEra_eraAndYearOnly_valid(ResolverStyle style, JapaneseEra era) { Map<TemporalField, Long> fieldValues = new HashMap<>(); fieldValues.put(ChronoField.ERA, (long) era.getValue()); fieldValues.put(ChronoField.YEAR, 1L); JapaneseDate date = JapaneseChronology.INSTANCE.resolveDate(fieldValues, style); assertEquals(date, null); assertEquals(fieldValues.get(ChronoField.ERA), (Long) (long) era.getValue()); assertEquals(fieldValues.get(ChronoField.YEAR), (Long) 1L); assertEquals(fieldValues.size(), 2); }
@Test(dataProvider = "resolve_yd") public void test_resolve_yd_lenient(int y, int d, MinguoDate expected, boolean smart, boolean strict) { Map<TemporalField, Long> fieldValues = new HashMap<>(); fieldValues.put(ChronoField.YEAR, (long) y); fieldValues.put(ChronoField.DAY_OF_YEAR, (long) d); MinguoDate date = MinguoChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.LENIENT); assertEquals(date, expected); assertEquals(fieldValues.size(), 0); }
@Test public void test_chronoPackageExample() { // Print the Thai Buddhist date ChronoLocalDate now1 = Chronology.of("ThaiBuddhist").dateNow(); int day = now1.get(ChronoField.DAY_OF_MONTH); int dow = now1.get(ChronoField.DAY_OF_WEEK); int month = now1.get(ChronoField.MONTH_OF_YEAR); int year = now1.get(ChronoField.YEAR); System.out.printf(" Today is %s %s %d-%s-%d%n", now1.getChronology().getId(), dow, day, month, year); // Enumerate the list of available calendars and print today for each Set<Chronology> chronos = Chronology.getAvailableChronologies(); for (Chronology chrono : chronos) { ChronoLocalDate date = chrono.dateNow(); System.out.printf(" %20s: %s%n", chrono.getId(), date.toString()); } // Print today's date and the last day of the year for the Thai Buddhist Calendar. ChronoLocalDate first = now1 .with(ChronoField.DAY_OF_MONTH, 1) .with(ChronoField.MONTH_OF_YEAR, 1); ChronoLocalDate last = first .plus(1, ChronoUnit.YEARS) .minus(1, ChronoUnit.DAYS); System.out.printf(" %s: 1st of year: %s; end of year: %s%n", last.getChronology().getId(), first, last); }
@Test(dataProvider = "resolve_styleByEra") public void test_resolve_yearOfEra_eraAndYearOnly_valid(ResolverStyle style, HijrahEra era) { Map<TemporalField, Long> fieldValues = new HashMap<>(); fieldValues.put(ChronoField.ERA, (long) era.getValue()); fieldValues.put(ChronoField.YEAR, 1343L); HijrahDate date = HijrahChronology.INSTANCE.resolveDate(fieldValues, style); assertEquals(date, null); assertEquals(fieldValues.get(ChronoField.ERA), (Long) (long) era.getValue()); assertEquals(fieldValues.get(ChronoField.YEAR), (Long) 1343L); assertEquals(fieldValues.size(), 2); }
@Override public boolean isSupported(TemporalField field) { if (field instanceof ChronoField) { ChronoField f = (ChronoField) field; return f.isDateBased() || f.isTimeBased(); } return field != null && field.isSupportedBy(this); }
public void test_printText() { Random r = RandomFactory.getRandom(); int N = 8; Locale[] locales = Locale.getAvailableLocales(); Set<String> zids = ZoneRulesProvider.getAvailableZoneIds(); ZonedDateTime zdt = ZonedDateTime.now(); //System.out.printf("locale==%d, timezone=%d%n", locales.length, zids.size()); while (N-- > 0) { zdt = zdt.withDayOfYear(r.nextInt(365) + 1) .with(ChronoField.SECOND_OF_DAY, r.nextInt(86400)); for (String zid : zids) { if (zid.equals("ROC") || zid.startsWith("Etc/GMT")) { continue; // TBD: match jdk behavior? } zdt = zdt.withZoneSameLocal(ZoneId.of(zid)); TimeZone tz = TimeZone.getTimeZone(zid); boolean isDST = tz.inDaylightTime(new Date(zdt.toInstant().toEpochMilli())); for (Locale locale : locales) { String longDisplayName = tz.getDisplayName(isDST, TimeZone.LONG, locale); String shortDisplayName = tz.getDisplayName(isDST, TimeZone.SHORT, locale); if ((longDisplayName.startsWith("GMT+") && shortDisplayName.startsWith("GMT+")) || (longDisplayName.startsWith("GMT-") && shortDisplayName.startsWith("GMT-"))) { printText(locale, zdt, TextStyle.FULL, tz, tz.getID()); printText(locale, zdt, TextStyle.SHORT, tz, tz.getID()); continue; } printText(locale, zdt, TextStyle.FULL, tz, tz.getDisplayName(isDST, TimeZone.LONG, locale)); printText(locale, zdt, TextStyle.SHORT, tz, tz.getDisplayName(isDST, TimeZone.SHORT, locale)); } } } }
@Override public MinguoDate with(TemporalField field, long newValue) { if (field instanceof ChronoField) { ChronoField f = (ChronoField) field; if (getLong(f) == newValue) { return this; } switch (f) { case PROLEPTIC_MONTH: getChronology().range(f).checkValidValue(newValue, f); return plusMonths(newValue - getProlepticMonth()); case YEAR_OF_ERA: case YEAR: case ERA: { int nvalue = getChronology().range(f).checkValidIntValue(newValue, f); switch (f) { case YEAR_OF_ERA: return with(isoDate.withYear(getProlepticYear() >= 1 ? nvalue + YEARS_DIFFERENCE : (1 - nvalue) + YEARS_DIFFERENCE)); case YEAR: return with(isoDate.withYear(nvalue + YEARS_DIFFERENCE)); case ERA: return with(isoDate.withYear((1 - getProlepticYear()) + YEARS_DIFFERENCE)); } } } return with(isoDate.with(field, newValue)); } return super.with(field, newValue); }