We help IT Professionals succeed at work.

Best way to remove time from a Date object in Java

kpbarem
kpbarem asked
on
65 Views
Last Modified: 2016-06-21
I have a method that is comparing a Date passed in to see if it is on or after today (fairly simple).  So I create a Date object and initialize it with default constructor, which gives me the current Date and time.  

if(dateParam.after(today) || dateParam.equals(today)) does not return the correct result if the date passed in is today.  This is because the default date constructor gives me the current date but also the current time.  So therefore if the param is 8/21/2014 and today is 8/21/2014 16:55:00:00 then these two are not equal.

What is the best way to ensure that it just compares the date portion and not the time portion?  Is there a way to zero out the time on a date object? Or possibly a method that doesn't consider the time in comparison?

Thanks in advance for your help!!
Comment
Watch Question

CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
rrzstudent
CERTIFIED EXPERT

Commented:
Is there a way to zero out the time on a date object?
 
The easiest way  to do it would be to convert it to a String. If you use
  String d = "8/21/2014 16:55:00:00";
  String d2 = d.substring(0,d.indexOf(" "));

Open in new window

then you could compare them as Strings.
mccarlIT Business Systems Analyst / Software Developer
CERTIFIED EXPERT
Top Expert 2015

Commented:
Do you have any issues against using third party libraries? If not, then I could suggest using Joda-Time (http://www.joda.org/joda-time/)

If you are going to do a reasonable amount of date/time manipulation in your project then it can be worth the hassle of using a library. The built in java Date/Calendar classes can get rather complicated to use if you are doing a reasonable amount of date operations, and Joda-Time can make these quite simple. For example, you would have your date to compare, in an object of class LocalDate (call it say testDate) and then you can simply do...
LocalDate testDate = ......
LocalDate today = new LocalDate();
boolean answer = !testDate.isBefore(today);

Open in new window

PS: Joda-Time is also very helpful if you need to properly handle timezone issues.
Jeffrey Dake Senior Director of Technology
CERTIFIED EXPERT

Commented:
Using the Calendar class as dpearson said is a good way to go. You can use that class and call the set function on each Calendar.Field value if you did want to clear out the time part.  This would then make the date object returned from getTime equal. I do this sometimes when I am then going to use the date again for other comparisons but only want to consider the Minh and day and not the individual time fields.
CERTIFIED EXPERT
Top Expert 2016

Commented:
if(dateParam.after(today) || dateParam.equals(today)) does not return the correct result if the date passed in is today.  

But it would if you did

if(dateParam.after(today) || !dateParam.before(today)) 

Open in new window

rrzstudent
CERTIFIED EXPERT

Commented:
Right on CEHJ .
I have a method that is comparing a Date passed in to see if it is on or after today (fairly simple).
 Why not just  
 !dateParam.before(today))

Open in new window

CERTIFIED EXPERT

Commented:
Yes this approach:
   !dateParam.before(today))
works as long as dateParam is itself set to midnight (i.e. 0:00 time) on the given day.

You just need to watch out that nobody ever takes that code and does:
  Date today = new Date() ;
  dateParam = today ;  // Oops - today has the current time
  boolean sameDay = !dateParam.before(anotherDay) ;   // May or may not work as expected

Personally I just use Date simply to store timestamps (longs).  For all actual comparisons/manipulations relating to dates, I always convert to Calendar or JodaTime (which is coming into core Java with Java 8).

Doug
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.