0

I want to make date format validation. It should be yyyy-mm-dd and to achieve my requirement I followed below code but it's not working.

My input request should be either 2020-13-08 or 2020-13-08T23:00:00Z but it should be 2020-08-13 or 2020-08-13T23:00:00Z

public static boolean validateJavaDate(String strDate) {

        /*
         * Set preferred date format, For example MM-dd-yyyy, MM.dd.yyyy,dd.MM.yyyy etc.
         */
        SimpleDateFormat sdfrmt = new SimpleDateFormat("yyyy-mm-dd");
        sdfrmt.setLenient(false);
        try {
            Date javaDate = sdfrmt.parse(strDate);
            System.out.println(strDate + " is valid date format");
        }
        catch (ParseException e) {
            System.out.println(strDate + " is Invalid Date format");
            return false;
        }
        return true;
    }
halfer
  • 19,824
  • 17
  • 99
  • 186
AbhiRam
  • 2,033
  • 7
  • 41
  • 94
  • 2
    I didn't understand by `My input request should be either 2020-13-08 or 2020-13-08T23:00:00Z but it should be 2020-13-08 or 2020-13-08T23:00:00Z`. Could you please clarify? Also, what is not working in current code? – Smile Aug 13 '20 at 13:50
  • 1
    The pattern for month should be `M` instead of `m`. I guess you want to validate that the month 13 is wrong. – M A Aug 13 '20 at 13:53
  • My input should either will come like 2020-13-08 or 2020-13-08T23:00:00Z but those are should be valid formate like 2020-08-13 or 2020-08-13T23:00:00Z – AbhiRam Aug 13 '20 at 13:54
  • @AbhiRam Could you edit the question to clarify that to readers? – M A Aug 13 '20 at 13:58
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDate` and `Instant`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Aug 13 '20 at 15:23

3 Answers3

3

It's better to use the new java.time package instead

public static boolean validateJavaDate(String strDate) {
    if (strDate == null || strDate.isEmpty()) { //for completeness 
        System.out.println("Given value can not be null or empty");
        return false;
    }

    DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE;
    try {
        formatter.parse(strDate);
        System.out.println(strDate + " is valid date format");
        return true;
    }
    catch (DateTimeParseException e) {
        System.out.println(strDate + " is Invalid Date format");
        return false;
    }
}

If the goal is to validate both date strings with and without time we can extract the actual parsing to a private method and call it with different formatters

private static boolean formatDate(String string, DateTimeFormatter formatter) {
    try {
        formatter.parse(string);
        return true;
    } catch (DateTimeParseException e) {
        return false;
    }

}
public static boolean validateJavaDate(String strDate) {
    if (strDate == null || strDate.isEmpty()) { //for completeness 
        System.out.println("Given value can not be null or empty");
        return false;
    }

    boolean isValid = formatDate(strDate, DateTimeFormatter.ISO_LOCAL_DATE);
    if (!isValid) {
        isValid = formatDate(strDate, DateTimeFormatter.ISO_DATE_TIME);
    }

    if (isValid) {
        System.out.println(strDate + " is valid date format");
    } else {
        System.out.println(strDate + " is Invalid Date format");
    }

    return isValid;
}
Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
1

yyyy-mm-dd matches minutes in hour (the mm) part, that's why the validation passes. Replace it with yyyy-MM-dd to match the month instead. See https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/text/SimpleDateFormat.html for the documentation of SimpleDateFormat.

M A
  • 71,713
  • 13
  • 134
  • 174
0

For other hand you can do it manual.

Somethink like that

public boolean isDate(String format)
{   
  int year = format.subString(0,3); //You must validate that the subString is Integer
  int month = format.subString(5,6); //Same
  int day = format.subString(8,9); //Same


  if(month <= 12 && month >= 1)
  {
     //is valid
     if(day <= 31 && day >= 1)
     {
        if(String.valueOf(format.chatAt(4)).equals("-") && String.valueOf(format.chatAt(7)).equals("-")
        return true;
     }
  }
  .
 }
Andreww
  • 3
  • 4