本文整理汇总了Java中java.time.format.DateTimeFormatter.parseUnresolved方法的典型用法代码示例。如果您正苦于以下问题:Java DateTimeFormatter.parseUnresolved方法的具体用法?Java DateTimeFormatter.parseUnresolved怎么用?Java DateTimeFormatter.parseUnresolved使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.time.format.DateTimeFormatter
的用法示例。
在下文中一共展示了DateTimeFormatter.parseUnresolved方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: test_parse_textLocalDate
import java.time.format.DateTimeFormatter; //导入方法依赖的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);
}
}
示例2: test_parse_textField
import java.time.format.DateTimeFormatter; //导入方法依赖的package包/类
@Test(dataProvider="parseData")
public void test_parse_textField(int minWidth, int maxWidth, SignStyle signStyle, int subsequentWidth, String text, int pos, int expectedPos, long expectedValue) {
ParsePosition ppos = new ParsePosition(pos);
DateTimeFormatter dtf = getFormatter(DAY_OF_WEEK, minWidth, maxWidth, signStyle);
if (subsequentWidth > 0) {
// hacky, to reserve space
dtf = builder.appendValue(DAY_OF_YEAR, subsequentWidth).toFormatter(locale).withDecimalStyle(decimalStyle);
}
TemporalAccessor parsed = dtf.parseUnresolved(text, ppos);
if (ppos.getErrorIndex() != -1) {
assertEquals(ppos.getErrorIndex(), expectedPos);
} else {
assertTrue(subsequentWidth >= 0);
assertEquals(ppos.getIndex(), expectedPos + subsequentWidth);
assertEquals(parsed.getLong(DAY_OF_WEEK), expectedValue);
assertEquals(parsed.query(TemporalQueries.chronology()), null);
assertEquals(parsed.query(TemporalQueries.zoneId()), null);
}
}
示例3: test_parseDigitsAdjacentLenient
import java.time.format.DateTimeFormatter; //导入方法依赖的package包/类
@Test(dataProvider="parseDigitsAdjacentLenient")
public void test_parseDigitsAdjacentLenient(String input, int parseLen, Integer parseMonth, Integer parsedDay) throws Exception {
setStrict(false);
ParsePosition pos = new ParsePosition(0);
DateTimeFormatter f = builder
.appendValue(MONTH_OF_YEAR, 1, 2, SignStyle.NORMAL)
.appendValue(DAY_OF_MONTH, 2).toFormatter(locale).withDecimalStyle(decimalStyle);
TemporalAccessor parsed = f.parseUnresolved(input, pos);
if (pos.getErrorIndex() != -1) {
assertEquals(pos.getErrorIndex(), parseLen);
} else {
assertEquals(pos.getIndex(), parseLen);
assertEquals(parsed.getLong(MONTH_OF_YEAR), (long) parseMonth);
assertEquals(parsed.getLong(DAY_OF_MONTH), (long) parsedDay);
assertEquals(parsed.query(TemporalQueries.chronology()), null);
assertEquals(parsed.query(TemporalQueries.zoneId()), null);
}
}
示例4: test_reducedWithLateChronoChange
import java.time.format.DateTimeFormatter; //导入方法依赖的package包/类
@Test
public void test_reducedWithLateChronoChange() {
ThaiBuddhistDate date = ThaiBuddhistDate.of(2543, 1, 1);
DateTimeFormatter df
= new DateTimeFormatterBuilder()
.appendValueReduced(YEAR, 2, 2, LocalDate.of(2000, 1, 1))
.appendLiteral(" ")
.appendChronologyId()
.toFormatter();
int expected = date.get(YEAR);
String input = df.format(date);
ParsePosition pos = new ParsePosition(0);
TemporalAccessor parsed = df.parseUnresolved(input, pos);
assertEquals(pos.getIndex(), input.length(), "Input not parsed completely");
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));
}
示例5: test_appendValueReduced
import java.time.format.DateTimeFormatter; //导入方法依赖的package包/类
@Test
public void test_appendValueReduced() throws Exception {
builder.appendValueReduced(YEAR, 2, 2, 2000);
DateTimeFormatter f = builder.toFormatter();
assertEquals(f.toString(), "ReducedValue(Year,2,2,2000)");
TemporalAccessor parsed = f.parseUnresolved("12", new ParsePosition(0));
assertEquals(parsed.getLong(YEAR), 2012L);
}
示例6: test_parseUnresolved_StringParsePosition_duplicateFieldDifferentValue
import java.time.format.DateTimeFormatter; //导入方法依赖的package包/类
@Test
public void test_parseUnresolved_StringParsePosition_duplicateFieldDifferentValue() {
DateTimeFormatter test = new DateTimeFormatterBuilder()
.appendValue(MONTH_OF_YEAR).appendLiteral('-').appendValue(MONTH_OF_YEAR).toFormatter();
ParsePosition pos = new ParsePosition(3);
TemporalAccessor result = test.parseUnresolved("XXX6-7", pos);
assertEquals(pos.getIndex(), 3);
assertEquals(pos.getErrorIndex(), 5);
assertEquals(result, null);
}
示例7: test_parseUnresolved_StringParsePosition_parseError
import java.time.format.DateTimeFormatter; //导入方法依赖的package包/类
@Test
public void test_parseUnresolved_StringParsePosition_parseError() {
DateTimeFormatter test = fmt.withLocale(Locale.ENGLISH).withDecimalStyle(DecimalStyle.STANDARD);
ParsePosition pos = new ParsePosition(0);
TemporalAccessor result = test.parseUnresolved("ONEXXX", pos);
assertEquals(pos.getIndex(), 0);
assertEquals(pos.getErrorIndex(), 3);
assertEquals(result, null);
}
示例8: test_adjacent_lenient_fractionFollows_0digit
import java.time.format.DateTimeFormatter; //导入方法依赖的package包/类
@Test
public void test_adjacent_lenient_fractionFollows_0digit() throws Exception {
// succeeds because hour, min and fraction of seconds are fixed width
DateTimeFormatter f = builder.parseLenient().appendValue(HOUR_OF_DAY, 2).appendValue(MINUTE_OF_HOUR, 2).appendFraction(NANO_OF_SECOND, 3, 3, false).toFormatter(Locale.UK);
ParsePosition pp = new ParsePosition(0);
TemporalAccessor parsed = f.parseUnresolved("1230", pp);
assertEquals(pp.getErrorIndex(), -1);
assertEquals(pp.getIndex(), 4);
assertEquals(parsed.getLong(HOUR_OF_DAY), 12L);
assertEquals(parsed.getLong(MINUTE_OF_HOUR), 30L);
}
示例9: test_adjacent_strict_firstVariableWidth_fails
import java.time.format.DateTimeFormatter; //导入方法依赖的package包/类
@Test
public void test_adjacent_strict_firstVariableWidth_fails() throws Exception {
// fails because literal is a number and variable width parse greedily absorbs it
DateTimeFormatter f = builder.appendValue(HOUR_OF_DAY).appendValue(MINUTE_OF_HOUR, 2).appendLiteral('9').toFormatter(Locale.UK);
ParsePosition pp = new ParsePosition(0);
TemporalAccessor parsed = f.parseUnresolved("12309", pp);
assertEquals(pp.getErrorIndex(), 5);
assertEquals(parsed, null);
}
示例10: test_adjacent_strict_firstVariableWidth_success
import java.time.format.DateTimeFormatter; //导入方法依赖的package包/类
@Test
public void test_adjacent_strict_firstVariableWidth_success() throws Exception {
// succeeds greedily parsing variable width, then fixed width, to non-numeric Z
DateTimeFormatter f = builder.appendValue(HOUR_OF_DAY).appendValue(MINUTE_OF_HOUR, 2).appendLiteral('Z').toFormatter(Locale.UK);
ParsePosition pp = new ParsePosition(0);
TemporalAccessor parsed = f.parseUnresolved("12309Z", pp);
assertEquals(pp.getErrorIndex(), -1);
assertEquals(pp.getIndex(), 6);
assertEquals(parsed.getLong(HOUR_OF_DAY), 123L);
assertEquals(parsed.getLong(MINUTE_OF_HOUR), 9L);
}
示例11: test_appendValue_subsequent2_parse5
import java.time.format.DateTimeFormatter; //导入方法依赖的package包/类
@Test
public void test_appendValue_subsequent2_parse5() throws Exception {
builder.appendValue(MONTH_OF_YEAR, 1, 2, SignStyle.NORMAL).appendValue(DAY_OF_MONTH, 2).appendLiteral('4');
DateTimeFormatter f = builder.toFormatter();
assertEquals(f.toString(), "Value(MonthOfYear,1,2,NORMAL)Value(DayOfMonth,2)'4'");
TemporalAccessor parsed = f.parseUnresolved("01234", new ParsePosition(0));
assertEquals(parsed.getLong(MONTH_OF_YEAR), 1L);
assertEquals(parsed.getLong(DAY_OF_MONTH), 23L);
}
示例12: test_appendValue_subsequent3_parse6
import java.time.format.DateTimeFormatter; //导入方法依赖的package包/类
@Test
public void test_appendValue_subsequent3_parse6() throws Exception {
builder
.appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
.appendValue(MONTH_OF_YEAR, 2)
.appendValue(DAY_OF_MONTH, 2);
DateTimeFormatter f = builder.toFormatter();
assertEquals(f.toString(), "Value(Year,4,10,EXCEEDS_PAD)Value(MonthOfYear,2)Value(DayOfMonth,2)");
TemporalAccessor parsed = f.parseUnresolved("20090630", new ParsePosition(0));
assertEquals(parsed.getLong(YEAR), 2009L);
assertEquals(parsed.getLong(MONTH_OF_YEAR), 6L);
assertEquals(parsed.getLong(DAY_OF_MONTH), 30L);
}
示例13: test_adjacent_lenient_fractionFollows_0digit
import java.time.format.DateTimeFormatter; //导入方法依赖的package包/类
@Test
public void test_adjacent_lenient_fractionFollows_0digit() throws Exception {
// succeeds because hour/min are fixed width
DateTimeFormatter f = builder.parseLenient().appendValue(HOUR_OF_DAY, 2).appendValue(MINUTE_OF_HOUR, 2).appendFraction(NANO_OF_SECOND, 3, 3, false).toFormatter(Locale.UK);
ParsePosition pp = new ParsePosition(0);
TemporalAccessor parsed = f.parseUnresolved("1230", pp);
assertEquals(pp.getErrorIndex(), -1);
assertEquals(pp.getIndex(), 4);
assertEquals(parsed.getLong(HOUR_OF_DAY), 12L);
assertEquals(parsed.getLong(MINUTE_OF_HOUR), 30L);
}
示例14: test_appendValue_subsequent2_parse3
import java.time.format.DateTimeFormatter; //导入方法依赖的package包/类
@Test
public void test_appendValue_subsequent2_parse3() throws Exception {
builder.appendValue(MONTH_OF_YEAR, 1, 2, SignStyle.NORMAL).appendValue(DAY_OF_MONTH, 2);
DateTimeFormatter f = builder.toFormatter();
assertEquals(f.toString(), "Value(MonthOfYear,1,2,NORMAL)Value(DayOfMonth,2)");
TemporalAccessor parsed = f.parseUnresolved("123", new ParsePosition(0));
assertEquals(parsed.getLong(MONTH_OF_YEAR), 1L);
assertEquals(parsed.getLong(DAY_OF_MONTH), 23L);
}
示例15: test_adjacent_lenient
import java.time.format.DateTimeFormatter; //导入方法依赖的package包/类
@Test
public void test_adjacent_lenient() throws Exception {
// succeeds because both number elements are fixed width even in lenient mode
DateTimeFormatter f = builder.parseLenient().appendValue(HOUR_OF_DAY, 2).appendValue(MINUTE_OF_HOUR, 2).appendLiteral('9').toFormatter(Locale.UK);
ParsePosition pp = new ParsePosition(0);
TemporalAccessor parsed = f.parseUnresolved("12309", pp);
assertEquals(pp.getErrorIndex(), -1);
assertEquals(pp.getIndex(), 5);
assertEquals(parsed.getLong(HOUR_OF_DAY), 12L);
assertEquals(parsed.getLong(MINUTE_OF_HOUR), 30L);
}