• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSTL: formatDate Vs Timestamp

 
Celso Oliveira
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone.
It�s my first post here.
I�m having some trouble and I don�t know what to do.

I have a JavaBean, User, with the following attribute: private Timestamp birthDate;

I�m trying to use the <c:set> tag to set this attribute when I load a JSP page.

here�s what I�m doing:


And here�s the error I get:


Do I have to build my own custom-tag to deal with java.sql.Timestamp ??
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've never tried, but I'm wondering if you could do the following:
1. Create a PropertyEditor for Timestamp. Use PropertyEditorSupport and override setAsText(String text). Make sure that the format of the text parameter is the same as the one you've got with fmt:parseDate
2. At the server startup (use a listener), register your PropertyEditor using the PropertyEditorManager.registerEditor(Class targetType, Class editorClass)
3. Try again

This sounds fun to do I may try.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64830
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<c:set target="${objUsr}" property="birthDate" value="<fmt:parseDate value='${param.bDate}' type='date' dateStyle='short' pattern='dd/MM/yyyy'/>"/>


You cannot use an action as the attribute value of another action.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here it is. This has been fun. I'll leave you tweak the date format.
Maybe you should avoid using Timestamp after all

The PropertyEditor


The ContextListener


The user bean


Listener registration in web.xml


The JSP test file
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64830
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ummmmmm, well, I guess Rube Goldberg would be proud?

You could just move the <fmt> action to the body of the <c:set>:


[ September 06, 2006: Message edited by: Bear Bibeault ]
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess Rube Goldberg would be proud?



I actually wanted to try the PropertyEditor, which I've never used.
Spring is using the same kind of stuff to register custom property editors to set beans' properties.

(But your one liner broke my heart ;) )
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64830
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Didn't mean to break your heart , but why over-complicate matters when a simple answer will suffice?
[ September 06, 2006: Message edited by: Bear Bibeault ]
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, are you sure this works with Timestamp ?
I've tried with

but got an exception : cannot convert java.util.Date into java.sql.Timestamp.
 
Stefan Evans
Bartender
Posts: 1715
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, JSTL can't convert a java.util.Date into a java.sql.Timestamp automatically.

There are a couple of approaches I can think of:
1 - Add a setter for the property that takes a java.util.Date, and converts it to a timestamp internally.
ie


2 - in the bean use java.util.Date rather than java.sql.Timestamp to declare the property. Convert it to a java.sql.Timestamp (if necessary) in the JDBC code. That separates the Bean from your data access layer.

Hope this helps,
evnafets
 
Celso Oliveira
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi people.
Thanks everyone for the sugestions.

It�s strange...
When I try to show(get) a Timestamp attribute with JSTL it�s OK. No erros.


When I try to set....All my problems start....

I�ll try somethings....

Thank you all again!!!
 
Stefan Evans
Bartender
Posts: 1715
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its because java.sql.Timestamp extends java.util.Date

So you can use a Timestamp object anywhere you use a Date (thus your display code works)
However anywhere that specifes timestamp (such as your save code) will not accept just plain Date objects - only Timestamps.
 
Celso Oliveira
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Satou

Your sugestion worked FINE!!!

I had to make a few adjustments but it was OK!!!

Thank you very much!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic