Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem formatting dates.

 
Dave Brown
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I'm having a problem formatting dates in a JSP page.

I'm using displaytag but even without displaytag I get problems,

E.g..

${order.dateStarted} will output 2005-04-10
However..

<fmt:formatDate pattern="MMM-dd-yyyy" value="${order.dateStarted}"/>

displays nothing.

the order.dateStarted is of java.util.Date type

Anyone any ideas why a returned date object is not being formatted ?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65535
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dave Brown:

However..

<fmt:formatDate pattern="MMM-dd-yyyy" value="${order.dateStarted}"/>

displays nothing.


Are you sure about that? In other words, what's in the generated HTML? Is it truly nothing? Or the unrenderred <fmt> tag?

Don't go by what you see displayed; look at the actual HTML.
 
Dave Brown
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear,

It really is nothing. I have the format taglib defined.

The displaytag library i'm using also failed to format the date.

The Object is definately a Date type, but it appears to be containing an 'invalid' date somehow.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure that order.dateStarted is java.util.Date ?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65535
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, this is suspicious:

${order.dateStarted} will output 2005-04-10


A java.util.Date will emit more information when toString'd.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65535
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For example, a java.util.Date will emit:

Wed Sep 20 10:01:30 CDT 2006

so what is the data type of the property?
 
Dave Brown
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is a java.util.Date

in a class with getters and setters..
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So perhaps a java.sql.Date object has been put into the object via that setter; this is possible because that's a subclass of java.util.Date, and I think that's how it toStrings itself. But that shouldn't matter.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65535
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good catch Paul.

I wrote up the following test JSP:



It results in output:

2006-09-20

September 20, 2006
 
Dave Brown
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wonder if because its being cast somewhere as java.sql.Date is the reason displaytag is refusing to show it..

I rebuilt the project and it does show now.. So the <fmt> tag issue must have been something daft I was doing..

But the displaytag still refuses to format it, I'm using hibernate in this app and wonder if it is that which is casting it as a java.sql.Date somewhere before using the setter..
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65535
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dave Brown:
I wonder if because its being cast somewhere as java.sql.Date is the reason displaytag is refusing to show it..


No, that will have no effect. The data is what the data is, no amount of casting can change that.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dave Brown:
I'm using hibernate in this app and wonder if it is that which is casting it as a java.sql.Date somewhere before using the setter..


Nope. Even the ResultSet's getDate() method returns java.sql.Date.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!