DateUtil { public static LocalDate parseLocalDate(String timestamp) { if (timestamp != null) { for (DateTimeFormatter formatter : DateUtil.formatters) { try { return formatter.parseLocalDate(timestamp); } catch (IllegalArgumentException e) { } } } return null; } private  DateUtil(); static String formatYearMonthDayNoDash(Date date); static String formatYearMonthDayNoDash(LocalDate date); static String formatYearMonthDay(Date date); static String formatYearMonthDay(LocalDate date); static String formatYearMonth(Date date); static String formatYearMonth(LocalDate date); static String formatYearMonthNoDash(Date date); static String formatYearMonthNoDash(LocalDate date); static LocalDate parseLocalDate(String timestamp); static String lastMonthInYearMonthFormat(); static LocalDate firstDayPreviousMonth(); static LocalDate lastDayPreviousMonth(); static LocalDate firstDayMonth(LocalDate localDate); static LocalDate lastDayMonth(LocalDate localDate);  }
@Test public void testParseYearMonthDay() { String toParse = "2010-02-22"; LocalDate localDate = DateUtil.parseLocalDate(toParse); assertNotNull("Error parsing yyyy-MM-dd", localDate); assertEquals(2010, localDate.getYear()); assertEquals(2, localDate.getMonthOfYear()); assertEquals(22, localDate.getDayOfMonth()); assertNull("Error parsing yyyy-MM-dd", DateUtil.parseLocalDate("invalid date")); }
@Test public void testParseYearMonth() { String toParse = "2000-12"; LocalDate localDate = DateUtil.parseLocalDate(toParse); assertNotNull("Error parsing yyyy-MM", localDate); assertEquals(2000, localDate.getYear()); assertEquals(12, localDate.getMonthOfYear()); assertEquals(1, localDate.getDayOfMonth()); toParse = "2010-20"; assertNull("Error parsing yyyy-MM", DateUtil.parseLocalDate(toParse)); }
@Test public void testParseYearMonthDayNoDash() { String toParse = "20010101"; LocalDate localDate = DateUtil.parseLocalDate(toParse); assertNotNull("Error parsing yyyyMMdd", localDate); assertEquals(2001, localDate.getYear()); assertEquals(1, localDate.getMonthOfYear()); assertEquals(1, localDate.getDayOfMonth()); toParse = "20001032"; assertNull("Error parsing yyyyMMdd", DateUtil.parseLocalDate(toParse)); }