下面列出了怎么用java.time.temporal.ValueRange的API类实例代码及写法,或者点击链接到github查看源代码。
ChronoLocalDate resolveYMD(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) {
int y = range(YEAR).checkValidIntValue(fieldValues.remove(YEAR), YEAR);
if (resolverStyle == ResolverStyle.LENIENT) {
long months = Math.subtractExact(fieldValues.remove(MONTH_OF_YEAR), 1);
long days = Math.subtractExact(fieldValues.remove(DAY_OF_MONTH), 1);
return date(y, 1, 1).plus(months, MONTHS).plus(days, DAYS);
}
int moy = range(MONTH_OF_YEAR).checkValidIntValue(fieldValues.remove(MONTH_OF_YEAR), MONTH_OF_YEAR);
ValueRange domRange = range(DAY_OF_MONTH);
int dom = domRange.checkValidIntValue(fieldValues.remove(DAY_OF_MONTH), DAY_OF_MONTH);
if (resolverStyle == ResolverStyle.SMART) { // previous valid
try {
return date(y, moy, dom);
} catch (DateTimeException ex) {
return date(y, moy, 1).with(TemporalAdjusters.lastDayOfMonth());
}
}
return date(y, moy, dom);
}
@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);
}
@Test(dataProvider="weekFields")
public void test_rangeWeekOfWeekBasedYear(DayOfWeek firstDayOfWeek, int minDays) {
WeekFields weekFields = WeekFields.of(firstDayOfWeek, minDays);
TemporalField dowField = weekFields.dayOfWeek();
TemporalField wowByField = weekFields.weekOfWeekBasedYear();
LocalDate day1 = LocalDate.of(2012, 1, weekFields.getMinimalDaysInFirstWeek());
day1 = day1.with(wowByField, 1).with(dowField, 1);
LocalDate day2 = LocalDate.of(2013, 1, weekFields.getMinimalDaysInFirstWeek());
day2 = day2.with(wowByField, 1).with(dowField, 1);
int expectedWeeks = (int)ChronoUnit.DAYS.between(day1, day2) / 7;
ValueRange range = day1.range(wowByField);
assertEquals(range.getMaximum(), expectedWeeks, "Range incorrect");
}
@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(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(dataProvider="weekFields")
public void test_withWeekOfWeekBasedYear(DayOfWeek firstDayOfWeek, int minDays) {
LocalDate day = LocalDate.of(2012, 12, 31);
WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
TemporalField dowField = week.dayOfWeek();
TemporalField wowbyField = week.weekOfWeekBasedYear();
TemporalField yowbyField = week.weekBasedYear();
int dowExpected = (day.get(dowField) - 1) % 7 + 1;
LocalDate dowDate = day.with(dowField, dowExpected);
int dowResult = dowDate.get(dowField);
assertEquals(dowResult, dowExpected, "Localized DayOfWeek not correct; " + day + " -->" + dowDate);
int weekExpected = day.get(wowbyField) + 1;
ValueRange range = day.range(wowbyField);
weekExpected = ((weekExpected - 1) % (int)range.getMaximum()) + 1;
LocalDate weekDate = day.with(wowbyField, weekExpected);
int weekResult = weekDate.get(wowbyField);
assertEquals(weekResult, weekExpected, "Localized WeekOfWeekBasedYear not correct; " + day + " -->" + weekDate);
int yearExpected = day.get(yowbyField) + 1;
LocalDate yearDate = day.with(yowbyField, yearExpected);
int yearResult = yearDate.get(yowbyField);
assertEquals(yearResult, yearExpected, "Localized WeekBasedYear not correct; " + day + " --> " + yearDate);
range = yearDate.range(wowbyField);
weekExpected = Math.min(day.get(wowbyField), (int)range.getMaximum());
int weekActual = yearDate.get(wowbyField);
assertEquals(weekActual, weekExpected, "Localized WeekOfWeekBasedYear week should not change; " + day + " --> " + yearDate + ", actual: " + weekActual + ", weekExpected: " + weekExpected);
}
@Override
public ValueRange range(TemporalField field) {
if (field instanceof ChronoField) {
if (isSupported(field)) {
return field.range();
}
throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
}
return field.rangeRefinedBy(this);
}
@Test(dataProvider="weekFields")
public void test_fieldRanges(DayOfWeek firstDayOfWeek, int minDays) {
WeekFields weekDef = WeekFields.of(firstDayOfWeek, minDays);
TemporalField womField = weekDef.weekOfMonth();
TemporalField woyField = weekDef.weekOfYear();
LocalDate day = LocalDate.of(2012, 11, 30);
LocalDate endDay = LocalDate.of(2013, 1, 2);
while (day.isBefore(endDay)) {
LocalDate last = day.with(DAY_OF_MONTH, day.lengthOfMonth());
int lastWOM = last.get(womField);
LocalDate first = day.with(DAY_OF_MONTH, 1);
int firstWOM = first.get(womField);
ValueRange rangeWOM = day.range(womField);
assertEquals(rangeWOM.getMinimum(), firstWOM,
"Range min should be same as WeekOfMonth for first day of month: "
+ first + ", " + weekDef);
assertEquals(rangeWOM.getMaximum(), lastWOM,
"Range max should be same as WeekOfMonth for last day of month: "
+ last + ", " + weekDef);
last = day.with(DAY_OF_YEAR, day.lengthOfYear());
int lastWOY = last.get(woyField);
first = day.with(DAY_OF_YEAR, 1);
int firstWOY = first.get(woyField);
ValueRange rangeWOY = day.range(woyField);
assertEquals(rangeWOY.getMinimum(), firstWOY,
"Range min should be same as WeekOfYear for first day of Year: "
+ day + ", " + weekDef);
assertEquals(rangeWOY.getMaximum(), lastWOY,
"Range max should be same as WeekOfYear for last day of Year: "
+ day + ", " + weekDef);
day = day.plusDays(1);
}
}
/**
* Calculates the range of months.
*
* @return the month range, -1 if not fixed range
*/
private long monthRange() {
ValueRange startRange = chrono.range(MONTH_OF_YEAR);
if (startRange.isFixed() && startRange.isIntValue()) {
return startRange.getMaximum() - startRange.getMinimum() + 1;
}
return -1;
}
/**
* Calculates the range of months.
*
* @return the month range, -1 if not fixed range
*/
private long monthRange() {
ValueRange startRange = chrono.range(MONTH_OF_YEAR);
if (startRange.isFixed() && startRange.isIntValue()) {
return startRange.getMaximum() - startRange.getMinimum() + 1;
}
return -1;
}
@Test(dataProvider="valid")
public void test_of_checkValidValue(long sMin, long lMin, long sMax, long lMax) {
ValueRange test = ValueRange.of(sMin, lMin, sMax, lMax);
assertEquals(test.checkValidIntValue(sMin, null), sMin);
assertEquals(test.checkValidIntValue(lMin, null), lMin);
assertEquals(test.checkValidIntValue(sMax, null), sMax);
assertEquals(test.checkValidIntValue(lMax, null), lMax);
}
@Test(dataProvider="weekFields")
public void test_fieldRanges(DayOfWeek firstDayOfWeek, int minDays) {
WeekFields weekDef = WeekFields.of(firstDayOfWeek, minDays);
TemporalField womField = weekDef.weekOfMonth();
TemporalField woyField = weekDef.weekOfYear();
LocalDate day = LocalDate.of(2012, 11, 30);
LocalDate endDay = LocalDate.of(2013, 1, 2);
while (day.isBefore(endDay)) {
LocalDate last = day.with(DAY_OF_MONTH, day.lengthOfMonth());
int lastWOM = last.get(womField);
LocalDate first = day.with(DAY_OF_MONTH, 1);
int firstWOM = first.get(womField);
ValueRange rangeWOM = day.range(womField);
assertEquals(rangeWOM.getMinimum(), firstWOM,
"Range min should be same as WeekOfMonth for first day of month: "
+ first + ", " + weekDef);
assertEquals(rangeWOM.getMaximum(), lastWOM,
"Range max should be same as WeekOfMonth for last day of month: "
+ last + ", " + weekDef);
last = day.with(DAY_OF_YEAR, day.lengthOfYear());
int lastWOY = last.get(woyField);
first = day.with(DAY_OF_YEAR, 1);
int firstWOY = first.get(woyField);
ValueRange rangeWOY = day.range(woyField);
assertEquals(rangeWOY.getMinimum(), firstWOY,
"Range min should be same as WeekOfYear for first day of Year: "
+ day + ", " + weekDef);
assertEquals(rangeWOY.getMaximum(), lastWOY,
"Range max should be same as WeekOfYear for last day of Year: "
+ day + ", " + weekDef);
day = day.plusDays(1);
}
}
@Test(dataProvider = "fields")
public void test_WOWBY_rangeRefinedBy(TemporalField weekField, TemporalField yearField) {
assertEquals(weekField.rangeRefinedBy(LocalDate.of(2012, 12, 31)), ValueRange.of(1, 52));
assertEquals(weekField.rangeRefinedBy(LocalDate.of(2013, 12, 29)), ValueRange.of(1, 52));
assertEquals(weekField.rangeRefinedBy(LocalDate.of(2013, 12, 30)), ValueRange.of(1, 52));
assertEquals(weekField.rangeRefinedBy(LocalDate.of(2014, 12, 28)), ValueRange.of(1, 52));
assertEquals(weekField.rangeRefinedBy(LocalDate.of(2014, 12, 29)), ValueRange.of(1, 53));
assertEquals(weekField.rangeRefinedBy(LocalDate.of(2016, 1, 3)), ValueRange.of(1, 53));
assertEquals(weekField.rangeRefinedBy(LocalDate.of(2016, 1, 4)), ValueRange.of(1, 52));
}
@Test
public void test_of_longlong_big() {
ValueRange test = ValueRange.of(1, 123456789012345L);
assertEquals(test.getMinimum(), 1);
assertEquals(test.getLargestMinimum(), 1);
assertEquals(test.getSmallestMaximum(), 123456789012345L);
assertEquals(test.getMaximum(), 123456789012345L);
assertEquals(test.isFixed(), true);
assertEquals(test.isIntValue(), false);
}
@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();
}
/**
* Calculates the range of months.
*
* @return the month range, -1 if not fixed range
*/
private long monthRange() {
ValueRange startRange = chrono.range(MONTH_OF_YEAR);
if (startRange.isFixed() && startRange.isIntValue()) {
return startRange.getMaximum() - startRange.getMinimum() + 1;
}
return -1;
}
@Test(dataProvider="dates")
public void Test_UmmAlQuraRanges(HijrahDate date,
int minYear, int maxYear,
int minMonth, int maxMonth,
int minDay, int maxDay, int maxChronoDay) {
// Check the chronology ranges
HijrahChronology chrono = date.getChronology();
ValueRange yearRange = chrono.range(YEAR);
assertEquals(yearRange.getMinimum(), minYear, "Minimum year for Hijrah chronology");
assertEquals(yearRange.getLargestMinimum(), minYear, "Largest minimum year for Hijrah chronology");
assertEquals(yearRange.getMaximum(), maxYear, "Maximum year for Hijrah chronology");
assertEquals(yearRange.getSmallestMaximum(), maxYear, "Smallest Maximum year for Hijrah chronology");
ValueRange monthRange = chrono.range(MONTH_OF_YEAR);
assertEquals(monthRange.getMinimum(), minMonth, "Minimum month for Hijrah chronology");
assertEquals(monthRange.getMaximum(), maxMonth, "Maximum month for Hijrah chronology");
ValueRange daysRange = chrono.range(DAY_OF_MONTH);
assertEquals(daysRange.getMinimum(), minDay, "Minimum day for chronology");
assertEquals(daysRange.getMaximum(), maxChronoDay, "Maximum day for Hijrah chronology");
// Check the date ranges
yearRange = date.range(YEAR);
assertEquals(yearRange.getMinimum(), minYear, "Minimum year for Hijrah date");
assertEquals(yearRange.getLargestMinimum(), minYear, "Largest minimum year for Hijrah date");
assertEquals(yearRange.getMaximum(), maxYear, "Maximum year for Hijrah date");
assertEquals(yearRange.getSmallestMaximum(), maxYear, "Smallest maximum year for Hijrah date");
monthRange = date.range(MONTH_OF_YEAR);
assertEquals(monthRange.getMinimum(), minMonth, "Minimum month for HijrahDate");
assertEquals(monthRange.getMaximum(), maxMonth, "Maximum month for HijrahDate");
daysRange = date.range(DAY_OF_MONTH);
assertEquals(daysRange.getMinimum(), minDay, "Minimum day for HijrahDate");
assertEquals(daysRange.getMaximum(), maxDay, "Maximum day for HijrahDate");
}
public void test_equals1() {
ValueRange a = ValueRange.of(1, 2, 3, 4);
ValueRange b = ValueRange.of(1, 2, 3, 4);
assertEquals(a.equals(a), true);
assertEquals(a.equals(b), true);
assertEquals(b.equals(a), true);
assertEquals(b.equals(b), true);
assertEquals(a.hashCode() == b.hashCode(), true);
}
@Test(dataProvider="dates")
public void Test_UmmAlQuraRanges(HijrahDate date,
int minYear, int maxYear,
int minMonth, int maxMonth,
int minDay, int maxDay, int maxChronoDay) {
// Check the chronology ranges
HijrahChronology chrono = date.getChronology();
ValueRange yearRange = chrono.range(YEAR);
assertEquals(yearRange.getMinimum(), minYear, "Minimum year for Hijrah chronology");
assertEquals(yearRange.getLargestMinimum(), minYear, "Largest minimum year for Hijrah chronology");
assertEquals(yearRange.getMaximum(), maxYear, "Maximum year for Hijrah chronology");
assertEquals(yearRange.getSmallestMaximum(), maxYear, "Smallest Maximum year for Hijrah chronology");
ValueRange monthRange = chrono.range(MONTH_OF_YEAR);
assertEquals(monthRange.getMinimum(), minMonth, "Minimum month for Hijrah chronology");
assertEquals(monthRange.getMaximum(), maxMonth, "Maximum month for Hijrah chronology");
ValueRange daysRange = chrono.range(DAY_OF_MONTH);
assertEquals(daysRange.getMinimum(), minDay, "Minimum day for chronology");
assertEquals(daysRange.getMaximum(), maxChronoDay, "Maximum day for Hijrah chronology");
// Check the date ranges
yearRange = date.range(YEAR);
assertEquals(yearRange.getMinimum(), minYear, "Minimum year for Hijrah date");
assertEquals(yearRange.getLargestMinimum(), minYear, "Largest minimum year for Hijrah date");
assertEquals(yearRange.getMaximum(), maxYear, "Maximum year for Hijrah date");
assertEquals(yearRange.getSmallestMaximum(), maxYear, "Smallest maximum year for Hijrah date");
monthRange = date.range(MONTH_OF_YEAR);
assertEquals(monthRange.getMinimum(), minMonth, "Minimum month for HijrahDate");
assertEquals(monthRange.getMaximum(), maxMonth, "Maximum month for HijrahDate");
daysRange = date.range(DAY_OF_MONTH);
assertEquals(daysRange.getMinimum(), minDay, "Minimum day for HijrahDate");
assertEquals(daysRange.getMaximum(), maxDay, "Maximum day for HijrahDate");
}
@Test (dataProvider="monthDays")
public void test_valueRange_monthDays(int year, int month, int maxlength) {
ChronoLocalDate date = HijrahChronology.INSTANCE.date(year, month, 1);
ValueRange range = null;
for (int i=1; i<=12; i++) {
range = date.range(ChronoField.DAY_OF_MONTH);
date = date.plus(1, ChronoUnit.MONTHS);
assertEquals(range.getMaximum(), month, maxlength);
}
}
@Test(dataProvider="valid")
public void test_of_checkValidValue(long sMin, long lMin, long sMax, long lMax) {
ValueRange test = ValueRange.of(sMin, lMin, sMax, lMax);
assertEquals(test.checkValidIntValue(sMin, null), sMin);
assertEquals(test.checkValidIntValue(lMin, null), lMin);
assertEquals(test.checkValidIntValue(sMax, null), sMax);
assertEquals(test.checkValidIntValue(lMax, null), lMax);
}
@DataProvider(name="isofields")
Object[][] data_isofields() {
return new Object[][] {
{IsoFields.DAY_OF_QUARTER, 49, ValueRange.of(1, 91)},
{IsoFields.QUARTER_OF_YEAR, 2, ValueRange.of(1, 4)},
{IsoFields.WEEK_OF_WEEK_BASED_YEAR, 20, ValueRange.of(1, 52)},
{IsoFields.WEEK_BASED_YEAR, 2016, ValueRange.of(LocalDate.MIN.getYear(),
LocalDate.MAX.getYear())},
};
}
@Override
default ValueRange range(TemporalField field) {
if (field instanceof ChronoField) {
if (field == INSTANT_SECONDS || field == OFFSET_SECONDS) {
return field.range();
}
return toLocalDateTime().range(field);
}
return field.rangeRefinedBy(this);
}
private long monthsUntil(ChronoLocalDate end) {
ValueRange range = getChronology().range(MONTH_OF_YEAR);
if (range.getMaximum() != 12) {
throw new IllegalStateException("ChronoLocalDateImpl only supports Chronologies with 12 months per year");
}
long packed1 = getLong(PROLEPTIC_MONTH) * 32L + get(DAY_OF_MONTH); // no overflow
long packed2 = end.getLong(PROLEPTIC_MONTH) * 32L + end.get(DAY_OF_MONTH); // no overflow
return (packed2 - packed1) / 32;
}
public void test_of_longlong() {
ValueRange test = ValueRange.of(1, 12);
assertEquals(test.getMinimum(), 1);
assertEquals(test.getLargestMinimum(), 1);
assertEquals(test.getSmallestMaximum(), 12);
assertEquals(test.getMaximum(), 12);
assertEquals(test.isFixed(), true);
assertEquals(test.isIntValue(), true);
}
@Test
public void test_range() {
assertEquals(MONTH_OF_YEAR.range(), ValueRange.of(1, 12));
assertEquals(MONTH_OF_YEAR.rangeRefinedBy(LocalDate.of(2000, 2, 29)), ValueRange.of(1, 12));
assertEquals(DAY_OF_MONTH.range(), ValueRange.of(1, 28, 31));
assertEquals(DAY_OF_MONTH.rangeRefinedBy(LocalDate.of(2000, 2, 29)), ValueRange.of(1, 29));
}
@Test (dataProvider="monthDays")
public void test_valueRange_monthDays(int year, int month, int maxlength) {
ChronoLocalDate date = HijrahChronology.INSTANCE.date(year, month, 1);
ValueRange range = null;
for (int i=1; i<=12; i++) {
range = date.range(ChronoField.DAY_OF_MONTH);
date = date.plus(1, ChronoUnit.MONTHS);
assertEquals(range.getMaximum(), month, maxlength);
}
}
@Test
public void test_range() {
assertEquals(MONTH_OF_YEAR.range(), ValueRange.of(1, 12));
assertEquals(MONTH_OF_YEAR.rangeRefinedBy(LocalDate.of(2000, 2, 29)), ValueRange.of(1, 12));
assertEquals(DAY_OF_MONTH.range(), ValueRange.of(1, 28, 31));
assertEquals(DAY_OF_MONTH.rangeRefinedBy(LocalDate.of(2000, 2, 29)), ValueRange.of(1, 29));
}
public void test_of_longlonglong() {
ValueRange test = ValueRange.of(1, 28, 31);
assertEquals(test.getMinimum(), 1);
assertEquals(test.getLargestMinimum(), 1);
assertEquals(test.getSmallestMaximum(), 28);
assertEquals(test.getMaximum(), 31);
assertEquals(test.isFixed(), false);
assertEquals(test.isIntValue(), true);
}
@Test
public void test_Chrono_range() {
long minYear = LocalDate.MIN.getYear() + YDIFF;
long maxYear = LocalDate.MAX.getYear() + YDIFF;
assertEquals(ThaiBuddhistChronology.INSTANCE.range(YEAR), ValueRange.of(minYear, maxYear));
assertEquals(ThaiBuddhistChronology.INSTANCE.range(YEAR_OF_ERA), ValueRange.of(1, -minYear + 1, maxYear));
assertEquals(ThaiBuddhistChronology.INSTANCE.range(DAY_OF_MONTH), DAY_OF_MONTH.range());
assertEquals(ThaiBuddhistChronology.INSTANCE.range(DAY_OF_YEAR), DAY_OF_YEAR.range());
assertEquals(ThaiBuddhistChronology.INSTANCE.range(MONTH_OF_YEAR), MONTH_OF_YEAR.range());
}