Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Date and Timestamp comparison

 
Craig Dixon
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:




?
 
Craig Dixon
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I tried the obvious, I got the following:

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about the "confused" comment. It was meant as a slight at my code rather than your response.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll be happier for it.

Meantime, the EL function route might be your easiest out.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic