• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 41
  • Last Modified:

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!!
0
kpbarem
Asked:
kpbarem
1 Solution
 
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
 
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 new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now