下面列出了java.time.format.DateTimeFormatterBuilder#appendPattern ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test(dataProvider="ParseAdjacent")
public void test_parseAdjacent(String pattern, String input, boolean strict, int pos, int parseLen, int year, int month, int day) {
ParsePosition ppos = new ParsePosition(0);
builder = new DateTimeFormatterBuilder();
setStrict(strict);
builder.appendPattern(pattern);
DateTimeFormatter dtf = builder.toFormatter();
TemporalAccessor parsed = dtf.parseUnresolved(input, ppos);
assertNotNull(parsed, String.format("parse failed: ppos: %s, formatter: %s%n", ppos.toString(), dtf));
if (ppos.getErrorIndex() != -1) {
assertEquals(ppos.getErrorIndex(), parseLen, "error case parse position");
} else {
assertEquals(ppos.getIndex(), parseLen, "parse position");
assertParsed(parsed, YEAR_OF_ERA, Long.valueOf(year));
assertParsed(parsed, MONTH_OF_YEAR, Long.valueOf(month));
assertParsed(parsed, DAY_OF_MONTH, Long.valueOf(day));
}
}
@Test(dataProvider="ParseAdjacent")
public void test_parseAdjacent(String pattern, String input, boolean strict, int pos, int parseLen, int year, int month, int day) {
ParsePosition ppos = new ParsePosition(0);
builder = new DateTimeFormatterBuilder();
setStrict(strict);
builder.appendPattern(pattern);
DateTimeFormatter dtf = builder.toFormatter();
TemporalAccessor parsed = dtf.parseUnresolved(input, ppos);
assertNotNull(parsed, String.format("parse failed: ppos: %s, formatter: %s%n", ppos.toString(), dtf));
if (ppos.getErrorIndex() != -1) {
assertEquals(ppos.getErrorIndex(), parseLen, "error case parse position");
} else {
assertEquals(ppos.getIndex(), parseLen, "parse position");
assertParsed(parsed, YEAR_OF_ERA, Long.valueOf(year));
assertParsed(parsed, MONTH_OF_YEAR, Long.valueOf(month));
assertParsed(parsed, DAY_OF_MONTH, Long.valueOf(day));
}
}
@Test(dataProvider="ParseAdjacent")
public void test_parseAdjacent(String pattern, String input, boolean strict, int pos, int parseLen, int year, int month, int day) {
ParsePosition ppos = new ParsePosition(0);
builder = new DateTimeFormatterBuilder();
setStrict(strict);
builder.appendPattern(pattern);
DateTimeFormatter dtf = builder.toFormatter();
TemporalAccessor parsed = dtf.parseUnresolved(input, ppos);
assertNotNull(parsed, String.format("parse failed: ppos: %s, formatter: %s%n", ppos.toString(), dtf));
if (ppos.getErrorIndex() != -1) {
assertEquals(ppos.getErrorIndex(), parseLen, "error case parse position");
} else {
assertEquals(ppos.getIndex(), parseLen, "parse position");
assertParsed(parsed, YEAR_OF_ERA, Long.valueOf(year));
assertParsed(parsed, MONTH_OF_YEAR, Long.valueOf(month));
assertParsed(parsed, DAY_OF_MONTH, Long.valueOf(day));
}
}
@Test(dataProvider="ParseAdjacent")
public void test_parseAdjacent(String pattern, String input, boolean strict, int pos, int parseLen, int year, int month, int day) {
ParsePosition ppos = new ParsePosition(0);
builder = new DateTimeFormatterBuilder();
setStrict(strict);
builder.appendPattern(pattern);
DateTimeFormatter dtf = builder.toFormatter();
TemporalAccessor parsed = dtf.parseUnresolved(input, ppos);
assertNotNull(parsed, String.format("parse failed: ppos: %s, formatter: %s%n", ppos.toString(), dtf));
if (ppos.getErrorIndex() != -1) {
assertEquals(ppos.getErrorIndex(), parseLen, "error case parse position");
} else {
assertEquals(ppos.getIndex(), parseLen, "parse position");
assertParsed(parsed, YEAR_OF_ERA, Long.valueOf(year));
assertParsed(parsed, MONTH_OF_YEAR, Long.valueOf(month));
assertParsed(parsed, DAY_OF_MONTH, Long.valueOf(day));
}
}
@Test(dataProvider="ParseAdjacent")
public void test_parseAdjacent(String pattern, String input, boolean strict, int pos, int parseLen, int year, int month, int day) {
ParsePosition ppos = new ParsePosition(0);
builder = new DateTimeFormatterBuilder();
setStrict(strict);
builder.appendPattern(pattern);
DateTimeFormatter dtf = builder.toFormatter();
TemporalAccessor parsed = dtf.parseUnresolved(input, ppos);
assertNotNull(parsed, String.format("parse failed: ppos: %s, formatter: %s%n", ppos.toString(), dtf));
if (ppos.getErrorIndex() != -1) {
assertEquals(ppos.getErrorIndex(), parseLen, "error case parse position");
} else {
assertEquals(ppos.getIndex(), parseLen, "parse position");
assertParsed(parsed, YEAR_OF_ERA, Long.valueOf(year));
assertParsed(parsed, MONTH_OF_YEAR, Long.valueOf(month));
assertParsed(parsed, DAY_OF_MONTH, Long.valueOf(day));
}
}
@Test(dataProvider="ParseAdjacent")
public void test_parseAdjacent(String pattern, String input, boolean strict, int pos, int parseLen, int year, int month, int day) {
ParsePosition ppos = new ParsePosition(0);
builder = new DateTimeFormatterBuilder();
setStrict(strict);
builder.appendPattern(pattern);
DateTimeFormatter dtf = builder.toFormatter();
TemporalAccessor parsed = dtf.parseUnresolved(input, ppos);
assertNotNull(parsed, String.format("parse failed: ppos: %s, formatter: %s%n", ppos.toString(), dtf));
if (ppos.getErrorIndex() != -1) {
assertEquals(ppos.getErrorIndex(), parseLen, "error case parse position");
} else {
assertEquals(ppos.getIndex(), parseLen, "parse position");
assertParsed(parsed, YEAR_OF_ERA, Long.valueOf(year));
assertParsed(parsed, MONTH_OF_YEAR, Long.valueOf(month));
assertParsed(parsed, DAY_OF_MONTH, Long.valueOf(day));
}
}
@Test(dataProvider="ParseAdjacent")
public void test_parseAdjacent(String pattern, String input, boolean strict, int pos, int parseLen, int year, int month, int day) {
ParsePosition ppos = new ParsePosition(0);
builder = new DateTimeFormatterBuilder();
setStrict(strict);
builder.appendPattern(pattern);
DateTimeFormatter dtf = builder.toFormatter();
TemporalAccessor parsed = dtf.parseUnresolved(input, ppos);
assertNotNull(parsed, String.format("parse failed: ppos: %s, formatter: %s%n", ppos.toString(), dtf));
if (ppos.getErrorIndex() != -1) {
assertEquals(ppos.getErrorIndex(), parseLen, "error case parse position");
} else {
assertEquals(ppos.getIndex(), parseLen, "parse position");
assertParsed(parsed, YEAR_OF_ERA, Long.valueOf(year));
assertParsed(parsed, MONTH_OF_YEAR, Long.valueOf(month));
assertParsed(parsed, DAY_OF_MONTH, Long.valueOf(day));
}
}
@Test(dataProvider="PrintAdjacent")
public void test_printAdjacent(String pattern, String text, int year, int month, int day) {
builder = new DateTimeFormatterBuilder();
builder.appendPattern(pattern);
DateTimeFormatter dtf = builder.toFormatter();
LocalDate ld = LocalDate.of(year, month, day);
String actual = dtf.format(ld);
assertEquals(actual, text, "formatter output: " + dtf);
}
@Test(dataProvider="PrintAdjacent")
public void test_printAdjacent(String pattern, String text, int year, int month, int day) {
builder = new DateTimeFormatterBuilder();
builder.appendPattern(pattern);
DateTimeFormatter dtf = builder.toFormatter();
LocalDate ld = LocalDate.of(year, month, day);
String actual = dtf.format(ld);
assertEquals(actual, text, "formatter output: " + dtf);
}
@Test(dataProvider="PrintAdjacent")
public void test_printAdjacent(String pattern, String text, int year, int month, int day) {
builder = new DateTimeFormatterBuilder();
builder.appendPattern(pattern);
DateTimeFormatter dtf = builder.toFormatter();
LocalDate ld = LocalDate.of(year, month, day);
String actual = dtf.format(ld);
assertEquals(actual, text, "formatter output: " + dtf);
}
@Test(dataProvider="PrintAdjacent")
public void test_printAdjacent(String pattern, String text, int year, int month, int day) {
builder = new DateTimeFormatterBuilder();
builder.appendPattern(pattern);
DateTimeFormatter dtf = builder.toFormatter();
LocalDate ld = LocalDate.of(year, month, day);
String actual = dtf.format(ld);
assertEquals(actual, text, "formatter output: " + dtf);
}
@Test(dataProvider="PrintAdjacent")
public void test_printAdjacent(String pattern, String text, int year, int month, int day) {
builder = new DateTimeFormatterBuilder();
builder.appendPattern(pattern);
DateTimeFormatter dtf = builder.toFormatter();
LocalDate ld = LocalDate.of(year, month, day);
String actual = dtf.format(ld);
assertEquals(actual, text, "formatter output: " + dtf);
}
@Test(dataProvider="PrintAdjacent")
public void test_printAdjacent(String pattern, String text, int year, int month, int day) {
builder = new DateTimeFormatterBuilder();
builder.appendPattern(pattern);
DateTimeFormatter dtf = builder.toFormatter();
LocalDate ld = LocalDate.of(year, month, day);
String actual = dtf.format(ld);
assertEquals(actual, text, "formatter output: " + dtf);
}
@Test(dataProvider="PrintAdjacent")
public void test_printAdjacent(String pattern, String text, int year, int month, int day) {
builder = new DateTimeFormatterBuilder();
builder.appendPattern(pattern);
DateTimeFormatter dtf = builder.toFormatter();
LocalDate ld = LocalDate.of(year, month, day);
String actual = dtf.format(ld);
assertEquals(actual, text, "formatter output: " + dtf);
}
/**
* The delegate {@link DateTimeFormatter} for formatting a {@link SlackDateTime} outside of the context of Slack.
*
* @return the {@link DateTimeFormatter} to use when formatting a {@link SlackDateTime} outside of Slack
*/
@Value.Derived
protected DateTimeFormatter delegateFormatter() {
DateTimeFormatterBuilder formatterBuilder = new DateTimeFormatterBuilder();
for (String patternToken : tokenizePattern(pattern())) {
Optional<DateTimeFormatToken> formatToken = DateTimeFormatToken.ofSafe(patternToken);
if (formatToken.isPresent()) {
formatterBuilder.appendPattern(formatToken.get().pattern());
} else {
formatterBuilder.appendLiteral(patternToken);
}
}
return formatterBuilder.toFormatter();
}
@Test(dataProvider="PrintAdjacent")
public void test_printAdjacent(String pattern, String text, int year, int month, int day) {
builder = new DateTimeFormatterBuilder();
builder.appendPattern(pattern);
DateTimeFormatter dtf = builder.toFormatter();
LocalDate ld = LocalDate.of(year, month, day);
String actual = dtf.format(ld);
assertEquals(actual, text, "formatter output: " + dtf);
}
@Override
public DateTimeFormatter getGMTDateTimeFormatter() {
if (null == dateTimeFormatter_) {
DateTimeFormatterBuilder dfb1 = new DateTimeFormatterBuilder();
DateTimeFormatterBuilder dfb2 = new DateTimeFormatterBuilder();
International i = getInternational();
String sep = i.getDateSep();
if (i.isDateMDY()) {
dfb1.appendPattern("MM" + sep + "dd" + sep + "uuuu");
dfb2.appendPattern("MM" + sep + "dd" + sep).appendValueReduced(ChronoField.YEAR, 2, 2, Year.now().getValue() - 80);
} else if (i.isDateDMY()) {
dfb1.appendPattern("dd" + sep + "MM" + sep + "uuuu");
dfb2.appendPattern("dd" + sep + "MM" + sep).appendValueReduced(ChronoField.YEAR, 2, 2, Year.now().getValue() - 80);
} else {
dfb1.appendPattern("uuuu" + sep + "MM" + sep + "dd");
dfb2.appendValueReduced(ChronoField.YEAR, 2, 2, Year.now().getValue() - 80).appendPattern(sep + "MM" + sep + "dd");
}
dfb1.appendPattern(" " + getTimeFormatForDateTimeFormatter());
dfb2.appendPattern(" " + getTimeFormatForDateTimeFormatter());
DateTimeFormatterBuilder dfb = new DateTimeFormatterBuilder();
dfb.appendOptional(dfb1.parseCaseInsensitive().toFormatter(Locale.ENGLISH))
.appendOptional(dfb2.parseCaseInsensitive().toFormatter(Locale.ENGLISH));
dateTimeFormatter_ = dfb.parseCaseInsensitive().toFormatter(Locale.ENGLISH).withResolverStyle(ResolverStyle.LENIENT)
.withZone(ZoneId.ofOffset("GMT", ZoneOffset.UTC));
}
return dateTimeFormatter_;
}
@Override
public void build(DateTimeFormatterBuilder dtfb, Args args,
boolean hasAmPm, Value.Type dtType) {
dtfb.appendPattern(args._ctx.getTemporalConfig().getDateTimeFormat(_type));
}
@Override
public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
BeanProperty property) throws JsonMappingException
{
JsonFormat.Value format = findFormatOverrides(ctxt, property, handledType());
JSR310DateTimeDeserializerBase<?> deser = this;
if (format != null) {
// 17-Aug-2019, tatu: For 2.10 let's start considering leniency/strictness too
if (format.hasLenient()) {
Boolean leniency = format.getLenient();
if (leniency != null) {
deser = deser.withLeniency(leniency);
}
}
if (format.hasPattern()) {
final String pattern = format.getPattern();
final Locale locale = format.hasLocale() ? format.getLocale() : ctxt.getLocale();
DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
if (acceptCaseInsensitiveValues(ctxt, format)) {
builder.parseCaseInsensitive();
}
builder.appendPattern(pattern);
DateTimeFormatter df;
if (locale == null) {
df = builder.toFormatter();
} else {
df = builder.toFormatter(locale);
}
// [#148]: allow strict parsing
if (!deser.isLenient()) {
df = df.withResolverStyle(ResolverStyle.STRICT);
}
// [#69]: For instant serializers/deserializers we need to configure the formatter with
//a time zone picked up from JsonFormat annotation, otherwise serialization might not work
if (format.hasTimeZone()) {
df = df.withZone(format.getTimeZone().toZoneId());
}
deser = deser.withDateFormat(df);
}
// [#58]: For LocalDate deserializers we need to configure the formatter with
//a shape picked up from JsonFormat annotation, to decide if the value is EpochSeconds
JsonFormat.Shape shape = format.getShape();
if (shape != null && shape != _shape) {
deser = deser.withShape(shape);
}
// any use for TimeZone?
}
return deser;
}
@Override
public void build(DateTimeFormatterBuilder dtfb, Args args,
boolean hasAmPm, Value.Type dtType) {
dtfb.appendPattern(_pat);
}