This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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)?
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.