Java (JDK 1.5+) Date and Time validation and conversion


I need to know the best way to validate a date\time string if format matches the below patterns and if ok - convert to Calendar


The below does not work I think a org.apache.commons.validator.routines does not do date and time together - any help on this or another wa y - example would be great - a BIG THANKS

//*****************************************************************************
  private Calendar paseDateTime(String datetime) {
   Calendar result = null; ;
   CalendarValidator validator = new CalendarValidator();

   result = validator.validate(datetime, "dd.MM.yy hh:mm");
   if (result != null)
    return result;
    result = validator.validate(datetime, "dd.MM.yyyy hh:mm");
   if (result != null)
    return result;
    result = validator.validate(datetime, "dd.MM.yy hh:mm:ss");
    if (result != null)
     return result;
    result = validator.validate(datetime, "dd.MM.yyyy hh:mm:ss");
    if (result != null)
     return result;
    result = validator.validate(datetime, "dd//MM//yy hh:mm");
    if (result != null)
     return result;
    result = validator.validate(datetime, "dd//MM//yyyy hh:mm");
    if (result != null)
     return result;
    result = validator.validate(datetime, "dd//MM//yy hh:mm:ss");
    if (result != null)
     return result;
    result = validator.validate(datetime, "dd//MM//yyyy hh:mm:ss");

   return result;
  }
stephenowenAsked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
>         sdf.setLenient(true);

should be false

>     result = checkDateTime(datetime, "dd//MM//yy HH:mm");

should be // not /
0
 
objectsCommented:
try:

   CalendarValidator validator = new CalendarValidator(true, DateFormat.LONG);


or using SDF:

try {
   SimpleDateFormat sdf = new SimpleDateFormat(patter);
   sdf.setLenient(false);
   Date d = sdf.parse(datestring);
   cal.setTime(d);
   return cal;
} catch (Exception ex) {
   // not valid
}
0
 
stephenowenAuthor Commented:
will try first thing in morning - am GMT
thanks for fast response - I will respond back under 8 hours
0
 
stephenowenAuthor Commented:
No luck works for some dates but it does not detect valid dates.

Have set         sdf.setLenient(true) and also false same results


Test data
23.04.2006 17:24
22/03/07  09:34:00 {FAILS}
01/12/07  21:00:00 {FAILS}
12.23.2007 18:23   (SHOULD FAIL - DOES NOT - SEE MONTH - in wrong position)
01/12/07  21:00:00 {FAILS}


=========
private Calendar checkDateTime(String datestring,String pattern){
   
 Calendar result = Calendar.getInstance();
  SimpleDateFormat sdf = null;
   try {
        sdf = new SimpleDateFormat(pattern);
        sdf.setLenient(true);
        java.util.Date d = sdf.parse(datestring);
        String temp = sdf.toPattern();
        result.setTime(d);
     } catch (Exception ex) {
        // not valid
        result = null;
     }
     finally {
      sdf = null;
     }
  return result;  
}    
//*****************************************************************************
  private Calendar paseDateTime(CalendarValidator validator, String datetime) {
   Calendar result = null; ;

   result = checkDateTime(datetime, "dd.MM.yy HH:mm");
   if (result != null)
    return result;
    result = checkDateTime(datetime, "dd.MM.yyyy HH:mm");
   if (result != null)
    return result;
    result = checkDateTime(datetime, "dd.MM.yy HH:mm:ss");
    if (result != null)
     return result;
    result = checkDateTime(datetime, "dd.MM.yyyy HH:mm:ss");
    if (result != null)
     return result;
    result = checkDateTime(datetime, "dd//MM//yy HH:mm");
    if (result != null)
     return result;
    result = checkDateTime(datetime, "dd//MM//yyyy HH:mm");
    if (result != null)
     return result;
    result = checkDateTime(datetime, "dd//MM//yy HH:mm:ss");
    if (result != null)
     return result;
    result = checkDateTime(datetime, "dd//MM//yyyy HH:mm:ss");

   return result;
  }

 //*****************************************************************************
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.