How to check if date is valid in Java

Question:
How to check if date is valid in java?

Answer:
Use SimpleDateFormat class to check if a provided date is valid.

Code:
import java.util.*;
import java.text.*;
 
public class TestDate {
   public static void main(String[] args) {
 
      SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy");
      sdf.setLenient(false);
 
      // Jan 28, 2015
      String str1 = "01/28/15";
 
      // invalid date
      String str2 = "124/12/12";
 
      try {
         Date date = sdf.parse(str1);
         System.out.println(str1 + " parses to " + date.toString());
      } catch (ParseException e) {
         System.out.println("invalid: " + str1 );
      }
 
     try {
         Date date = sdf.parse(str2);
         System.out.println(str2 + " parses to " + date.toString());
      } catch (ParseException e) {
         System.out.println("invalid: " + str2 );
      }
 
 
   }
}

Output:
$ java TestDate
01/28/15 parses to Wed Jan 28 00:00:00 EST 2015
invalid: 124/12/12