aspose file tools*
The moose likes JSP and the fly likes Date and Timestamp comparison Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Date and Timestamp comparison" Watch "Date and Timestamp comparison" New topic
Author

Date and Timestamp comparison

Craig Dixon
Greenhorn

Joined: Jul 19, 2005
Posts: 19
Forgive me for asking what is probably a simple question; I've been out of JSP development for a few years now. I'm pulling a date field from a database, which returns a java.sql.Timestamp object. I need to compare it to the current date, which I'm presently getting as follows:



Now I need to compare the two to see if now is later than the date I pulled from the database. However, I can't compare the two types (java.util.Date and java.sql.Timestamp.) I need to do this using JSTL. Someone please make me feel silly by telling me how easy it is to do this.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

Lots has changed in JSP recently. My first question would be, why is this being done in the JSP rather than in the page controller?

And, what happened when you tried the obvious:




?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Craig Dixon
Greenhorn

Joined: Jul 19, 2005
Posts: 19
When I tried the obvious, I got the following:

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

Next dumb question: how did a java.sql.Date make it all the way to the JSP? It should have been converted to a java.util.Date long before. By exposing the java.sql.* instances, you are exposing model layer implementation details that shouldn't be visible to the UI layer.
[ May 14, 2008: Message edited by: Bear Bibeault ]
Craig Dixon
Greenhorn

Joined: Jul 19, 2005
Posts: 19
Guess it would be easiest just to post some code. I'll redact the irrelevant parts.



Does this help, or are you even more confused now?
[ May 14, 2008: Message edited by: Bear Bibeault ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

Oh, you didn't say that you were using the JSTL SQL tags. You do realize that those are not recommended for anything but prototype work, right?

I was originally a bit surprised that the EL comparison failed as Timestamp is derived from Date, but a perusal of the Javadoc reveals:
The Timestamp.equals(Object) method never returns true when passed a value of type java.util.Date because the nanos component of a date is unknown.
which I had forgotten.

Bottom line: you cannot directly compare them.

Solutions? Write a tag or EL function to do either the conversion or comparison in a manner you deem appropriate, or better yet, restructure the app to avoid such pitfalls completely.
[ May 14, 2008: Message edited by: Bear Bibeault ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

Does this help, or are you even more confused now?
You probably want to put this a little less dismissively in future posts.
Craig Dixon
Greenhorn

Joined: Jul 19, 2005
Posts: 19
I did not realize they aren't recommended for anything but prototype work. All the books I have on the subject fail to mention that. What is recommended instead?
Craig Dixon
Greenhorn

Joined: Jul 19, 2005
Posts: 19
Sorry about the "confused" comment. It was meant as a slight at my code rather than your response.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

Here's the blurb from the JSTL Specification itself:

Many web applications need to access relational databases as the source of dynamic data for their presentation layer. While it is generally preferred to have database operations handled within the business logic of a web application designed with an MVC architecture, there are situations where page authors require this capability within their JSP pages (e.g. prototyping/testing, small scale/simple applications, lack of developer resources).


No reputable web developer that I know would use these tags in anything that will actually see the light of day.

Preferred is to follow best-practice tiered web application structure, where the business logic and persistence layers are well-insulated from the UI. You may want to research the term MVC as it applies to web applications. And perhaps this article might be a good start.

At minimum, move any logic out of the JSPs and into Java classes, leaving the JSPs purpose to render the view only.
[ May 14, 2008: Message edited by: Bear Bibeault ]
Craig Dixon
Greenhorn

Joined: Jul 19, 2005
Posts: 19
Thanks for the information. This is an in-house, not commercial, application, but I'll still try to brush up on the best practices.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

You'll be happier for it.

Meantime, the EL function route might be your easiest out.
 
jQuery in Action, 2nd edition
 
subject: Date and Timestamp comparison