title: Formatting Date with time, only if there is a time on the date
body: <p>I am trying to sanitize dates that are generated from another application. Sometimes the dates can be of a bad format, when they are, they should be blanked out. The general conditions should be;</p>
  
  <ul>
  <li><p>Any dates on or before the year 1900 are considered invalid and blanked out</p></li>
  <li><p>Any dates after the current year are considered invalid and blanked out</p></li>
  <li><p>Any valid dates that match the patterns should returned in the output <code>yyyy-MM-dd HH:mm:ss</code> (where there is a timestamp available) OR <code>yyyy-MM-dd</code> when there is no timestamp available.</p></li>
  </ul>
  
  <p>Dates &lt;= 1900 and after the current year are considered to be invalid as they are being loaded into another system, where these dates won't make sense in the context of the application. For the same reason, I need to return the dates in the aforementioned format so they can be loaded into the next application.</p>
  
  <p>How can I modify my code below to handle dates that do not have a timestamp? The current code gives an error of: <code>java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: HourOfDay</code> when you try to call; <code>SanitiseDate("01/01/2001")</code></p>
  
  <p>Here is my code so far:</p>
  
  <pre><code>public static String SanitiseDate(String input) {
      DateTimeFormatter[] possibleFormats = {
              DateTimeFormatter.ofPattern("uuuu-MM-dd").withResolverStyle(ResolverStyle.STRICT),
              DateTimeFormatter.ofPattern("uuuu/MM/dd").withResolverStyle(ResolverStyle.STRICT),
              DateTimeFormatter.ofPattern("dd/MM/uuuu").withResolverStyle(ResolverStyle.STRICT),
              DateTimeFormatter.ofPattern("dd-MM-uuuu").withResolverStyle(ResolverStyle.STRICT),
              DateTimeFormatter.ofPattern("mm-DD-uuuu").withResolverStyle(ResolverStyle.STRICT),
  
              DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss").withResolverStyle(ResolverStyle.STRICT),
              DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm").withResolverStyle(ResolverStyle.STRICT),
  
              DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm").withResolverStyle(ResolverStyle.STRICT),
              DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm:ss").withResolverStyle(ResolverStyle.STRICT),
  
              DateTimeFormatter.ofPattern("dd/MM/uuuu HH:mm:ss").withResolverStyle(ResolverStyle.STRICT),
              DateTimeFormatter.ofPattern("dd/MM/uuuu HH:mm").withResolverStyle(ResolverStyle.STRICT),
  
              DateTimeFormatter.ofPattern("dd-MM-uuuu HH:mm").withResolverStyle(ResolverStyle.STRICT),
              DateTimeFormatter.ofPattern("dd-MM-uuuu HH:mm:ss").withResolverStyle(ResolverStyle.STRICT),
  
              DateTimeFormatter.ofPattern("mm-DD-uuuu HH:mm").withResolverStyle(ResolverStyle.STRICT),
              DateTimeFormatter.ofPattern("mm-DD-uuuu HH:mm:ss").withResolverStyle(ResolverStyle.STRICT),
      };
  
      for (DateTimeFormatter format : possibleFormats) {
          try {
              LocalDate result = LocalDate.parse(input, format);
              if (result.getYear() &lt;= 1900 || result.getYear() &gt; Year.now().getValue())
              {
                  return "";
              }
              return result.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
          } catch (DateTimeParseException dtpe) {
              // ignore, try next format
          }
      }
      return "";
  }
  </code></pre>
  
  <p>My tests:</p>
  
  <pre><code>@Test
  public void sanitiseDateTest()
  {
      assertEquals("", StringUtil.SanitiseDate(""));
      assertEquals("", StringUtil.SanitiseDate("01/01/1601"));
      assertEquals("", StringUtil.SanitiseDate("01/01/1501"));
      assertEquals("2001-01-01", StringUtil.SanitiseDate("01/01/2001"));
      assertEquals("1980-01-01", StringUtil.SanitiseDate("01/01/1980"));
      assertEquals("", StringUtil.SanitiseDate("05/05/1900"));
      assertEquals("", StringUtil.SanitiseDate("01/01/1989"));
      assertEquals("1901-01-01", StringUtil.SanitiseDate("01/01/1901"));
      assertEquals("2020-02-29", StringUtil.SanitiseDate("29/02/2020"));
      assertEquals("2020-01-29", StringUtil.SanitiseDate("29/01/2020"));
      assertEquals("2020-01-29 01:00:00", StringUtil.SanitiseDate("29/01/2020 01:00:00"));
      assertEquals("", StringUtil.SanitiseDate("12/12/2030 01:00:00"));
      assertEquals("", StringUtil.SanitiseDate("12/12/2030 01:00:00; 12/12/2030 01:00:00;"));
  }
  </code></pre>