本文整理汇总了Java中java.time.temporal.ChronoField类的典型用法代码示例。如果您正苦于以下问题:Java ChronoField类的具体用法?Java ChronoField怎么用?Java ChronoField使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ChronoField类属于java.time.temporal包,在下文中一共展示了ChronoField类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: test_signStyle
import java.time.temporal.ChronoField; //导入依赖的package包/类
@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));
}
}
}
示例2: test_resolve_yd_strict
import java.time.temporal.ChronoField; //导入依赖的package包/类
@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
}
}
}
示例3: test_resolve_ymd_strict
import java.time.temporal.ChronoField; //导入依赖的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
}
}
}
示例4: test_resolve_yd_smart
import java.time.temporal.ChronoField; //导入依赖的package包/类
@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
}
}
}
示例5: test_resolve_yearOfEra
import java.time.temporal.ChronoField; //导入依赖的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) {
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
}
}
}
示例6: get0
import java.time.temporal.ChronoField; //导入依赖的package包/类
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);
}
示例7: range
import java.time.temporal.ChronoField; //导入依赖的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);
}
示例8: range
import java.time.temporal.ChronoField; //导入依赖的package包/类
@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();
}
示例9: getLong
import java.time.temporal.ChronoField; //导入依赖的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);
}
示例10: testCalculateDateTimeDateTime
import java.time.temporal.ChronoField; //导入依赖的package包/类
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));
}
示例11: testCalculateDateTimeDateTime
import java.time.temporal.ChronoField; //导入依赖的package包/类
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));
}
示例12: range
import java.time.temporal.ChronoField; //导入依赖的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 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);
}
示例13: test_getLong_TemporalField
import java.time.temporal.ChronoField; //导入依赖的package包/类
@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);
}
示例14: test_resolve_ymaa_lenient
import java.time.temporal.ChronoField; //导入依赖的package包/类
@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);
}
示例15: testSetGetDateTimeAt
import java.time.temporal.ChronoField; //导入依赖的package包/类
@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);
}