Best way to remove time from a Date object in Java

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!!
kpbaremAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dpearsonCommented:
Try something like this:

      public static boolean sameDay(Date date1, Date date2) {
            Calendar cal1 = Calendar.getInstance() ;
            Calendar cal2 = Calendar.getInstance() ;
            cal1.setTime(date1) ;
            cal2.setTime(date2) ;
            return (cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
                       cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR)) ;
      }

Doug
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rrzCommented:
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.
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
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.
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Jeffrey Dake Senior Director of TechnologyCommented:
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.
0
CEHJCommented:
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

0
rrzCommented:
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

0
dpearsonCommented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.