How to remove milliseconds, seconds, minutes and hours from Date

date, java

Solution

Do you really need to use `java.util.Date` ? If you can switch to joda time, you'll find very nice features like:

dateTime.dayOfMonth().roundFloorCopy()

which does exactly what you need.

Problem

I ran into a problem where I wanted to compare two dates. However, I only wanted to compare Year, Month and Day. And this is what I can up with: ``` 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 user this function to trim all units but days, months and years. Now, the question is what do you think about it? Do you know any other way to do so? Thanks

Original source

Related problems