Parse String(2004-02-29) to a date


import java.text.ParseException;
import java.text.SimpleDateFormat;

public class Main {

  public static boolean isValidDate(String inDate) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    dateFormat.setLenient(false);
    try {
      dateFormat.parse(inDate.trim());
    } catch (ParseException pe) {
      return false;
    }
    return true;
  }

  public static void main(String[] args) {

    System.out.println(isValidDate("2004-02-29"));
    System.out.println(isValidDate("2005-02-29"));
  }
}

Output:


true
false
Home 
  Java Book 
    Runnable examples  

Date Parse:
  1. Parse string(31/12/06) into date
  2. Parse string(29 jan 2002) into date
  3. Parse string(02.47.44 PM) to date time
  4. Parse time string(15:30:18) to date
  5. Parse string(29-Jan-02) to date
  6. Parse string(Feb 28, 2002) to date value
  7. Convert String(20/12/2005) to Date object
  8. Parse String(2004-02-29) to a date
  9. Parse string(2002.01.29.08.36.33) into a date and time
  10. Parse String(24-Feb-2009 17:39:35) to a Date/GregorianCalendar
  11. Parse string(Tue, 29 Jan 2004 21:14:02 -0500) to date
  12. Parse date string in short form
  13. Parse date string in default Locale Canada format