How to get date part (dispose of time part) from java.util.Date?

MozenRath picture MozenRath · Sep 30, 2011 · Viewed 11.7k times · Source

I want to compare the date part of two java.util.Date objects. How can I achieve this? I am not looking to comparing the date, month and year separately.

Thanks in advance!

Answer

Turbokiwi picture Turbokiwi · Sep 30, 2011

The commons-lang DateUtils provide a nice solution for this problem:

watch this

With this you can compare two Date instances in a single line, loosing sight of every part of the Date you want.

Date date1 = new Date(2011, 8, 30, 13, 42, 15);
    Date date2 = new Date(2011, 8, 30, 15, 23, 46);
    int compareTo = DateUtils.truncatedCompareTo(date1, date2,
            Calendar.DAY_OF_MONTH);

In this example the value of compareTo is 0.