?
Solved

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

Posted on 2006-05-18
4
Medium Priority
?
246 Views
Last Modified: 2011-04-14

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;
  }
0
Comment
Question by:stephenowen
  • 2
  • 2
4 Comments
 
LVL 92

Expert Comment

by:objects
ID: 16713819
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
 

Author Comment

by:stephenowen
ID: 16713982
will try first thing in morning - am GMT
thanks for fast response - I will respond back under 8 hours
0
 

Author Comment

by:stephenowen
ID: 16716337
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
 
LVL 92

Accepted Solution

by:
objects earned 1000 total points
ID: 16722409
>         sdf.setLenient(true);

should be false

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

should be // not /
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month17 days, 12 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question