本文整理匯總了Java中java.time.format.DateTimeFormatterBuilder.appendLiteral方法的典型用法代碼示例。如果您正苦於以下問題:Java DateTimeFormatterBuilder.appendLiteral方法的具體用法?Java DateTimeFormatterBuilder.appendLiteral怎麽用?Java DateTimeFormatterBuilder.appendLiteral使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.time.format.DateTimeFormatterBuilder
的用法示例。
在下文中一共展示了DateTimeFormatterBuilder.appendLiteral方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: test_resolverStyle
import java.time.format.DateTimeFormatterBuilder; //導入方法依賴的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));
}
}
}