0

I am getting date like DDMMYYYYHHMMSS.

I want to check weather it is valid date format.

How can I do so?

unknownbits
  • 2,855
  • 10
  • 41
  • 81

2 Answers2

0

You can try the below code

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

Change the format according to your need. Check this link for more details

Keen Sage
  • 1,899
  • 5
  • 26
  • 44
0

Pass Any Valid Date Format and check if the date is valid or not

public boolean isThisDateValid(String dateToValidate, String dateFromat){

    if(dateToValidate == null){
        return false;
    }

    SimpleDateFormat sdf = new SimpleDateFormat(dateFromat);
    sdf.setLenient(false);

    try {

        //if not valid, it will throw ParseException
        Date date = sdf.parse(dateToValidate);
        System.out.println(date);

    } catch (ParseException e) {

        e.printStackTrace();
        return false;
    }

    return true;
}
Neeraj Jain
  • 7,643
  • 6
  • 34
  • 62