How to remove milliseconds, seconds, minutes and hours from a date

I had a problem when I wanted to compare two dates. However, I wanted to compare the year, month, and day. And this is what I can handle:

private Date trim(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.MILLISECOND, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.HOUR, 0); return calendar.getTime(); } 

I use this function to trim all blocks, but days, months and years.

Now the question is, what do you think of this? Do you know any other way to do this?

thanks

+7
java date
source share
5 answers

Do you really need to use java.util.Date ? If you can switch to joda time , you will find some very nice features, such as:

 dateTime.dayOfMonth().roundFloorCopy() 

which does exactly what you need.

+8
source share

Your code works and is easy to read. I do not see any problems or reasons for changing it.

+6
source share

Now the question is, what are you doing?

It looks simple and reasonable. You create a new Date and a temporary Calendar object, but this is only minor overhead. (... unless you use this to sort / organize a large data structure ...)

IMO, there is probably no need to modify it.

Do you know any other way to do this?

  • Design / use a Comparator<Date> that uses only the fields you are interested in, using Calendar to retrieve them.

    Whether this will help depends on how you are comparing. The comparator may be more accurate. But the flip side is that you can end up performing conversions multiple times, and Comparator<Date> does not give you the ability to cache converted dates.

  • Perform calculations using the values ​​returned by Date.getTime() . More or less like this:

     long val = date.getTime(); // milliseconds since 'epoch' in UTC val = val + /* local timezone offset in milliseconds */ long day = val / (1000 * 60 * 60 * 24); // days since "local" epoch. // repeat for other Date, and compare the 'day' numbers as integers 

    Explanation, it should be more efficient to do some simple arithmetic than using Calendar (or something else). The code is a bit more obscure, but it should be obvious to someone with reasonable math skills.

+4
source share

I assume that you want to compare two dates on which you want to compare only year , month and day .

What you did is fine, that is, removing hour , minute , second and millisecond . You will need to do this from both dates.

After trimming is complete, you will need to use the compareTo method in Date .

You can use the following code -

 if(date1.compareTo(date2) < 0) { // date1 is earlier } else if(date1.compareTo(date2) > 0) { // date 2 is earlier } else { // both dates are equal } 
+2
source share

You can try this because the date formats remain fixed:

  String first = date1.toString().substring(4,10) + " " + date.toString().substring(24, 28); String second = date2.toString().substring(4,10) + " " + date.toString().substring(24, 28); 

which will output

December 31, 1969

January 06 1969

And then compare the results.

 return (first.equals(second)); 

I don’t know if this is better than your idea, but you get the same result.

0
source share

All Articles