下面列出了怎么用java.time.format.SignStyle的API类实例代码及写法,或者点击链接到github查看源代码。
@Test(dataProvider = "signStyle")
public void test_signStyle(LocalDate localDate, SignStyle style, Class<?> expectedEx, String expectedStr) {
DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
DateTimeFormatter formatter = builder.appendValue(ChronoField.YEAR, 2, 4, style)
.toFormatter();
formatter = formatter.withZone(ZoneOffset.UTC);
if (expectedEx == null) {
String output = formatter.format(localDate);
assertEquals(output, expectedStr);
} else {
try {
formatter.format(localDate);
fail();
} catch (Exception ex) {
assertTrue(expectedEx.isInstance(ex));
}
}
}
@Test(dataProvider="Pad")
public void test_pad_EXCEEDS_PAD(int minPad, int maxPad, long value, String result) throws Exception {
try {
getFormatter(DAY_OF_MONTH, minPad, maxPad, SignStyle.EXCEEDS_PAD).formatTo(new MockFieldValue(DAY_OF_MONTH, value), buf);
if (result == null) {
fail("Expected exception");
return; // unreachable
}
if (result.length() > minPad || value < 0) {
result = (value < 0 ? "-" + result : "+" + result);
}
assertEquals(buf.toString(), result);
} catch (DateTimeException ex) {
if (result != null) {
throw ex;
}
assertEquals(ex.getMessage().contains(DAY_OF_MONTH.toString()), true);
}
}
@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);
}
}
@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);
}
}
@Test(dataProvider = "signStyle")
public void test_signStyle(LocalDate localDate, SignStyle style, Class<?> expectedEx, String expectedStr) {
DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
DateTimeFormatter formatter = builder.appendValue(ChronoField.YEAR, 2, 4, style)
.toFormatter();
formatter = formatter.withZone(ZoneOffset.UTC);
if (expectedEx == null) {
String output = formatter.format(localDate);
assertEquals(output, expectedStr);
} else {
try {
formatter.format(localDate);
fail();
} catch (Exception ex) {
assertTrue(expectedEx.isInstance(ex));
}
}
}
@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);
}
}
@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);
}
}
@Test(dataProvider="parseData")
public void test_parse_fresh(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_MONTH, 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_MONTH), expectedValue);
assertEquals(parsed.query(TemporalQueries.chronology()), null);
assertEquals(parsed.query(TemporalQueries.zoneId()), null);
}
}
@Test(dataProvider="parseLenient")
public void test_parseLenient(String text, int expectedIndex, int expectedErrorIndex, Number expectedMonth) {
builder.parseLenient().padNext(3, '#').appendValue(MONTH_OF_YEAR, 1, 3, SignStyle.NORMAL);
TemporalAccessor parsed = builder.toFormatter().parseUnresolved(text, pos);
assertEquals(pos.getIndex(), expectedIndex);
assertEquals(pos.getErrorIndex(), expectedErrorIndex);
if (expectedMonth != null) {
assertEquals(parsed.isSupported(MONTH_OF_YEAR), true);
assertEquals(parsed.getLong(MONTH_OF_YEAR), expectedMonth.longValue());
} else {
assertEquals(parsed, null);
}
}
@Test
public void test_parse_decoratedStartsWithPad_number() {
builder.padNext(3, '-').appendValue(MONTH_OF_YEAR, 1, 2, SignStyle.NORMAL);
TemporalAccessor parsed = builder.toFormatter().parseUnresolved("--2", pos);
assertEquals(pos.getIndex(), 3);
assertEquals(pos.getErrorIndex(), -1);
assertEquals(parsed.isSupported(MONTH_OF_YEAR), true);
assertEquals(parsed.getLong(MONTH_OF_YEAR), 2L); // +2, not -2
}
@Test(dataProvider="Pad")
public void test_pad_NORMAL(int minPad, int maxPad, long value, String result) throws Exception {
try {
getFormatter(DAY_OF_MONTH, minPad, maxPad, SignStyle.NORMAL).formatTo(new MockFieldValue(DAY_OF_MONTH, value), buf);
if (result == null) {
fail("Expected exception");
}
assertEquals(buf.toString(), (value < 0 ? "-" + result : result));
} catch (DateTimeException ex) {
if (result != null) {
throw ex;
}
assertEquals(ex.getMessage().contains(DAY_OF_MONTH.toString()), true);
}
}
@Test(dataProvider="error")
public void test_parse_error(TemporalField field, int min, int max, SignStyle style, String text, int pos, Class<?> expected) {
try {
getFormatter(field, min, max, style).parseUnresolved(text, new ParsePosition(pos));
fail();
} catch (RuntimeException ex) {
assertTrue(expected.isInstance(ex));
}
}
@Test
public void test_appendValue_subsequent2_parse4() 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("0123", new ParsePosition(0));
assertEquals(parsed.getLong(MONTH_OF_YEAR), 1L);
assertEquals(parsed.getLong(DAY_OF_MONTH), 23L);
}
@Test(dataProvider="parseDigitsLenient")
public void test_parseDigitsLenient(String input, int min, int max, SignStyle style, int parseLen, Integer parseVal) throws Exception {
setStrict(false);
ParsePosition pos = new ParsePosition(0);
TemporalAccessor parsed = getFormatter(DAY_OF_MONTH, min, max, style).parseUnresolved(input, pos);
if (pos.getErrorIndex() != -1) {
assertEquals(pos.getErrorIndex(), parseLen);
} else {
assertEquals(pos.getIndex(), parseLen);
assertEquals(parsed.getLong(DAY_OF_MONTH), (long)parseVal);
assertEquals(parsed.query(TemporalQueries.chronology()), null);
assertEquals(parsed.query(TemporalQueries.zoneId()), null);
}
}
@Test
public void test_withLocale_same() throws Exception {
DateTimeFormatter base =
new DateTimeFormatterBuilder().appendLiteral("ONE")
.appendValue(DAY_OF_MONTH, 1, 2, SignStyle.NOT_NEGATIVE)
.toFormatter(Locale.ENGLISH)
.withDecimalStyle(DecimalStyle.STANDARD);
DateTimeFormatter test = base.withLocale(Locale.ENGLISH);
assertSame(test, base);
}
@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);
}
@Test
public void test_appendValue_subsequent2_parse4() 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("0123", new ParsePosition(0));
assertEquals(parsed.getLong(MONTH_OF_YEAR), 1L);
assertEquals(parsed.getLong(DAY_OF_MONTH), 23L);
}
@DataProvider(name="signStyle")
Object[][] data_signStyle() {
return new Object[][] {
{LocalDate.of(0, 10, 2), SignStyle.ALWAYS, null, "+00"},
{LocalDate.of(2001, 10, 2), SignStyle.ALWAYS, null, "+2001"},
{LocalDate.of(-2001, 10, 2), SignStyle.ALWAYS, null, "-2001"},
{LocalDate.of(2001, 10, 2), SignStyle.NORMAL, null, "2001"},
{LocalDate.of(-2001, 10, 2), SignStyle.NORMAL, null, "-2001"},
{LocalDate.of(2001, 10, 2), SignStyle.NEVER, null, "2001"},
{LocalDate.of(-2001, 10, 2), SignStyle.NEVER, null, "2001"},
{LocalDate.of(2001, 10, 2), SignStyle.NOT_NEGATIVE, null, "2001"},
{LocalDate.of(-2001, 10, 2), SignStyle.NOT_NEGATIVE, DateTimeException.class, ""},
{LocalDate.of(0, 10, 2), SignStyle.EXCEEDS_PAD, null, "00"},
{LocalDate.of(1, 10, 2), SignStyle.EXCEEDS_PAD, null, "01"},
{LocalDate.of(-1, 10, 2), SignStyle.EXCEEDS_PAD, null, "-01"},
{LocalDate.of(20001, 10, 2), SignStyle.ALWAYS, DateTimeException.class, ""},
{LocalDate.of(20001, 10, 2), SignStyle.NORMAL, DateTimeException.class, ""},
{LocalDate.of(20001, 10, 2), SignStyle.NEVER, DateTimeException.class, ""},
{LocalDate.of(20001, 10, 2), SignStyle.EXCEEDS_PAD, DateTimeException.class, ""},
{LocalDate.of(20001, 10, 2), SignStyle.NOT_NEGATIVE, DateTimeException.class, ""},
};
}
@Test(dataProvider="error")
public void test_parse_error(TemporalField field, int min, int max, SignStyle style, String text, int pos, Class<?> expected) {
try {
getFormatter(field, min, max, style).parseUnresolved(text, new ParsePosition(pos));
fail();
} catch (RuntimeException ex) {
assertTrue(expected.isInstance(ex));
}
}
@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);
}
@Test(dataProvider="Pad")
public void test_pad_ALWAYS(int minPad, int maxPad, long value, String result) throws Exception {
try {
getFormatter(DAY_OF_MONTH, minPad, maxPad, SignStyle.ALWAYS).formatTo(new MockFieldValue(DAY_OF_MONTH, value), buf);
if (result == null) {
fail("Expected exception");
}
assertEquals(buf.toString(), (value < 0 ? "-" + result : "+" + result));
} catch (DateTimeException ex) {
if (result != null) {
throw ex;
}
assertEquals(ex.getMessage().contains(DAY_OF_MONTH.toString()), true);
}
}
@DataProvider(name="error")
Object[][] data_error() {
return new Object[][] {
{DAY_OF_MONTH, 1, 2, SignStyle.NEVER, "12", -1, IndexOutOfBoundsException.class},
{DAY_OF_MONTH, 1, 2, SignStyle.NEVER, "12", 3, IndexOutOfBoundsException.class},
};
}
@Override
public void enterPs(StrfTimeParser.PsContext ctx) {
// %s The number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).
// Based upon https://stackoverflow.com/questions/36066155/datetimeformatter-for-epoch-milliseconds#answer-36069732
builder
.appendValue(ChronoField.INSTANT_SECONDS, 1, 19, SignStyle.NEVER);
}
@Test(dataProvider="Pad")
public void test_pad_ALWAYS(int minPad, int maxPad, long value, String result) throws Exception {
try {
getFormatter(DAY_OF_MONTH, minPad, maxPad, SignStyle.ALWAYS).formatTo(new MockFieldValue(DAY_OF_MONTH, value), buf);
if (result == null) {
fail("Expected exception");
}
assertEquals(buf.toString(), (value < 0 ? "-" + result : "+" + result));
} catch (DateTimeException ex) {
if (result != null) {
throw ex;
}
assertEquals(ex.getMessage().contains(DAY_OF_MONTH.toString()), true);
}
}
@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);
}
@Test(dataProvider="parseSignsStrict")
public void test_parseSignsStrict(String input, int min, int max, SignStyle style, int parseLen, Integer parseVal) throws Exception {
ParsePosition pos = new ParsePosition(0);
TemporalAccessor parsed = getFormatter(DAY_OF_MONTH, min, max, style).parseUnresolved(input, pos);
if (pos.getErrorIndex() != -1) {
assertEquals(pos.getErrorIndex(), parseLen);
} else {
assertEquals(pos.getIndex(), parseLen);
assertEquals(parsed.getLong(DAY_OF_MONTH), (long)parseVal);
assertEquals(parsed.query(TemporalQueries.chronology()), null);
assertEquals(parsed.query(TemporalQueries.zoneId()), null);
}
}
@DataProvider(name="signStyle")
Object[][] data_signStyle() {
return new Object[][] {
{LocalDate.of(0, 10, 2), SignStyle.ALWAYS, null, "+00"},
{LocalDate.of(2001, 10, 2), SignStyle.ALWAYS, null, "+2001"},
{LocalDate.of(-2001, 10, 2), SignStyle.ALWAYS, null, "-2001"},
{LocalDate.of(2001, 10, 2), SignStyle.NORMAL, null, "2001"},
{LocalDate.of(-2001, 10, 2), SignStyle.NORMAL, null, "-2001"},
{LocalDate.of(2001, 10, 2), SignStyle.NEVER, null, "2001"},
{LocalDate.of(-2001, 10, 2), SignStyle.NEVER, null, "2001"},
{LocalDate.of(2001, 10, 2), SignStyle.NOT_NEGATIVE, null, "2001"},
{LocalDate.of(-2001, 10, 2), SignStyle.NOT_NEGATIVE, DateTimeException.class, ""},
{LocalDate.of(0, 10, 2), SignStyle.EXCEEDS_PAD, null, "00"},
{LocalDate.of(1, 10, 2), SignStyle.EXCEEDS_PAD, null, "01"},
{LocalDate.of(-1, 10, 2), SignStyle.EXCEEDS_PAD, null, "-01"},
{LocalDate.of(20001, 10, 2), SignStyle.ALWAYS, DateTimeException.class, ""},
{LocalDate.of(20001, 10, 2), SignStyle.NORMAL, DateTimeException.class, ""},
{LocalDate.of(20001, 10, 2), SignStyle.NEVER, DateTimeException.class, ""},
{LocalDate.of(20001, 10, 2), SignStyle.EXCEEDS_PAD, DateTimeException.class, ""},
{LocalDate.of(20001, 10, 2), SignStyle.NOT_NEGATIVE, DateTimeException.class, ""},
};
}
@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);
}
@Test(dataProvider="parseSignsLenient")
public void test_parseSignsLenient(String input, int min, int max, SignStyle style, int parseLen, Integer parseVal) throws Exception {
setStrict(false);
ParsePosition pos = new ParsePosition(0);
TemporalAccessor parsed = getFormatter(DAY_OF_MONTH, min, max, style).parseUnresolved(input, pos);
if (pos.getErrorIndex() != -1) {
assertEquals(pos.getErrorIndex(), parseLen);
} else {
assertEquals(pos.getIndex(), parseLen);
assertEquals(parsed.getLong(DAY_OF_MONTH), (long)parseVal);
assertEquals(parsed.query(TemporalQueries.chronology()), null);
assertEquals(parsed.query(TemporalQueries.zoneId()), null);
}
}
@Test
public void test_appendValue_subsequent2_parse4() 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("0123", new ParsePosition(0));
assertEquals(parsed.getLong(MONTH_OF_YEAR), 1L);
assertEquals(parsed.getLong(DAY_OF_MONTH), 23L);
}