本文整理汇总了Java中java.time.format.DateTimeFormatter.withResolverStyle方法的典型用法代码示例。如果您正苦于以下问题:Java DateTimeFormatter.withResolverStyle方法的具体用法?Java DateTimeFormatter.withResolverStyle怎么用?Java DateTimeFormatter.withResolverStyle使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.time.format.DateTimeFormatter
的用法示例。
在下文中一共展示了DateTimeFormatter.withResolverStyle方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: test_resolverStyle
import java.time.format.DateTimeFormatter; //导入方法依赖的package包/类
@Test(dataProvider = "resolverStyle")
public void test_resolverStyle(String str, ResolverStyle style, Class<?> expectedEx, int year, int month, int day) {
DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
builder.appendValue(ChronoField.YEAR_OF_ERA);
builder.appendLiteral("/");
builder.appendValue(ChronoField.MONTH_OF_YEAR);
builder.appendLiteral("/");
builder.appendValue(ChronoField.DAY_OF_MONTH);
Map<Long, String> eraMap = new HashMap<Long, String>();
eraMap.put(1L, "CE");
eraMap.put(0L, "BCE");
DateTimeFormatter optionalFormatter = new DateTimeFormatterBuilder().appendLiteral(" ").appendText(ChronoField.ERA, eraMap).toFormatter();
DateTimeFormatter formatter = builder.appendOptional(optionalFormatter).toFormatter();
formatter = formatter.withResolverStyle(style);
if (expectedEx == null) {
TemporalAccessor accessor = formatter.parse(str);
assertEquals(accessor.get(ChronoField.YEAR_OF_ERA), year);
assertEquals(accessor.get(ChronoField.MONTH_OF_YEAR), month);
assertEquals(accessor.get(ChronoField.DAY_OF_MONTH), day);
} else {
try {
formatter.parse(str);
fail();
} catch (Exception ex) {
assertTrue(expectedEx.isInstance(ex));
}
}
}