title: Java 8 DateTimeFormatterBuilder().appendOptional not working
body: <p>My requirement is to validate that a date String is in the correct format based on a set of valid formats specified.</p>
  
  <p>Valid formats:</p>
  
  <pre><code>MM/dd/yy
  MM/dd/yyyy
  </code></pre>
  
  <p>I created a simple test method that uses the Java 8 DateTimeFormatterBuilder to create a flexible formatter that supports multiple optional formats. Here is the code:</p>
  
  <pre><code>public static void test() {
      DateTimeFormatter formatter = new DateTimeFormatterBuilder()
              .appendOptional(DateTimeFormatter.ofPattern("MM/dd/yy"))
              .appendOptional(DateTimeFormatter.ofPattern("MM/dd/yyyy"))
              .toFormatter();
  
      String dateString = "10/30/2017";
  
      try {
          LocalDate.parse(dateString, formatter);
          System.out.println(dateString + " has a valid date format");
      } catch (Exception e) {
          System.out.println(dateString + " has an invalid date format");
      }
  }
  </code></pre>
  
  <p>When I run this,  here is the output</p>
  
  <pre><code>10/30/2017 has an invalid date format
  </code></pre>
  
  <p>As you see in the code, the valid date formats are MM/dd/yy and MM/dd/yyyy. 
  My expectation was that the date 10/30/2017 should be valid as it matches MM/dd/yyyy. However, 10/30/2017 is being reported as invalid.</p>
  
  <p>What is going wrong ? Why is this not working ?</p>
  
  <p>I also tried </p>
  
  <pre><code>.appendOptional(DateTimeFormatter.ofPattern("MM/dd/yy[yy]"))
  </code></pre>
  
  <p>in place of</p>
  
  <pre><code>.appendOptional(DateTimeFormatter.ofPattern("MM/dd/yy"))
  .appendOptional(DateTimeFormatter.ofPattern("MM/dd/yyyy"))
  </code></pre>
  
  <p>but still had the same issue.</p>
  
  <p>This code runs as expected if I use:</p>
  
  <pre><code>String dateString = "10/30/17";
  </code></pre>
  
  <p>in place of </p>
  
  <pre><code>String dateString = "10/30/2017";
  </code></pre>
  
  <p>I have 2 questions</p>
  
  <ol>
  <li><p>What is going wrong here ? Why is it not working for "10/30/2017" ?</p></li>
  <li><p>Using Java 8, how to correctly create a flexible Date formatter (a formatter that supports multiple optional formats) ? I know the use of [] to create optional sections in the pattern string itself. I'm looking for something more similar to what I am trying (avoiding [] inside the pattern string and using separate optional clauses for each separate format string)</p></li>
  </ol>