下面列出了java.time.DateTimeException#getMessage ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void test_throws_message_zone() {
ZoneId zone = ZoneId.of("Pacific/Honolulu");
DateTimeFormatter fmt = new DateTimeFormatterBuilder().appendChronologyId().toFormatter()
.withZone(zone);
LocalTime now = LocalTime.now();
try {
fmt.format(now);
fail("Format using appendChronologyId() should have failed");
} catch (DateTimeException dte) {
String msg = dte.getMessage();
// Verify message contains the type that is missing and the temporal value
assertTrue(msg.contains("Chronology"),
String.format("\"%s\" missing from %s", "Chronology", msg));
assertTrue(msg.contains(zone.toString()),
String.format("\"%s\" missing from %s", zone.toString(), msg));
}
}
private void assertGoodErrorDate(Function<TemporalAccessor, Object> function, String expectedText) {
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-mm-dd");
TemporalAccessor temporal = f.parse("2010-06-30");
try {
function.apply(temporal);
fail("Should have failed");
} catch (DateTimeException ex) {
String msg = ex.getMessage();
assertTrue(msg.contains(expectedText), msg);
assertTrue(msg.contains("Year"), msg);
assertTrue(msg.contains("MinuteOfHour"), msg);
assertTrue(msg.contains("DayOfMonth"), msg);
}
}
private void assertGoodErrorTime(Function<TemporalAccessor, Object> function, String expectedText) {
DateTimeFormatter f = DateTimeFormatter.ofPattern("HH:MM:ss");
TemporalAccessor temporal = f.parse("11:30:56");
try {
function.apply(temporal);
fail("Should have failed");
} catch (DateTimeException ex) {
String msg = ex.getMessage();
assertTrue(msg.contains(expectedText), msg);
assertTrue(msg.contains("HourOfDay"), msg);
assertTrue(msg.contains("MonthOfYear"), msg);
assertTrue(msg.contains("SecondOfMinute"), msg);
}
}
private void assertGoodErrorDate(Function<TemporalAccessor, Object> function, String expectedText) {
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-mm-dd");
TemporalAccessor temporal = f.parse("2010-06-30");
try {
function.apply(temporal);
fail("Should have failed");
} catch (DateTimeException ex) {
String msg = ex.getMessage();
assertTrue(msg.contains(expectedText), msg);
assertTrue(msg.contains("Year"), msg);
assertTrue(msg.contains("MinuteOfHour"), msg);
assertTrue(msg.contains("DayOfMonth"), msg);
}
}
private void assertGoodErrorTime(Function<TemporalAccessor, Object> function, String expectedText) {
DateTimeFormatter f = DateTimeFormatter.ofPattern("HH:MM:ss");
TemporalAccessor temporal = f.parse("11:30:56");
try {
function.apply(temporal);
fail("Should have failed");
} catch (DateTimeException ex) {
String msg = ex.getMessage();
assertTrue(msg.contains(expectedText), msg);
assertTrue(msg.contains("HourOfDay"), msg);
assertTrue(msg.contains("MonthOfYear"), msg);
assertTrue(msg.contains("SecondOfMinute"), msg);
}
}
private void assertGoodErrorTime(Function<TemporalAccessor, Object> function, String expectedText) {
DateTimeFormatter f = DateTimeFormatter.ofPattern("HH:MM:ss");
TemporalAccessor temporal = f.parse("11:30:56");
try {
function.apply(temporal);
fail("Should have failed");
} catch (DateTimeException ex) {
String msg = ex.getMessage();
assertTrue(msg.contains(expectedText), msg);
assertTrue(msg.contains("HourOfDay"), msg);
assertTrue(msg.contains("MonthOfYear"), msg);
assertTrue(msg.contains("SecondOfMinute"), msg);
}
}
private void assertGoodErrorTime(Function<TemporalAccessor, Object> function, String expectedText) {
DateTimeFormatter f = DateTimeFormatter.ofPattern("HH:MM:ss");
TemporalAccessor temporal = f.parse("11:30:56");
try {
function.apply(temporal);
fail("Should have failed");
} catch (DateTimeException ex) {
String msg = ex.getMessage();
assertTrue(msg.contains(expectedText), msg);
assertTrue(msg.contains("HourOfDay"), msg);
assertTrue(msg.contains("MonthOfYear"), msg);
assertTrue(msg.contains("SecondOfMinute"), msg);
}
}
private void assertGoodErrorDate(Function<TemporalAccessor, Object> function, String expectedText) {
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-mm-dd");
TemporalAccessor temporal = f.parse("2010-06-30");
try {
function.apply(temporal);
fail("Should have failed");
} catch (DateTimeException ex) {
String msg = ex.getMessage();
assertTrue(msg.contains(expectedText), msg);
assertTrue(msg.contains("Year"), msg);
assertTrue(msg.contains("MinuteOfHour"), msg);
assertTrue(msg.contains("DayOfMonth"), msg);
}
}
private void assertGoodErrorTime(Function<TemporalAccessor, Object> function, String expectedText) {
DateTimeFormatter f = DateTimeFormatter.ofPattern("HH:MM:ss");
TemporalAccessor temporal = f.parse("11:30:56");
try {
function.apply(temporal);
fail("Should have failed");
} catch (DateTimeException ex) {
String msg = ex.getMessage();
assertTrue(msg.contains(expectedText), msg);
assertTrue(msg.contains("HourOfDay"), msg);
assertTrue(msg.contains("MonthOfYear"), msg);
assertTrue(msg.contains("SecondOfMinute"), msg);
}
}
private void assertGoodErrorDate(Function<TemporalAccessor, Object> function, String expectedText) {
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-mm-dd");
TemporalAccessor temporal = f.parse("2010-06-30");
try {
function.apply(temporal);
fail("Should have failed");
} catch (DateTimeException ex) {
String msg = ex.getMessage();
assertTrue(msg.contains(expectedText), msg);
assertTrue(msg.contains("Year"), msg);
assertTrue(msg.contains("MinuteOfHour"), msg);
assertTrue(msg.contains("DayOfMonth"), msg);
}
}
private void assertGoodErrorTime(Function<TemporalAccessor, Object> function, String expectedText) {
DateTimeFormatter f = DateTimeFormatter.ofPattern("HH:MM:ss");
TemporalAccessor temporal = f.parse("11:30:56");
try {
function.apply(temporal);
fail("Should have failed");
} catch (DateTimeException ex) {
String msg = ex.getMessage();
assertTrue(msg.contains(expectedText), msg);
assertTrue(msg.contains("HourOfDay"), msg);
assertTrue(msg.contains("MonthOfYear"), msg);
assertTrue(msg.contains("SecondOfMinute"), msg);
}
}
private void assertGoodErrorDate(Function<TemporalAccessor, Object> function, String expectedText) {
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-mm-dd");
TemporalAccessor temporal = f.parse("2010-06-30");
try {
function.apply(temporal);
fail("Should have failed");
} catch (DateTimeException ex) {
String msg = ex.getMessage();
assertTrue(msg.contains(expectedText), msg);
assertTrue(msg.contains("Year"), msg);
assertTrue(msg.contains("MinuteOfHour"), msg);
assertTrue(msg.contains("DayOfMonth"), msg);
}
}
private void assertGoodErrorTime(Function<TemporalAccessor, Object> function, String expectedText) {
DateTimeFormatter f = DateTimeFormatter.ofPattern("HH:MM:ss");
TemporalAccessor temporal = f.parse("11:30:56");
try {
function.apply(temporal);
fail("Should have failed");
} catch (DateTimeException ex) {
String msg = ex.getMessage();
assertTrue(msg.contains(expectedText), msg);
assertTrue(msg.contains("HourOfDay"), msg);
assertTrue(msg.contains("MonthOfYear"), msg);
assertTrue(msg.contains("SecondOfMinute"), msg);
}
}
private void assertGoodErrorDate(Function<TemporalAccessor, Object> function, String expectedText) {
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-mm-dd");
TemporalAccessor temporal = f.parse("2010-06-30");
try {
function.apply(temporal);
fail("Should have failed");
} catch (DateTimeException ex) {
String msg = ex.getMessage();
assertTrue(msg.contains(expectedText), msg);
assertTrue(msg.contains("Year"), msg);
assertTrue(msg.contains("MinuteOfHour"), msg);
assertTrue(msg.contains("DayOfMonth"), msg);
}
}
private void assertGoodErrorTime(Function<TemporalAccessor, Object> function, String expectedText) {
DateTimeFormatter f = DateTimeFormatter.ofPattern("HH:MM:ss");
TemporalAccessor temporal = f.parse("11:30:56");
try {
function.apply(temporal);
fail("Should have failed");
} catch (DateTimeException ex) {
String msg = ex.getMessage();
assertTrue(msg.contains(expectedText), msg);
assertTrue(msg.contains("HourOfDay"), msg);
assertTrue(msg.contains("MonthOfYear"), msg);
assertTrue(msg.contains("SecondOfMinute"), msg);
}
}
private void assertGoodErrorTime(Function<TemporalAccessor, Object> function, String expectedText) {
DateTimeFormatter f = DateTimeFormatter.ofPattern("HH:MM:ss");
TemporalAccessor temporal = f.parse("11:30:56");
try {
function.apply(temporal);
fail("Should have failed");
} catch (DateTimeException ex) {
String msg = ex.getMessage();
assertTrue(msg.contains(expectedText), msg);
assertTrue(msg.contains("HourOfDay"), msg);
assertTrue(msg.contains("MonthOfYear"), msg);
assertTrue(msg.contains("SecondOfMinute"), msg);
}
}
private void assertGoodErrorTime(Function<TemporalAccessor, Object> function, String expectedText) {
DateTimeFormatter f = DateTimeFormatter.ofPattern("HH:MM:ss");
TemporalAccessor temporal = f.parse("11:30:56");
try {
function.apply(temporal);
fail("Should have failed");
} catch (DateTimeException ex) {
String msg = ex.getMessage();
assertTrue(msg.contains(expectedText), msg);
assertTrue(msg.contains("HourOfDay"), msg);
assertTrue(msg.contains("MonthOfYear"), msg);
assertTrue(msg.contains("SecondOfMinute"), msg);
}
}
private void assertGoodErrorDate(Function<TemporalAccessor, Object> function, String expectedText) {
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-mm-dd");
TemporalAccessor temporal = f.parse("2010-06-30");
try {
function.apply(temporal);
fail("Should have failed");
} catch (DateTimeException ex) {
String msg = ex.getMessage();
assertTrue(msg.contains(expectedText), msg);
assertTrue(msg.contains("Year"), msg);
assertTrue(msg.contains("MinuteOfHour"), msg);
assertTrue(msg.contains("DayOfMonth"), msg);
}
}
private void assertGoodErrorTime(Function<TemporalAccessor, Object> function, String expectedText) {
DateTimeFormatter f = DateTimeFormatter.ofPattern("HH:MM:ss");
TemporalAccessor temporal = f.parse("11:30:56");
try {
function.apply(temporal);
fail("Should have failed");
} catch (DateTimeException ex) {
String msg = ex.getMessage();
assertTrue(msg.contains(expectedText), msg);
assertTrue(msg.contains("HourOfDay"), msg);
assertTrue(msg.contains("MonthOfYear"), msg);
assertTrue(msg.contains("SecondOfMinute"), msg);
}
}
private void assertGoodErrorDate(Function<TemporalAccessor, Object> function, String expectedText) {
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-mm-dd");
TemporalAccessor temporal = f.parse("2010-06-30");
try {
function.apply(temporal);
fail("Should have failed");
} catch (DateTimeException ex) {
String msg = ex.getMessage();
assertTrue(msg.contains(expectedText), msg);
assertTrue(msg.contains("Year"), msg);
assertTrue(msg.contains("MinuteOfHour"), msg);
assertTrue(msg.contains("DayOfMonth"), msg);
}
}