aspose file tools*
The moose likes JSF and the fly likes convertDateTime Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "convertDateTime" Watch "convertDateTime" New topic
Author

convertDateTime

gary stines
Greenhorn

Joined: Mar 11, 2002
Posts: 19
<h:inputText value="#{policy.testDate}" id="received">
<f:convertDateTime pattern="dd-MMM-yyyy"/>
</h:inputText>

when using the above code in a jsp page I get the following error

java.lang.IllegalArgumentException: argument type mismatch

This occurs when testDate maps to a java.sql.Date object in the backing bean. It seems to work ok when I change testDate to a java.util.Date object.

Question, does convertDateTime only work with a java.util.Date object. I thought it should work with java.util.Date and all its subclasses which would include java.sql.Date.
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

I believe the problem is that you have 3 'M's in your pattern.

That would throw an IllegalArgumentException from the SimpleDateFormat's applyPattern() method.


A good workman is known by his tools.
Varun Khanna
Ranch Hand

Joined: May 30, 2002
Posts: 1400
Originally posted by gary stines:
<h:inputText value="#{policy.testDate}" id="received">
<f:convertDateTime pattern="dd-MMM-yyyy"/>
</h:inputText>

when using the above code in a jsp page I get the following error

java.lang.IllegalArgumentException: argument type mismatch

This occurs when testDate maps to a java.sql.Date object in the backing bean. It seems to work ok when I change testDate to a java.util.Date object.

Question, does convertDateTime only work with a java.util.Date object. I thought it should work with java.util.Date and all its subclasses which would include java.sql.Date.


It is because, internally getAsObject() in converter performs SimpleDateFormat_instance.parse() operation which return a java.util.Date type of object. Now an java.sql.Date type reference can't hold a java.util.Date type of object (only parent can hold a reference to child object)


- Varun
Varun Khanna
Ranch Hand

Joined: May 30, 2002
Posts: 1400
Originally posted by Marc Peabody:
I believe the problem is that you have 3 'M's in your pattern.

That would throw an IllegalArgumentException from the SimpleDateFormat's applyPattern() method.


Using 3 'M's is fine.

SimpleDateFormat spec mentions

Month: If the number of pattern letters is 3 or more, the month is interpreted as text; otherwise, it is interpreted as a number.

[ February 16, 2005: Message edited by: K Varun ]
Gary VanMatre
Greenhorn

Joined: Sep 26, 2003
Posts: 6
I don't understand why this converter won't support a java.sql.Date. I think it might be rooted in the common bean utils library.
Varun Khanna
Ranch Hand

Joined: May 30, 2002
Posts: 1400
Originally posted by Gary VanMatre:
I don't understand why this converter won't support a java.sql.Date. I think it might be rooted in the common bean utils library.


When the DateTimeConverter gets a string it tries to convert it into Date object as follows :

1. It takes the input in the form of "String"
2. Passes the String to getAsObject method
3. getAsObject method, ultimately calls SimpleDateFormat.parse(String str) method and return the output of this method to you.

Now you want the output to be of java.sql.Date type but if you see the
API here, "parse" method returns java.util.Date type of object.
 
 
subject: convertDateTime