@Nullable
  public static LocalDateTime parseDateTime(String s) {
    for (Map.Entry<DateTimeFormatter, Integer> entry : FORMATTERS.entrySet()) {
      DateTimeFormatter fmt = entry.getKey();
      int len = entry.getValue();
      try {
        if (s.length() < len) {
          continue;
        }
        String datePart = s.substring(0, len);
        LocalDateTime datetime = LocalDateTime.parse(datePart, fmt);
        return datetime;
      } catch (DateTimeParseException e) {
        // It just means the string is not in this format
        continue;
      }
    }
    // Unknown format here
    LOG.warn("Unknown date format in {}", s.length() > 50 ? s.substring(0, 50) : s);
    return null;
  }
@Test
  public void commandLineDatetime() throws Exception {
    Map<String, LocalDateTime> stringToDatetime = new HashMap<>();
    stringToDatetime.put("2020-06-27 11:58:53,084",
            LocalDateTime.of(2020, 6, 27, 11, 58, 53, 84 * MILLISEC_TO_NANOSEC));
    stringToDatetime.put("2020-06-27 11:58:53",
            LocalDateTime.of(2020, 6, 27, 11, 58, 53));
    stringToDatetime.put("2020-06-27 11:58",
            LocalDateTime.of(2020, 6, 27, 11, 58));
    stringToDatetime.put("20/06/27 11:58:53",
            LocalDateTime.of(2020, 6, 27, 11, 58, 53));
    stringToDatetime.put("20/06/27 11:58",
            LocalDateTime.of(2020, 6, 27, 11, 58));
    stringToDatetime.put("2020-06-27T11:58:53.084+0800",
            LocalDateTime.of(2020, 6, 27, 11, 58, 53, 84 * MILLISEC_TO_NANOSEC));
    stringToDatetime.put("2020-06-27T11:58:53",
            LocalDateTime.of(2020, 6, 27, 11, 58, 53));
    stringToDatetime.put("2020-06-27T11:58",
            LocalDateTime.of(2020, 6, 27, 11, 58));

    for (Map.Entry<String, LocalDateTime> entry : stringToDatetime.entrySet()) {
      String key = entry.getKey();
      LocalDateTime expectedDatetime = entry.getValue();
      LocalDateTime parsedDatetime = CollectLogCommand.parseDateTime(key);
      assertThat(String.format("Expected datetime is %s but parsed %s from string %s%n",
              expectedDatetime, parsedDatetime, key),
              parsedDatetime,
              new DatetimeMatcher().setDatetime(expectedDatetime));
    }
  }