title: Parsing both dd-MM-yyyy and yyyy-MM-dd to Date type in Java?
body: <pre><code>import java.text.DateFormat;
  import java.text.ParseException;
  import java.text.SimpleDateFormat;
  import java.util.Locale;
  
  public class demo {
      public static void main(String args[]) throws ParseException {
          
        DateFormat date=new SimpleDateFormat(&quot;yyyy-MM-dd&quot;,Locale.ENGLISH);
        System.out.println(date.parse(&quot;18-09-2017&quot;)); //Doesn't give an error but output is incorrect
        System.out.println(date.parse(&quot;2017-09-18&quot;));  //Output is correct.
      }
  }
  </code></pre>
  <p>How can I parse both the Strings containing date in different fashion.
  NOTE: I need the output of parse in java.util.Date type.</p>