title: How to Validate Date Against multiple patterns format in Java?
body: <p>My problem is not "how to do it", my problem is the corner cases involved.  Here is my code and test case.</p>
  
  <pre><code>package datetest.com;
  
  import java.text.ParseException;
  import java.text.SimpleDateFormat;
  
  public class MultipleDateParserFormat {
      private final static String[] PossibleDateFormat=new String[]{
          "MM/dd/yyyy",
          "MM.dd.yyyy",
          "MM-dd-yyyy",
          "yyyy/MM/dd",
          "yyyy.MM.dd",
          "yyyy-MM-dd",
          "dd/MM/yyyy",
          "dd.MM.yyyy",
          "dd-MM-yyyy"
          };
  
      public MultipleDateParserFormat(){};
      public void checkParseDate(String passedDate){
          int PossibleDateFormatLength=PossibleDateFormat.length;
  
          for(int i=0;i&lt;PossibleDateFormatLength;i++){
              try {
                  SimpleDateFormat simpleDateFormant=new SimpleDateFormat(PossibleDateFormat[i]);
                  simpleDateFormant.setLenient(false);
                  java.util.Date date= simpleDateFormant.parse(passedDate);
                  System.out.println(date);
  
              } catch (ParseException e) {
  
                  System.out.println("Parse Exception Occured for your input Value"+passedDate + "for format" + PossibleDateFormat[i]);
              }
          }
  
  
      }
  
      public static void main(String...strings){
  
          String passedDate="4-03-1992";// This test case is validating against two Pattern 
  
          MultipleDateParserFormat multipleDateParserFormat= new MultipleDateParserFormat();
          multipleDateParserFormat.checkParseDate(passedDate);
      }
  
  }
  </code></pre>
  
  <p>Here is the OUTPUT that I am getting when I run this code:</p>
  
  <pre><code>Parse Exception Occured for your input Value4-03-1992for formatMM/dd/yyyy
  Parse Exception Occured for your input Value4-03-1992for formatMM.dd.yyyy
  Fri Apr 03 00:00:00 IST 1992
  Parse Exception Occured for your input Value4-03-1992for formatyyyy/MM/dd
  Parse Exception Occured for your input Value4-03-1992for formatyyyy.MM.dd
  Parse Exception Occured for your input Value4-03-1992for formatyyyy-MM-dd
  Parse Exception Occured for your input Value4-03-1992for formatdd/MM/yyyy
  Parse Exception Occured for your input Value4-03-1992for formatdd.MM.yyyy
  Wed Mar 04 00:00:00 IST 1992
  </code></pre>
  
  <p>Since my test case is satisfying two given patterns, I'm getting two outputs.</p>
  
  <p>How can I avoid such cases here? What are other ways to do such multiple pattern validation?</p>