• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

date conversion problem

 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello:

I am writing a custom converter for my dates in my app. The built-in converter populates text fields with a short date but I want MM/dd/yyyy since this is the format users enter in dates. The date fields are also optional in my app. That presents a problem.

Here is my customer conversion code:

public class ProjectDateConverter extends StrutsTypeConverter {


public Object convertFromString(Map map, String[] strings, Class class1) {
String enteredDateString = strings[0];
if (!enteredDateString.trim().equals("")) {
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
try {
return sdf.parse(enteredDateString);

}
catch (ParseException e) {
throw new TypeConversionException("A date was not entered as MM/DD/YYYY");
}
}
System.out.println("Converter: " + enteredDateString);
return null;
}

public String convertToString(Map map, Object object) {
Date date = (Date) object;
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
return sdf.format(date);
}
}


As you can see in the convertFromString method, some type of Object must be returned. If dates are optional, it would be ideal to return a null here but that, of course, doesn't work. How do I allow a user to optionally enter a date without the converter throwing an error? I have not been able to locate the built-in converter code yet.

Any help would be appreciated!

Eric
 
A lot of people cry when they cut onions. The trick is not to form an emotional bond. This tiny ad told me:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic