Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

unable to type case

 
nikhil katoch
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
greetings!!
i am using a converter in which getAsString is defined as

public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
SimpleDateFormat format=new SimpleDateFormat("dd/MMM/yyyy");
Calendar cal=(Calendar)arg2; //////////////line of error
Date t=cal.getTime();
String s=format.format(t);

return (s);
}

this is applied to a field that display a calender data. so arg2 is a calendar data but on running the code error say
java.lang.ClassCastException: java.lang.String cannot be cast to java.util.GregorianCalendar

but arg2 is already a calender data (i have also tried to print arg2 by using getAsString and it gives calendar data ). so why is it giving this type cast error???


 
Tim Holloway
Saloon Keeper
Posts: 18303
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://www.coderanch.com/how-to/java/UseCodeTags and your code samples will be easier to read.

I think you may discover that more than one type of data is being passed as arg2 and that you'll need to determine which type arg2 has as part of the conversion function so that the appropriate conversion code (including type-casting) can be selected and executed.

It's also possible that you've configured your converter to convert one or more non-Calendar properties.

Incidentally, I think that the standard JSF date converter handles both Date and Calendar objects, so you might not actually need a custom converter.
 
nikhil katoch
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well i tried standard converter
<f:convertDateTime dateStyle="full"/> also <f:convertDateTime pattern="mm/dd/yyyy"/>
but it gives error
Servlet.service() for servlet Faces Servlet threw exception
javax.servlet.jsp.JspException: Not nested in a tag of proper type. Error for tag with handler class:com.sun.faces.taglib.jsf_core.ConvertDateTimeTag.

 
Tim Holloway
Saloon Keeper
Posts: 18303
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you show us the surrounding tag as well as the converter tag?
 
nikhil katoch
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
</h:column>
<h:column id="column5">
<f:facet name="header">
<h:outputText value="Issued on"></h:outputText>
</f:facet>
<h:outputText value="#{row.from1}" ></h:outputText>
<f:convertDateTime pattern="mm/dd/yyyy"/>

</h:column>

from1 is a calendar data type (without adding converter it is showing calendar data fine)

(jl: disabled smilies)
 
Tim Holloway
Saloon Keeper
Posts: 18303
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I see your problem, but if you use the "Code" button to wrap code tags around your XML, it won't get mangled.

Here is the proper use of the converter:
 
nikhil katoch
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did it and got

java.lang.IllegalArgumentException: Cannot format given Object as a Date
(is it because object is calendar???)

(thanks for trying Tim I really appreciate your efforts!!)
 
Junilu Lacar
Bartender
Pie
Posts: 7771
62
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
then try row.from1.date
 
nikhil katoch
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i did it and got
org.apache.jasper.el.JspPropertyNotFoundException: /issue1.jsp(50,4) '#{row.from1.date}' Property 'date' not found on type java.util.GregorianCalendar
 
Junilu Lacar
Bartender
Pie
Posts: 7771
62
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry you couldn't find the right method to call on the java.util.Calendar, Nikhil. I was trying to get you to ShowSomeEffort by finding the JavaDocs that actually had the answer. (hint hint)
 
nikhil katoch
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will try JavaDocs for sure.(I also tried to use only date object in whole program (to eradicate calendar issue) but still I was getting error)
when I used

the codes were not working as expected (you know that for sure!!! )but when I used

I got the desired result (but still unable to understand why only by shifting </hutputText> to next line made the program work???)
 
Tim Holloway
Saloon Keeper
Posts: 18303
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nikhil katoch wrote:
I got the desired result (but still unable to understand why only by shifting </hutputText> to next line made the program work???)


I doubt that it did. Though it's possible that updating the View definition one last time pushed out some stale code or data.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic