• Post Reply Bookmark Topic Watch Topic
  • New Topic

Handling Dates in JSP + Beans  RSS feed

 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
I have a bean with a date property.
The date is a java.sql.Date property. I'm getting an exception in my JSP file when I get this property from the bean.
myBean.getDate();
I was not getting any problem before because I was getting the date from the database and storing it in the bean as a STRING, but know that i change my property in the bean for Date I get an exception.
Should I stored the date in the bean as a date or should I just get the date from the database and store it in a String date in my bean?
any suggestions.??
thanks
 
Shubhrajit Chatterjee
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
show us ur stack trace
 
John Wetherbie
Rancher
Posts: 1449
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aside from the exception problem you need to decide what you will be doing with this information. If all you are doing is displaying then a String should be fine. Since most of the java.sql.Date interface is deprecated (in 1.2 and up) what is driving you to use a Date?
As far as the exception goes, posting your code and the stack trace would be helpful to troubleshoot the problem.
Good luck!
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe he would like to be able to both compare date values with one another and output them to the response. String representations aren't helpful (necessarily) when you want to find out if a date is after another date. In what context are you trying to use the Date value? Are you trying to assign it to a tag's attribute value (via a scriptlet) that expects a String object (which is why it used to work)? Again, a code example would be useful.
 
Sandep Chaturvedi
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Easy solution which will work
Create custom date class with java.util.Date as instance variable and override toString method so that jsp gets the string value.
You can do date operations in the bean using lets say getDate of this class which returns the actual date and your jsp:getProperty will get the toString version.
You can even apply the date formats so that date is stored as date and display can vary with format.
We have been using this and it always works.
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sandep Chaturvedi:
Easy solution which will work
Create custom date class with java.util.Date as instance variable and override toString method so that jsp gets the string value.
You can do date operations in the bean using lets say getDate of this class which returns the actual date and your jsp:getProperty will get the toString version.
You can even apply the date formats so that date is stored as date and display can vary with format.
We have been using this and it always works.


From a design standpoint, I don't like this solution. You are creating an entirely new class that only formats a java.util.Date object into a String. However, there already exist classes in the java.text package for doing this very thing. If it were me, I would use the Date object directly, but have the JSP format the date using some sort of custom tag (available in the JSTL or Struts). There's no need for a wrapper class here IMHO.
 
Sandep Chaturvedi
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I gave was an example I used. I was actually using this class for other purposes (writting to CSV, PDF) other than just JSP.
You are right plain ol' Date should do the job for just JSP solution.
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by John Wetherbie:
Aside from the exception problem you need to decide what you will be doing with this information. If all you are doing is displaying then a String should be fine. Since most of the java.sql.Date interface is deprecated (in 1.2 and up) what is driving you to use a Date?

Hello John and all of you guys. Thanks for your suggestion. Being in Australia complicates my postings, While I'm sleeping everyone is awake!!

Ok, let's begin. What i want to do with the date is display it in a table. But later one I want to add a feature in which a user should be able to sort the results by date. So I thought that if I get the date from the database, but store it as a string in my Bean I might complicate things later, so I decided to change my bean property for a Date instead of a string.
Now I'm getting this weird exception. By the way, I'm starting to hate Forte for Java , it's so damn slow and sometimes behaves really strange. Here's the exception:

I understand nothing. I don't know, I might leave it the way it was before... what do you guys think?
thanks in advance
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hhmm.. guys... should I get the date from my bean like

and then parse it to String using java.text.SimpleDateFormat, and display it in my table as a string ??
just a suggestion..
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!