@Test public void checkFormattedDateForMongo() { String date = "2016-09-27T15:37:53"; String dateFormatted = "2016-09-27T15:37:53.000"; String dateNoTime = "2016-09-27"; String dateNoTimeFormatted = "2016-09-27T00:00:00.000"; String dateWithHAndM = "2016-09-27T15:37"; String dateWithHAndMFormatted = "2016-09-27T15:37:00.000"; String dateWithMillis = "2016-09-27T15:37:53.54"; String dateWithMillisFormatted = "2016-09-27T15:37:53.540"; String dateWithMillisZone = "2016-09-27T15:37:53.548Z"; String dateWithMillisZoneFormatted = "2016-09-27T15:37:53.548"; String dateWithMillisZonePST = "2016-09-27T15:37:53.548PST"; String dateWithMillisZonePSTFormatted = "2016-09-27T15:37:53.548"; String dateWithMillisZonePSTNoMillis = "2016-09-27T15:37:53PST"; String dateWithMillisZonePSTNoMillisFormatted = "2016-09-27T15:37:53.000"; String slashedDate = "27/09/2016"; String slashedDateFormatted = "2016-09-27T00:00:00.000"; assertEquals(dateFormatted, LocalDateUtil.getFormattedDateForMongo(date)); assertEquals(dateNoTimeFormatted, LocalDateUtil.getFormattedDateForMongo(dateNoTime)); assertEquals(dateWithHAndMFormatted, LocalDateUtil.getFormattedDateForMongo(dateWithHAndM)); assertEquals(dateWithMillisFormatted, LocalDateUtil.getFormattedDateForMongo(dateWithMillis)); assertEquals(dateWithMillisZoneFormatted, LocalDateUtil.getFormattedDateForMongo(dateWithMillisZone)); assertEquals(dateWithMillisZonePSTFormatted, LocalDateUtil.getFormattedDateForMongo(dateWithMillisZonePST)); assertEquals(dateWithMillisZonePSTNoMillisFormatted, LocalDateUtil.getFormattedDateForMongo(dateWithMillisZonePSTNoMillis)); assertEquals(slashedDateFormatted, LocalDateUtil.getFormattedDateForMongo(slashedDate)); }
public static String getFormattedDateForMongo(String date) { LocalDateTime ldt; try { ldt = LocalDateTime.parse(date, DateTimeFormatter.ISO_LOCAL_DATE_TIME); } catch (DateTimeParseException e) { LOGGER.debug("Cannot use ISO_LOCAL_DATE_TIME formatter, try with Zoned one"); try { ldt = LocalDateTime.parse(date, ZONED_DATE_TIME_FORMAT); } catch (DateTimeParseException ex) { LOGGER.debug("Cannot use Zoned LOCAL_DATE_TIME formatter, try with ISO_DATE one and time to " + "00:00:00.000"); try { ldt = LocalDate.parse(date, DateTimeFormatter.ISO_DATE).atTime(0, 0, 0, 0); } catch (DateTimeParseException exc) { LOGGER.debug("Cannot use ISO_DATE formatter, try with SLASH_DATE on and set time to 00:00:00.000"); ldt = LocalDate.parse(date, SLASHED_DATE).atTime(0, 0, 0, 0); } } } return getFormattedDateForMongo(ldt); }
LocalDateUtil { public static String getFormattedDateForMongo(String date) { LocalDateTime ldt; try { ldt = LocalDateTime.parse(date, DateTimeFormatter.ISO_LOCAL_DATE_TIME); } catch (DateTimeParseException e) { LOGGER.debug("Cannot use ISO_LOCAL_DATE_TIME formatter, try with Zoned one"); try { ldt = LocalDateTime.parse(date, ZONED_DATE_TIME_FORMAT); } catch (DateTimeParseException ex) { LOGGER.debug("Cannot use Zoned LOCAL_DATE_TIME formatter, try with ISO_DATE one and time to " + "00:00:00.000"); try { ldt = LocalDate.parse(date, DateTimeFormatter.ISO_DATE).atTime(0, 0, 0, 0); } catch (DateTimeParseException exc) { LOGGER.debug("Cannot use ISO_DATE formatter, try with SLASH_DATE on and set time to 00:00:00.000"); ldt = LocalDate.parse(date, SLASHED_DATE).atTime(0, 0, 0, 0); } } } return getFormattedDateForMongo(ldt); } }
LocalDateUtil { public static String getFormattedDateForMongo(String date) { LocalDateTime ldt; try { ldt = LocalDateTime.parse(date, DateTimeFormatter.ISO_LOCAL_DATE_TIME); } catch (DateTimeParseException e) { LOGGER.debug("Cannot use ISO_LOCAL_DATE_TIME formatter, try with Zoned one"); try { ldt = LocalDateTime.parse(date, ZONED_DATE_TIME_FORMAT); } catch (DateTimeParseException ex) { LOGGER.debug("Cannot use Zoned LOCAL_DATE_TIME formatter, try with ISO_DATE one and time to " + "00:00:00.000"); try { ldt = LocalDate.parse(date, DateTimeFormatter.ISO_DATE).atTime(0, 0, 0, 0); } catch (DateTimeParseException exc) { LOGGER.debug("Cannot use ISO_DATE formatter, try with SLASH_DATE on and set time to 00:00:00.000"); ldt = LocalDate.parse(date, SLASHED_DATE).atTime(0, 0, 0, 0); } } } return getFormattedDateForMongo(ldt); } private  LocalDateUtil(); }
LocalDateUtil { public static String getFormattedDateForMongo(String date) { LocalDateTime ldt; try { ldt = LocalDateTime.parse(date, DateTimeFormatter.ISO_LOCAL_DATE_TIME); } catch (DateTimeParseException e) { LOGGER.debug("Cannot use ISO_LOCAL_DATE_TIME formatter, try with Zoned one"); try { ldt = LocalDateTime.parse(date, ZONED_DATE_TIME_FORMAT); } catch (DateTimeParseException ex) { LOGGER.debug("Cannot use Zoned LOCAL_DATE_TIME formatter, try with ISO_DATE one and time to " + "00:00:00.000"); try { ldt = LocalDate.parse(date, DateTimeFormatter.ISO_DATE).atTime(0, 0, 0, 0); } catch (DateTimeParseException exc) { LOGGER.debug("Cannot use ISO_DATE formatter, try with SLASH_DATE on and set time to 00:00:00.000"); ldt = LocalDate.parse(date, SLASHED_DATE).atTime(0, 0, 0, 0); } } } return getFormattedDateForMongo(ldt); } private  LocalDateUtil(); static final String getString(LocalDateTime localDateTime); static final String getStringFormatted(LocalDateTime localDateTime); static String getString(Date date); static LocalDateTime now(); static Date getDate(String date); static LocalDateTime fromMillis(long millis); static long getMillis(LocalDateTime ldt); static LocalDateTime fromDate(Date date); static LocalDateTime fromDate(FileTime fileTime); static Date getDate(LocalDateTime ldt); static String getFormattedDate(Date date); static String getFormattedDate(LocalDateTime date); static String getFormattedSimpleDate(Date date); static String getFormattedSimpleDate(LocalDate date); static Date getSimpleFormattedDate(final String date); static LocalDate getLocalDateFromSimpleFormattedDate(String date); static String getFormattedDateForMongo(String date); static String transformIsoOffsetDateToIsoOffsetDateTime(String date); static String getFormattedDateForMongo(LocalDateTime date); static LocalDateTime parseMongoFormattedDate(String str); static String getFormattedDateForEsIndexes(LocalDateTime date); static DateTimeFormatter getDateTimeFormatterForFileNames(); }
LocalDateUtil { public static String getFormattedDateForMongo(String date) { LocalDateTime ldt; try { ldt = LocalDateTime.parse(date, DateTimeFormatter.ISO_LOCAL_DATE_TIME); } catch (DateTimeParseException e) { LOGGER.debug("Cannot use ISO_LOCAL_DATE_TIME formatter, try with Zoned one"); try { ldt = LocalDateTime.parse(date, ZONED_DATE_TIME_FORMAT); } catch (DateTimeParseException ex) { LOGGER.debug("Cannot use Zoned LOCAL_DATE_TIME formatter, try with ISO_DATE one and time to " + "00:00:00.000"); try { ldt = LocalDate.parse(date, DateTimeFormatter.ISO_DATE).atTime(0, 0, 0, 0); } catch (DateTimeParseException exc) { LOGGER.debug("Cannot use ISO_DATE formatter, try with SLASH_DATE on and set time to 00:00:00.000"); ldt = LocalDate.parse(date, SLASHED_DATE).atTime(0, 0, 0, 0); } } } return getFormattedDateForMongo(ldt); } private  LocalDateUtil(); static final String getString(LocalDateTime localDateTime); static final String getStringFormatted(LocalDateTime localDateTime); static String getString(Date date); static LocalDateTime now(); static Date getDate(String date); static LocalDateTime fromMillis(long millis); static long getMillis(LocalDateTime ldt); static LocalDateTime fromDate(Date date); static LocalDateTime fromDate(FileTime fileTime); static Date getDate(LocalDateTime ldt); static String getFormattedDate(Date date); static String getFormattedDate(LocalDateTime date); static String getFormattedSimpleDate(Date date); static String getFormattedSimpleDate(LocalDate date); static Date getSimpleFormattedDate(final String date); static LocalDate getLocalDateFromSimpleFormattedDate(String date); static String getFormattedDateForMongo(String date); static String transformIsoOffsetDateToIsoOffsetDateTime(String date); static String getFormattedDateForMongo(LocalDateTime date); static LocalDateTime parseMongoFormattedDate(String str); static String getFormattedDateForEsIndexes(LocalDateTime date); static DateTimeFormatter getDateTimeFormatterForFileNames(); static final String LONG_SECOND_DATE_FORMAT; }