Ejemplo n.º 1
0
 /**
  * Validates a datetime value. All values matching the "date" core validation rule, and the "time"
  * one will be valid
  *
  * @param String dateTimeString
  * @param List<String> format. The possible formats to pass to the function are : dmy 05-12-2011
  *     or 05-12-11 separators can be a space, period, dash, forward slash mdy 12-05-2011 or
  *     12-05-11 separators can be a space, period, dash, forward slash ymd 2011-12-05 or 11-12-05
  *     separators can be a space, period, dash, forward slash dMy 05 December 2011 or 05 Dec 2011
  *     Mdy December 05, 2011 or Dec 05, 2011 comma is optional My December 2011 or Dec 2011 my
  *     12/2011 separators can be a space, period, dash, forward slash
  * @param String regex. This argument is optional. If a custom regular expression is used this is
  *     the only validation that will occur
  * @return boolean true when the string is a valid datetime or false when it is not
  */
 public static boolean checkDateTime(String dateString, List<String> format, String regex) {
   // variable holding the end result
   boolean valid = false;
   // Get the various parts of the dateString.
   String[] parts = dateString.split(" ");
   if (parts != null && parts.length > 1) {
     // Get the time part
     String time = parts[parts.length - 1];
     // Reconstruct the date parts
     String date = parts[0];
     for (int i = 1; i < parts.length - 2; i++) {
       date += " " + parts[i];
     }
     // pass to the validation function
     valid = Validation.checkDate(date, format, regex) && Validation.checkTime(time);
   }
   return valid;
 }