Skip to content

Instantly share code, notes, and snippets.

@Martyr2
Last active February 15, 2020 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Martyr2/008c53c2e2e25a1efbaf571e5f59f029 to your computer and use it in GitHub Desktop.
Save Martyr2/008c53c2e2e25a1efbaf571e5f59f029 to your computer and use it in GitHub Desktop.
DateValidator class in Java which will validate dates
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