Last active
February 15, 2020 16:44
-
-
Save Martyr2/008c53c2e2e25a1efbaf571e5f59f029 to your computer and use it in GitHub Desktop.
DateValidator class in Java which will validate dates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
public class DateValidator { | |
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); | |
} catch (ParseException e) { | |
return false; | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment