Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Date and time comparison  RSS feed

 
Jayaram Ponnusamy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am beginner in JSP.
As per requirement a content section will enable/disable based on the date set in the CMS for each contents.
Basically its a Date field and cannot set time.

I have added below condition to Enable / Disable. My problem is Content Section will disable when hrs start 00:00. but I want to disable by EOD (23:59)
Please help me on this.


Thanks
Jay
 
Marcus Biel
Ranch Hand
Posts: 51
IntelliJ IDE Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I assume you are trying to exclude the exact hour + minutes of the day and just do a comparision by date.

E.g.:
StartDate: 1. June 2015
EndDate: 11th June 2015

Now: 11th June 2015, 4:05pm-> enable selection

1. First advice:
Try to limit jsp coding as close as possible to writing out content only. Never use inline Java code, this really will make your JSPs hard to read and a NIGHTMARE to maintain later one.
Instead, do put all this logic into the server side, e.g. into a Servlet.

2. Second advice: Don't use java.util.Date, it makes your comparision harder than it would need to be.
Include and use the joda time dependency in your project, that will allow you to do these comparisons very easily in a readable way:
http://www.joda.org/joda-time/

In case you are using Java JDK 8, the library is included by default.


 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66205
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marcus Biel wrote:1. First advice:
Try to limit jsp coding as close as possible to writing out content only. Never use inline Java code, this really will make your JSPs hard to read and a NIGHTMARE to maintain later one.
Instead, do put all this logic into the server side, e.g. into a Servlet.

He didn't; so I'm not sure why you posted this. it's good advice, but not applicable in this case.

This is simply a border condition case: Jayaram, closely evaluate your use of the operators <= and >=. If you do not want an end-point included, you'd omit the = part.
 
Marcus Biel
Ranch Hand
Posts: 51
IntelliJ IDE Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


That is inline Java code.
 
Marcus Biel
Ranch Hand
Posts: 51
IntelliJ IDE Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
This is simply a border condition case: Jayaram, closely evaluate your use of the operators <= and >=. If you do not want an end-point included, you'd omit the = part.



To me this:

My problem is Content Section will disable when hrs start 00:00. but I want to disable by EOD (23:59)


Clearly states he does not want time to be included in the comparison.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66205
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah yes, embedded as that is, I missed it. Good eye.

In any case, yes it's good advice. Java code (scriptlets) in JSPs have been obsolete for 14 years! And using it in combination with JSTL and EL is even worse!

And yes, Dates are a pain the keester to deal with. If some computations need to be done on the dates (strip off times, use Joda time, and so on) I'd hide the complexity behind a custom tag or EL function rather than doing handstands in the JSP.
 
Marcus Biel
Ranch Hand
Posts: 51
IntelliJ IDE Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Java code (scriptlets) in JSPs have been obsolete for 14 years! And using it in combination with JSTL and EL is even worse!
And yes, Dates are a pain the keester to deal with.

I totally agree on that one.


Bear Bibeault wrote:
I'd hide the complexity behind a custom tag or EL


Disagree on that one. I would say I kind of earned the black belt in custom- and el-tag usage about 7 years ago
(I wrote custom tags for pretty much everything, improved a very dirty scripted jsp application at this time...),
but since then, much has changed (and I have learned a bunch more since...)
Using tags is an improvement to scriplets, yes, agreed.
But they will still polute your view. The view is supposed to display stuff. Only (the exception proofs the rule).
Doing all these calculations on the server (well jsp is also run on the server, but I just mean using native java code in a Controller / Servlet / Entity...)
Unleashes the full power of Java, using delegation, interfaces... you name it - and clearly separates your view from your (business) logic.

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66205
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't disagree with you. Putting stuff that isn't display-oriented into the JSP is a bad idea, even if it does end up getting hidden behind custom tags or EL functions. But there are times when hiding display-oriented complexity behind tags or functions is appropriate.

In this case, however, I think I favor your approach of handling it in the controller. It's also a good habit to instill in novices who, by default, will tend to put a bunch of stuff into JSP views that doesn't belong there.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!