Skip to content

Comparing dates in java. Search today, tomorrow

I accept the attribute from the JSP page and want to compare. Whether the date passed to the servlet is today, tomorrow, or another day. How can I compare this ???

Advertisement

Answer

java.time

Don’t use java.sql.Date. First, it is poorly designed, an awful hack, indeed, and long outdated. Second, it was never meant for anything else than transferring dates to and from SQL databases. Third, while it pretends to be just a date without time of day, it isn’t, but internally stores milliseconds precision, which causes its equals method not to work as expected. Instead I am using LocalDate from java.time, the modern Java date and time API. A LocalDate is what you need: a date without time of day and without time zone.

Link: Oracle tutorial: Date Time explaining how to use java.time.

User contributions licensed under: CC BY-SA
5 People found this is helpful