本文整理匯總了Java中java.time.temporal.TemporalAccessor類的典型用法代碼示例。如果您正苦於以下問題:Java TemporalAccessor類的具體用法?Java TemporalAccessor怎麽用?Java TemporalAccessor使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
TemporalAccessor類屬於java.time.temporal包,在下文中一共展示了TemporalAccessor類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: test_resolveFourToTime
import java.time.temporal.TemporalAccessor; //導入依賴的package包/類
@Test(dataProvider="resolveFourToTime")
public void test_resolveFourToTime(ResolverStyle style,
long hour, long min, long sec, long nano, LocalTime expectedTime, Period excessPeriod) {
DateTimeFormatter f = new DateTimeFormatterBuilder()
.parseDefaulting(HOUR_OF_DAY, hour)
.parseDefaulting(MINUTE_OF_HOUR, min)
.parseDefaulting(SECOND_OF_MINUTE, sec)
.parseDefaulting(NANO_OF_SECOND, nano).toFormatter();
ResolverStyle[] styles = (style != null ? new ResolverStyle[] {style} : ResolverStyle.values());
for (ResolverStyle s : styles) {
if (expectedTime != null) {
TemporalAccessor accessor = f.withResolverStyle(s).parse("");
assertEquals(accessor.query(TemporalQueries.localDate()), null, "ResolverStyle: " + s);
assertEquals(accessor.query(TemporalQueries.localTime()), expectedTime, "ResolverStyle: " + s);
assertEquals(accessor.query(DateTimeFormatter.parsedExcessDays()), excessPeriod, "ResolverStyle: " + s);
} else {
try {
f.withResolverStyle(style).parse("");
fail();
} catch (DateTimeParseException ex) {
// expected
}
}
}
}
示例2: test_parseSuccess_caseInsensitive
import java.time.temporal.TemporalAccessor; //導入依賴的package包/類
@Test(dataProvider="parseSuccess")
public void test_parseSuccess_caseInsensitive(String text, int expectedIndex, int expectedErrorIndex, ZoneId expected) {
builder.parseCaseInsensitive().appendZoneId();
String lcText = text.toLowerCase(Locale.ENGLISH);
TemporalAccessor parsed = builder.toFormatter().parseUnresolved(lcText, pos);
assertEquals(pos.getErrorIndex(), expectedErrorIndex, "Incorrect error index parsing: " + lcText);
assertEquals(pos.getIndex(), expectedIndex, "Incorrect index parsing: " + lcText);
if (expected != null) {
ZoneId zid = parsed.query(TemporalQueries.zoneId());
assertEquals(parsed.query(TemporalQueries.zoneId()), expected, "Incorrect zoneId parsing: " + lcText);
assertEquals(parsed.query(TemporalQueries.offset()), null, "Incorrect offset parsing: " + lcText);
assertEquals(parsed.query(TemporalQueries.zone()), expected, "Incorrect zone parsing: " + lcText);
} else {
assertEquals(parsed, null);
}
}
示例3: test_print_isoZonedDateTime
import java.time.temporal.TemporalAccessor; //導入依賴的package包/類
@Test(dataProvider="sample_isoZonedDateTime")
public void test_print_isoZonedDateTime(
Integer year, Integer month, Integer day,
Integer hour, Integer min, Integer sec, Integer nano, String offsetId, String zoneId,
String expected, Class<?> expectedEx) {
TemporalAccessor test = buildAccessor(year, month, day, hour, min, sec, nano, offsetId, zoneId);
if (expectedEx == null) {
assertEquals(DateTimeFormatter.ISO_ZONED_DATE_TIME.format(test), expected);
} else {
try {
DateTimeFormatter.ISO_ZONED_DATE_TIME.format(test);
fail(test.toString());
} catch (Exception ex) {
assertTrue(expectedEx.isInstance(ex));
}
}
}
示例4: test_parse_WeekBasedYear
import java.time.temporal.TemporalAccessor; //導入依賴的package包/類
@Test(dataProvider="LocalWeekBasedYearPatterns")
public void test_parse_WeekBasedYear(String pattern, String text, int pos, int expectedPos, LocalDate expectedValue) {
ParsePosition ppos = new ParsePosition(pos);
DateTimeFormatterBuilder b = new DateTimeFormatterBuilder().appendPattern(pattern);
DateTimeFormatter dtf = b.toFormatter(locale);
TemporalAccessor parsed = dtf.parseUnresolved(text, ppos);
if (ppos.getErrorIndex() != -1) {
assertEquals(ppos.getErrorIndex(), expectedPos);
} else {
WeekFields weekDef = WeekFields.of(locale);
assertEquals(ppos.getIndex(), expectedPos, "Incorrect ending parse position");
assertEquals(parsed.isSupported(weekDef.dayOfWeek()), pattern.indexOf('e') >= 0);
assertEquals(parsed.isSupported(weekDef.weekOfWeekBasedYear()), pattern.indexOf('w') >= 0);
assertEquals(parsed.isSupported(weekDef.weekBasedYear()), pattern.indexOf('Y') >= 0);
// ensure combination resolves into a date
LocalDate result = LocalDate.parse(text, dtf);
assertEquals(result, expectedValue, "LocalDate incorrect for " + pattern + ", weekDef: " + weekDef);
}
}
示例5: format
import java.time.temporal.TemporalAccessor; //導入依賴的package包/類
@Override
public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
Objects.requireNonNull(obj, "obj");
Objects.requireNonNull(toAppendTo, "toAppendTo");
Objects.requireNonNull(pos, "pos");
if (obj instanceof TemporalAccessor == false) {
throw new IllegalArgumentException("Format target must implement TemporalAccessor");
}
pos.setBeginIndex(0);
pos.setEndIndex(0);
try {
formatter.formatTo((TemporalAccessor) obj, toAppendTo);
} catch (RuntimeException ex) {
throw new IllegalArgumentException(ex.getMessage(), ex);
}
return toAppendTo;
}
示例6: test_parseSuccess_prefix
import java.time.temporal.TemporalAccessor; //導入依賴的package包/類
@Test(dataProvider="parseSuccess")
public void test_parseSuccess_prefix(String text, int expectedIndex, int expectedErrorIndex, ZoneId expected) {
builder.appendZoneId();
pos.setIndex(3);
String prefixText = "XXX" + text;
TemporalAccessor parsed = builder.toFormatter().parseUnresolved(prefixText, pos);
assertEquals(pos.getErrorIndex(), expectedErrorIndex >= 0 ? expectedErrorIndex + 3 : expectedErrorIndex, "Incorrect error index parsing: " + prefixText);
assertEquals(pos.getIndex(), expectedIndex + 3, "Incorrect index parsing: " + prefixText);
if (expected != null) {
assertEquals(parsed.query(TemporalQueries.zoneId()), expected, "Incorrect zoneId parsing: " + prefixText);
assertEquals(parsed.query(TemporalQueries.offset()), null, "Incorrect offset parsing: " + prefixText);
assertEquals(parsed.query(TemporalQueries.zone()), expected, "Incorrect zone parsing: " + prefixText);
} else {
assertEquals(parsed, null);
}
}
示例7: parseResolved0
import java.time.temporal.TemporalAccessor; //導入依賴的package包/類
/**
* Parses and resolves the specified text.
* <p>
* This parses to a {@code TemporalAccessor} ensuring that the text is fully parsed.
*
* @param text the text to parse, not null
* @param position the position to parse from, updated with length parsed
* and the index of any error, null if parsing whole string
* @return the resolved result of the parse, not null
* @throws DateTimeParseException if the parse fails
* @throws DateTimeException if an error occurs while resolving the date or time
* @throws IndexOutOfBoundsException if the position is invalid
*/
private TemporalAccessor parseResolved0(final CharSequence text, final ParsePosition position) {
ParsePosition pos = (position != null ? position : new ParsePosition(0));
DateTimeParseContext context = parseUnresolved0(text, pos);
if (context == null || pos.getErrorIndex() >= 0 || (position == null && pos.getIndex() < text.length())) {
String abbr;
if (text.length() > 64) {
abbr = text.subSequence(0, 64).toString() + "...";
} else {
abbr = text.toString();
}
if (pos.getErrorIndex() >= 0) {
throw new DateTimeParseException("Text '" + abbr + "' could not be parsed at index " +
pos.getErrorIndex(), text, pos.getErrorIndex());
} else {
throw new DateTimeParseException("Text '" + abbr + "' could not be parsed, unparsed text found at index " +
pos.getIndex(), text, pos.getIndex());
}
}
return context.toResolved(resolverStyle, resolverFields);
}
示例8: test_print_isoLocalTime
import java.time.temporal.TemporalAccessor; //導入依賴的package包/類
@Test(dataProvider="sample_isoLocalTime")
public void test_print_isoLocalTime(
Integer hour, Integer min, Integer sec, Integer nano, String offsetId, String zoneId,
String expected, Class<?> expectedEx) {
TemporalAccessor test = buildAccessor(null, null, null, hour, min, sec, nano, offsetId, zoneId);
if (expectedEx == null) {
assertEquals(DateTimeFormatter.ISO_LOCAL_TIME.format(test), expected);
} else {
try {
DateTimeFormatter.ISO_LOCAL_TIME.format(test);
fail();
} catch (Exception ex) {
assertTrue(expectedEx.isInstance(ex));
}
}
}
示例9: test_parse_textLocalDate
import java.time.temporal.TemporalAccessor; //導入依賴的package包/類
@Test(dataProvider="LocalWeekMonthYearPatterns")
public void test_parse_textLocalDate(String pattern, String text, int pos, int expectedPos, LocalDate expectedValue) {
ParsePosition ppos = new ParsePosition(pos);
DateTimeFormatterBuilder b = new DateTimeFormatterBuilder().appendPattern(pattern);
DateTimeFormatter dtf = b.toFormatter(locale);
TemporalAccessor parsed = dtf.parseUnresolved(text, ppos);
if (ppos.getErrorIndex() != -1) {
assertEquals(ppos.getErrorIndex(), expectedPos);
} else {
assertEquals(ppos.getIndex(), expectedPos, "Incorrect ending parse position");
assertEquals(parsed.isSupported(YEAR_OF_ERA), true);
assertEquals(parsed.isSupported(WeekFields.of(locale).dayOfWeek()), true);
assertEquals(parsed.isSupported(WeekFields.of(locale).weekOfMonth()) ||
parsed.isSupported(WeekFields.of(locale).weekOfYear()), true);
// ensure combination resolves into a date
LocalDate result = LocalDate.parse(text, dtf);
assertEquals(result, expectedValue, "LocalDate incorrect for " + pattern);
}
}
示例10: test_print_isoOffsetDateTime
import java.time.temporal.TemporalAccessor; //導入依賴的package包/類
@Test(dataProvider="sample_isoOffsetDateTime")
public void test_print_isoOffsetDateTime(
Integer year, Integer month, Integer day,
Integer hour, Integer min, Integer sec, Integer nano, String offsetId, String zoneId,
String expected, Class<?> expectedEx) {
TemporalAccessor test = buildAccessor(year, month, day, hour, min, sec, nano, offsetId, zoneId);
if (expectedEx == null) {
assertEquals(DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(test), expected);
} else {
try {
DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(test);
fail();
} catch (Exception ex) {
assertTrue(expectedEx.isInstance(ex));
}
}
}
示例11: test_print_isoDateTime
import java.time.temporal.TemporalAccessor; //導入依賴的package包/類
@Test(dataProvider="sample_isoDateTime")
public void test_print_isoDateTime(
Integer year, Integer month, Integer day,
Integer hour, Integer min, Integer sec, Integer nano, String offsetId, String zoneId,
String expected, Class<?> expectedEx) {
TemporalAccessor test = buildAccessor(year, month, day, hour, min, sec, nano, offsetId, zoneId);
if (expectedEx == null) {
assertEquals(DateTimeFormatter.ISO_DATE_TIME.format(test), expected);
} else {
try {
DateTimeFormatter.ISO_DATE_TIME.format(test);
fail();
} catch (Exception ex) {
assertTrue(expectedEx.isInstance(ex));
}
}
}
示例12: test_resolveOneToField
import java.time.temporal.TemporalAccessor; //導入依賴的package包/類
@Test(dataProvider="resolveOneToField")
public void test_resolveOneToField(TemporalField field1, long value1,
TemporalField expectedField1, Long expectedValue1,
TemporalField expectedField2, Long expectedValue2) {
String str = Long.toString(value1);
DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(field1).toFormatter();
TemporalAccessor accessor = f.parse(str);
assertEquals(accessor.query(TemporalQueries.localDate()), null);
assertEquals(accessor.query(TemporalQueries.localTime()), null);
if (expectedField1 != null) {
assertEquals(accessor.isSupported(expectedField1), true);
assertEquals(accessor.getLong(expectedField1), expectedValue1.longValue());
}
if (expectedField2 != null) {
assertEquals(accessor.isSupported(expectedField2), true);
assertEquals(accessor.getLong(expectedField2), expectedValue2.longValue());
}
}
示例13: test_print_isoOffsetTime
import java.time.temporal.TemporalAccessor; //導入依賴的package包/類
@Test(dataProvider="sample_isoOffsetTime")
public void test_print_isoOffsetTime(
Integer hour, Integer min, Integer sec, Integer nano, String offsetId, String zoneId,
String expected, Class<?> expectedEx) {
TemporalAccessor test = buildAccessor(null, null, null, hour, min, sec, nano, offsetId, zoneId);
if (expectedEx == null) {
assertEquals(DateTimeFormatter.ISO_OFFSET_TIME.format(test), expected);
} else {
try {
DateTimeFormatter.ISO_OFFSET_TIME.format(test);
fail();
} catch (Exception ex) {
assertTrue(expectedEx.isInstance(ex));
}
}
}
示例14: test_print_isoOffsetDate
import java.time.temporal.TemporalAccessor; //導入依賴的package包/類
@Test(dataProvider="sample_isoOffsetDate")
public void test_print_isoOffsetDate(
Integer year, Integer month, Integer day, String offsetId, String zoneId,
String expected, Class<?> expectedEx) {
TemporalAccessor test = buildAccessor(year, month, day, null, null, null, null, offsetId, zoneId);
if (expectedEx == null) {
assertEquals(DateTimeFormatter.ISO_OFFSET_DATE.format(test), expected);
} else {
try {
DateTimeFormatter.ISO_OFFSET_DATE.format(test);
fail();
} catch (Exception ex) {
assertTrue(expectedEx.isInstance(ex));
}
}
}
示例15: test_reducedWithLateChronoChangeTwice
import java.time.temporal.TemporalAccessor; //導入依賴的package包/類
@Test
public void test_reducedWithLateChronoChangeTwice() {
DateTimeFormatter df
= new DateTimeFormatterBuilder()
.appendValueReduced(YEAR, 2, 2, LocalDate.of(2000, 1, 1))
.appendLiteral(" ")
.appendChronologyId()
.appendLiteral(" ")
.appendChronologyId()
.toFormatter();
int expected = 2044;
String input = "44 ThaiBuddhist ISO";
ParsePosition pos = new ParsePosition(0);
TemporalAccessor parsed = df.parseUnresolved(input, pos);
assertEquals(pos.getIndex(), input.length(), "Input not parsed completely: " + pos);
assertEquals(pos.getErrorIndex(), -1, "Error index should be -1 (no-error)");
int actual = parsed.get(YEAR);
assertEquals(actual, expected,
String.format("Wrong date parsed, chrono: %s, input: %s",
parsed.query(TemporalQueries.chronology()), input));
}