Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse LocalDateTime with leading spaces?

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?

like image 452
Yurii Bondarenko Avatar asked Feb 11 '23 02:02

Yurii Bondarenko


1 Answers

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.

like image 123
Meno Hochschild Avatar answered Feb 13 '23 22:02

Meno Hochschild