Java 8 DateTimeFormatter parsing for optional fractional seconds of varying significance
My MCVE (as a TestNG unit test): public class MyDateTimeFormatterTest { private static final String BASE_PATTERN = "yyyy/MM/dd HH:mm:ss"; private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern(BASE_PATTERN + "[.SSSSSSSSS]"); private static final LocalDateTime TEST_INPUT = LocalDateTime.of(2015, 5, 4, 12, 34, 56, 123456789); @DataProvider(name = "test-cases") public Iterator<Object[]> getTestCases() { return Arrays.asList(testFor("", ChronoUnit.SECONDS), testFor(".SSS", ChronoUnit.MILLIS), testFor(".SSSSSS", ChronoUnit.MICROS), testFor(".SSSSSSSSS", ChronoUnit.NANOS))