import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; public class DateValidator extends BaseValidator implements GracieValidator{ public DateValidator() { } public static final String GLOBAL_DATE_FORMAT = Util.GLOBAL_DATE_FORMAT; public static final String CUSTOMER_DATE_FORMAT = "DD/MM/YYYY"; /** * Validation Rule: Validate the input text whether it is valid date string or not by using global date format * @param inputText Input string text * @return boolean Return true when the input text is valid date string or not by using global date format, * otherwise return false. */ public static boolean validateDateByDefaultFormat(String inputText) { return DateValidator.validateDate(inputText, DateValidator.GLOBAL_DATE_FORMAT); } /** * Validation Rule: Validate the input text whether it is valid date string or not by using global date format * @param inputText Input string text * @param fieldErrorParam Field error tag parameter * @param fieldErrorMessage Field error message * @return boolean Return true when the input text is valid date string or not by using global date format, * otherwise return false. */ public static boolean validateDateByDefaultFormat(String inputText, String fieldErrorParam, String fieldErrorMessage) { boolean isValid = DateValidator.validateDateByDefaultFormat(inputText); if (!isValid) { addFieldErrorMessage(fieldErrorParam, fieldErrorMessage); } return isValid; } /** * Validation Rule: Validate the input text whether it is valid date string or not by using global date format * @param inputText Input string text * @param fieldErrorParam Field error tag parameter * @param bundle Language property file id * @param key Message key * @param defaultMessage Default message * @param arguments The arguments of message * @return boolean Return true when the input text is valid date string or not by using global date format, * otherwise return false. */ public static boolean validateDateByDefaultFormat(String inputText, String fieldErrorParam, String bundle, String key, String defaultMessage, String[] arguments) { boolean isValid = DateValidator.validateDateByDefaultFormat(inputText); if (!isValid) { String fieldErrorMessage = MessageUtil.getText(bundle, key, defaultMessage, arguments); addFieldErrorMessage(fieldErrorParam, fieldErrorMessage); } return isValid; } /** * Validation Rule: Validate the input text whether it is valid date string or not by using customer date format * @param inputText Input string text * @return boolean Return true when the input text is valid date string or not by using customer date format, * otherwise return false. */ public static boolean validateDateByCustomerFormat(String inputText) { return DateValidator.validateDate(inputText, DateValidator.CUSTOMER_DATE_FORMAT); } /** * Validation Rule: Validate the input text whether it is valid date string or not by using customer date format * @param inputText Input string text * @param fieldErrorParam Field error tag parameter * @param fieldErrorMessage Field error message * @return boolean Return true when the input text is valid date string or not by using customer date format, * otherwise return false. */ public static boolean validateDateByCustomerFormat(String inputText, String fieldErrorParam, String fieldErrorMessage) { boolean isValid = DateValidator.validateDateByCustomerFormat(inputText); if (!isValid) { addFieldErrorMessage(fieldErrorParam, fieldErrorMessage); } return isValid; } /** * Validation Rule: Validate the input text whether it is valid date string or not by using customer date format * @param inputText Input string text * @param fieldErrorParam Field error tag parameter * @param bundle Language property file id * @param key Message key * @param defaultMessage Default message * @param arguments The arguments of message * @return boolean Return true when the input text is valid date string or not by using customer date format, * otherwise return false. */ public static boolean validateDateByCustomerFormat(String inputText, String fieldErrorParam, String bundle, String key, String defaultMessage, String[] arguments) { boolean isValid = DateValidator.validateDateByCustomerFormat(inputText); if (!isValid) { String fieldErrorMessage = MessageUtil.getText(bundle, key, defaultMessage, arguments); addFieldErrorMessage(fieldErrorParam, fieldErrorMessage); } return isValid; } /** * Validation Rule: Validate the input text whether it is valid date string or not by using specified date format * @param inputText Input string text * @return boolean Return true when the input text is valid date string or not by using specified date format, * otherwise return false. */ private static boolean validateDate(String inputText, String format) { boolean isValid = false; try { if (!Util.isEmpty(inputText)) { DateFormat formatter = new SimpleDateFormat(format); Date date = formatter.parse(inputText); if (formatter.format(date).equalsIgnoreCase(inputText)) { isValid = true; } else { isValid = false; } } else { isValid = true; } } catch (Exception e) { isValid = false; } return isValid; } public boolean isValid(String inputText) { return validateDateByDefaultFormat(inputText); } public boolean isValid(String inputText, String fieldErrorParam, String bundle, String key, String defaultMessage, String[] arguments) { return DateValidator.validateDateByDefaultFormat(inputText, fieldErrorParam, bundle, key, defaultMessage, arguments); } public boolean isValid(String inputText, String fieldErrorParam, String fieldErrorMessage) { return DateValidator.validateDateByDefaultFormat(inputText, fieldErrorParam, fieldErrorMessage); } public boolean isValid(String inputText, Object[] objs) { String format = (String) objs[0]; return validateDate(inputText,format); } }