本文整理汇总了Java中java.time.temporal.TemporalField类的典型用法代码示例。如果您正苦于以下问题:Java TemporalField类的具体用法?Java TemporalField怎么用?Java TemporalField使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
TemporalField类属于java.time.temporal包,在下文中一共展示了TemporalField类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: test_resolve_ymaa_strict
import java.time.temporal.TemporalField; //导入依赖的package包/类
@Test(dataProvider = "resolve_ymaa")
public void test_resolve_ymaa_strict(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);
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
}
}
}
示例2: basicTest_get_TemporalField_supported
import java.time.temporal.TemporalField; //导入依赖的package包/类
@Test()
public void basicTest_get_TemporalField_supported() {
for (TemporalAccessor sample : samples()) {
for (TemporalField field : validFields()) {
if (sample.range(field).isIntValue()) {
sample.get(field); // no exception
} else {
try {
sample.get(field);
fail("Failed on " + sample + " " + field);
} catch (DateTimeException ex) {
// expected
}
}
}
}
}
示例3: test_resolve_yearOfEra
import java.time.temporal.TemporalField; //导入依赖的package包/类
@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) {
LocalDate date = IsoChronology.INSTANCE.resolveDate(fieldValues, style);
assertEquals(date, null);
assertEquals(fieldValues.get(field), (Long) expected.longValue());
assertEquals(fieldValues.size(), 1);
} else {
try {
IsoChronology.INSTANCE.resolveDate(fieldValues, style);
fail("Should have failed");
} catch (DateTimeException ex) {
// expected
}
}
}
示例4: getLong
import java.time.temporal.TemporalField; //导入依赖的package包/类
@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);
}
示例5: validFields
import java.time.temporal.TemporalField; //导入依赖的package包/类
@Override
protected List<TemporalField> validFields() {
TemporalField[] array = {
NANO_OF_SECOND,
NANO_OF_DAY,
MICRO_OF_SECOND,
MICRO_OF_DAY,
MILLI_OF_SECOND,
MILLI_OF_DAY,
SECOND_OF_MINUTE,
SECOND_OF_DAY,
MINUTE_OF_HOUR,
MINUTE_OF_DAY,
CLOCK_HOUR_OF_AMPM,
HOUR_OF_AMPM,
CLOCK_HOUR_OF_DAY,
HOUR_OF_DAY,
AMPM_OF_DAY,
};
return Arrays.asList(array);
}
示例6: getLong
import java.time.temporal.TemporalField; //导入依赖的package包/类
@Override
public long getLong(TemporalField field) {
Objects.requireNonNull(field, "field");
Long value = fieldValues.get(field);
if (value != null) {
return value;
}
if (date != null && date.isSupported(field)) {
return date.getLong(field);
}
if (time != null && time.isSupported(field)) {
return time.getLong(field);
}
if (field instanceof ChronoField) {
throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
}
return field.getFrom(this);
}
示例7: test_resolve_ymd_strict
import java.time.temporal.TemporalField; //导入依赖的package包/类
@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
}
}
}
示例8: with
import java.time.temporal.TemporalField; //导入依赖的package包/类
@Override
public HijrahDate with(TemporalField field, long newValue) {
if (field instanceof ChronoField) {
ChronoField f = (ChronoField) field;
// not using checkValidIntValue so EPOCH_DAY and PROLEPTIC_MONTH work
chrono.range(f).checkValidValue(newValue, f); // TODO: validate value
int nvalue = (int) newValue;
switch (f) {
case DAY_OF_WEEK: return plusDays(newValue - getDayOfWeek());
case ALIGNED_DAY_OF_WEEK_IN_MONTH: return plusDays(newValue - getLong(ALIGNED_DAY_OF_WEEK_IN_MONTH));
case ALIGNED_DAY_OF_WEEK_IN_YEAR: return plusDays(newValue - getLong(ALIGNED_DAY_OF_WEEK_IN_YEAR));
case DAY_OF_MONTH: return resolvePreviousValid(prolepticYear, monthOfYear, nvalue);
case DAY_OF_YEAR: return plusDays(Math.min(nvalue, lengthOfYear()) - getDayOfYear());
case EPOCH_DAY: return new HijrahDate(chrono, newValue);
case ALIGNED_WEEK_OF_MONTH: return plusDays((newValue - getLong(ALIGNED_WEEK_OF_MONTH)) * 7);
case ALIGNED_WEEK_OF_YEAR: return plusDays((newValue - getLong(ALIGNED_WEEK_OF_YEAR)) * 7);
case MONTH_OF_YEAR: return resolvePreviousValid(prolepticYear, nvalue, dayOfMonth);
case PROLEPTIC_MONTH: return plusMonths(newValue - getProlepticMonth());
case YEAR_OF_ERA: return resolvePreviousValid(prolepticYear >= 1 ? nvalue : 1 - nvalue, monthOfYear, dayOfMonth);
case YEAR: return resolvePreviousValid(nvalue, monthOfYear, dayOfMonth);
case ERA: return resolvePreviousValid(1 - prolepticYear, monthOfYear, dayOfMonth);
}
throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
}
return super.with(field, newValue);
}
示例9: getText
import java.time.temporal.TemporalField; //导入依赖的package包/类
/**
* Gets the text for the specified chrono, field, locale and style
* for the purpose of formatting.
* <p>
* The text associated with the value is returned.
* The null return value should be used if there is no applicable text, or
* if the text would be a numeric representation of the value.
*
* @param chrono the Chronology to get text for, not null
* @param field the field to get text for, not null
* @param value the field value to get text for, not null
* @param style the style to get text for, not null
* @param locale the locale to get text for, not null
* @return the text for the field value, null if no text found
*/
public String getText(Chronology chrono, TemporalField field, long value,
TextStyle style, Locale locale) {
if (chrono == IsoChronology.INSTANCE
|| !(field instanceof ChronoField)) {
return getText(field, value, style, locale);
}
int fieldIndex;
int fieldValue;
if (field == ERA) {
fieldIndex = Calendar.ERA;
if (chrono == JapaneseChronology.INSTANCE) {
if (value == -999) {
fieldValue = 0;
} else {
fieldValue = (int) value + 2;
}
} else {
fieldValue = (int) value;
}
} else if (field == MONTH_OF_YEAR) {
fieldIndex = Calendar.MONTH;
fieldValue = (int) value - 1;
} else if (field == DAY_OF_WEEK) {
fieldIndex = Calendar.DAY_OF_WEEK;
fieldValue = (int) value + 1;
if (fieldValue > 7) {
fieldValue = Calendar.SUNDAY;
}
} else if (field == AMPM_OF_DAY) {
fieldIndex = Calendar.AM_PM;
fieldValue = (int) value;
} else {
return null;
}
return CalendarDataUtility.retrieveJavaTimeFieldValueName(
chrono.getCalendarType(), fieldIndex, fieldValue, style.toCalendarStyle(), locale);
}
示例10: DateTimeFormatter
import java.time.temporal.TemporalField; //导入依赖的package包/类
/**
* Constructor.
*
* @param printerParser the printer/parser to use, not null
* @param locale the locale to use, not null
* @param decimalStyle the DecimalStyle to use, not null
* @param resolverStyle the resolver style to use, not null
* @param resolverFields the fields to use during resolving, null for all fields
* @param chrono the chronology to use, null for no override
* @param zone the zone to use, null for no override
*/
DateTimeFormatter(CompositePrinterParser printerParser,
Locale locale, DecimalStyle decimalStyle,
ResolverStyle resolverStyle, Set<TemporalField> resolverFields,
Chronology chrono, ZoneId zone) {
this.printerParser = Objects.requireNonNull(printerParser, "printerParser");
this.resolverFields = resolverFields;
this.locale = Objects.requireNonNull(locale, "locale");
this.decimalStyle = Objects.requireNonNull(decimalStyle, "decimalStyle");
this.resolverStyle = Objects.requireNonNull(resolverStyle, "resolverStyle");
this.chrono = chrono;
this.zone = zone;
}
示例11: test_withWeekOfWeekBasedYear
import java.time.temporal.TemporalField; //导入依赖的package包/类
@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);
}
示例12: test_weekOfYearField
import java.time.temporal.TemporalField; //导入依赖的package包/类
@Test(dataProvider="weekFields")
public void test_weekOfYearField(DayOfWeek firstDayOfWeek, int minDays) {
LocalDate day = LocalDate.of(2012, 12, 31); // Known to be ISO Monday
WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
TemporalField dowField = week.dayOfWeek();
TemporalField woyField = week.weekOfYear();
for (int i = 1; i <= 15; i++) {
int actualDOW = day.get(dowField);
int actualWOY = day.get(woyField);
// Verify that the combination of day of week and week of month can be used
// to reconstruct the same date.
LocalDate day1 = day.withDayOfYear(1);
int offset = - (day1.get(dowField) - 1);
int week1 = day1.get(woyField);
if (week1 == 0) {
// week of the 1st is partial; start with first full week
offset += 7;
}
offset += actualDOW - 1;
offset += (actualWOY - 1) * 7;
LocalDate result = day1.plusDays(offset);
assertEquals(result, day, "Incorrect dayOfWeek or weekOfYear "
+ String.format("%s, ISO Dow: %s, offset: %s, actualDOW: %s, actualWOM: %s, expected: %s, result: %s%n",
week, day.getDayOfWeek(), offset, actualDOW, actualWOY, day, result));
day = day.plusDays(1);
}
}
示例13: resolve
import java.time.temporal.TemporalField; //导入依赖的package包/类
/**
* Resolves the fields in this context.
*
* @param resolverStyle the resolver style, not null
* @param resolverFields the fields to use for resolving, null for all fields
* @return this, for method chaining
* @throws DateTimeException if resolving one field results in a value for
* another field that is in conflict
*/
TemporalAccessor resolve(ResolverStyle resolverStyle, Set<TemporalField> resolverFields) {
if (resolverFields != null) {
fieldValues.keySet().retainAll(resolverFields);
}
this.resolverStyle = resolverStyle;
resolveFields();
resolveTimeLenient();
crossCheck();
resolvePeriod();
resolveFractional();
resolveInstant();
return this;
}
示例14: get0
import java.time.temporal.TemporalField; //导入依赖的package包/类
private int get0(TemporalField field) {
switch ((ChronoField) field) {
case NANO_OF_SECOND: return nano;
case NANO_OF_DAY: throw new UnsupportedTemporalTypeException("Invalid field 'NanoOfDay' for get() method, use getLong() instead");
case MICRO_OF_SECOND: return nano / 1000;
case MICRO_OF_DAY: throw new UnsupportedTemporalTypeException("Invalid field 'MicroOfDay' for get() method, use getLong() instead");
case MILLI_OF_SECOND: return nano / 1000_000;
case MILLI_OF_DAY: return (int) (toNanoOfDay() / 1000_000);
case SECOND_OF_MINUTE: return second;
case SECOND_OF_DAY: return toSecondOfDay();
case MINUTE_OF_HOUR: return minute;
case MINUTE_OF_DAY: return hour * 60 + minute;
case HOUR_OF_AMPM: return hour % 12;
case CLOCK_HOUR_OF_AMPM: int ham = hour % 12; return (ham % 12 == 0 ? 12 : ham);
case HOUR_OF_DAY: return hour;
case CLOCK_HOUR_OF_DAY: return (hour == 0 ? 24 : hour);
case AMPM_OF_DAY: return hour / 12;
}
throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
}
示例15: range
import java.time.temporal.TemporalField; //导入依赖的package包/类
@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);
}