jQuery in Action, 3rd edition
The moose likes JSF and the fly likes Using java.util.date in faces-config.xml Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Using java.util.date in faces-config.xml" Watch "Using java.util.date in faces-config.xml" New topic

Using java.util.date in faces-config.xml

Tom Reilly

Joined: Jun 01, 2010
Posts: 618
I have a value object with a property of java.util.Date and am trying to create an instance of the object as a managed bean in my faces-config.xml. I get an error trying to create the object if the setter expects a java.util.Date. It works fine if the setter expects a String (and I do a conversion in the setter). It also will not work if I have two setters; one that accepts a Date and one that accepts a String.

Is there a way to do this (perhaps with some conversion technique)?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17417

Welcome to the JavaRanch, Tom!

java.util.date's "Set day/month/year" methods are deprecated, but one alternative is to use the setTime mutator, which would five you something like this:

However, the time in milliseconds since January 1 1970 isn't very user-friendly. Referencing a Calendar would seem a reasonable alternative, but unfortunately Calendar doesn't work too well in JavaBean mode.

Here's 3 options:

1. Ignore the fact that setMonth/Dayt/Year are deprecated and use those properties anyway. You'll run the risk that a future Java implementation will fail because someone finally got rid of them, though. I really don't recommend using deprecated methods, no matter how much the boss screams "Git 'R Dun!".

2. Wrap the java.util.Date with a class of your own making that DOES support either a date-string or month/day/year properties (or both). You can use a Calendar to make the translation and then inject the calendar time into the superclass value. This could, however, lead to possible cases where you actually need a Date and not something which merely has a Date superclass, so tread carefully.

3. Last, but not least, take solution 2 and create another Managed Bean to set the data, then take that bean's time property and inject it into the time property of a true java.util.Date bean.

An IDE is no substitute for an Intelligent Developer.
I agree. Here's the link: http://aspose.com/file-tools
subject: Using java.util.date in faces-config.xml
It's not a secret anymore!