Date Validation In Servlets/JSPs

Discussions

Web tier: servlets, JSP, Web frameworks: Date Validation In Servlets/JSPs

  1. Date Validation In Servlets/JSPs (3 messages)

    Hello everybody,

    We are moving all our validations from client side (java script) to server side (servlets/jsp). I need to know how to do the following

    - How to validate that a String contains a valid date while we have a date format "YYYY/MM/DD"
    - If we have a valid date in String, how to convert it into java.util.Date from String
    - If we have a java.util.Date, how to convert it into different date formats like DD MONTH YYYY

    Thanks,
    fa.
  2. Date Validation In Servlets/JSPs[ Go to top ]

    There are a bunch of helper classes that you should take a look at in the java.text package:

    DateFormat
    SimpleDateFormat

    These classes should be able to help you convert dates and strings pretty easily.

    Dan
  3. Here are a pair of overloaded methods for validating a Date string, the return true(valid) false(invalid). The first one allows the caller to set the date pattern (i.e. 'mm/dd/yy'), the second one I use as sort of the "default" format. An exception is thrown if the date passed in can't be parsed properly.

    They are a little heavy, but I'm kinda of paranoid about dates, so they are very exacting. You could probably loosen them up quite a bit. They'd be faster.
    Two side notes.

    1) Make sure that you use setLenient=false.
    This is very important. If you don't then the date 05/99/99
    will be valid (I'm trying to remember here but I think Java just adds 99 days to the first day of may or something like that).

    2) Then length test is important as you'll see. Since without it the date '05/05/1999a' would return as valid, since the date parse only gets the part that is matched with the parse pattern.

    Here ya go.

    public static boolean checkDate(String inDate, String dateFormat) {
        int dateFormatLength = dateFormat.length();
        try {
          if (inDate.length() != dateFormatLength)
            throw new Exception();
          else {
            SimpleDateFormat format = new SimpleDateFormat(dateFormat);
            format.setLenient(false);
            Date theDate = new Date();
            theDate = format.parse(inDate);
            return true;
          }
        }
        catch(Exception e) {
          return false;
        }
      }
    public static boolean checkDate(String inDate) {
        String dateFormat = "MM/dd/yyyy";
        int dateFormatLength = dateFormat.length();
        try {
          if (inDate.length() != dateFormatLength)
            throw new Exception();
          else {
            SimpleDateFormat format = new SimpleDateFormat(dateFormat);
            format.setLenient(false);
            Date theDate = new Date();
            theDate = format.parse(inDate);
            return true;
          }
        }
        catch(Exception e) {
          return false;
        }
      }
  4. Might be worth having a look at http://developer.java.sun.com/developer/qow/archive/120/index.html