I'm trying to parse dates from html pages using LocalDateTime. And I couldn't come up with solution to make the following test pass.
(Using TestNG) First row always fails
static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("E MMM dd HH:mm:ss u");
@DataProvider(name = "dateTimeFormats")
public Object[][] dateTimeFormats() {
return new Object[][]{
{"Mon Jan 5 06:41:07 2015", LocalDateTime.of(2015, 1, 5, 6, 41, 7)},
{"Sun Jan 25 00:48:23 2015", LocalDateTime.of(2015, 1, 25, 0, 48, 23)},
{"Mon Aug 14 07:58:00 2006", LocalDateTime.of(2006, 8, 14, 7, 58, 0)}
};
}
@Test(dataProvider = "dateTimeFormats")
public void testParsePostDateTime(String dateTimeString, LocalDateTime expectedResult) {
LocalDateTime result = LocalDateTime.parse(dateTimeString, DATE_TIME_FORMATTER);
assertEquals(result, expectedResult);
}
Any ideas which date pattern should be used?
You can use the pattern "E MMM ppd HH:mm:ss u". Explanation of p-symbol see javadoc. It means padding the next field up to the width as there are count of p-letters available.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With