aspose file tools*
The moose likes JSP and the fly likes JSTL:  formatDate Vs Timestamp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "JSTL:  formatDate Vs Timestamp" Watch "JSTL:  formatDate Vs Timestamp" New topic
Author

JSTL: formatDate Vs Timestamp

Celso Oliveira
Greenhorn

Joined: Sep 06, 2006
Posts: 3
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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.


[My Blog]
All roads lead to JavaRanch
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

<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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: Jan 10, 2002
Posts: 61426
    
  67

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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: Jan 10, 2002
Posts: 61426
    
  67

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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: Jul 06, 2005
Posts: 1018
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

Joined: Sep 06, 2006
Posts: 3
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

Joined: Jul 06, 2005
Posts: 1018
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

Joined: Sep 06, 2006
Posts: 3
Thanks Satou

Your sugestion worked FINE!!!

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

Thank you very much!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSTL: formatDate Vs Timestamp