Here you can find the source of compareDays(final Date date1, final Date date2)
Parameter | Description |
---|---|
date1 | First date |
date2 | Second date |
public static int compareDays(final Date date1, final Date date2)
//package com.java2s; //License from project: Open Source License import java.util.Calendar; import java.util.Date; public class Main { private static final int[] DATE_FIELDS = new int[] { Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_YEAR }; /**// w w w.jav a2 s.c o m * Compare two dates considering "day" as the most significant field. * * @param date1 * First date * @param date2 * Second date * @return If the first date is before the second date, a negative number * will be returned. If the first date is same day of the second * date, zero will be returned. If the first date is after the * second date, a positive number will be returned */ public static int compareDays(final Date date1, final Date date2) { Calendar calendar1 = getCalendarInstance(date1); Calendar calendar2 = getCalendarInstance(date2); for (int field : DATE_FIELDS) { int fieldValue1 = calendar1.get(field); int fieldValue2 = calendar2.get(field); if (fieldValue1 != fieldValue2) { return fieldValue1 - fieldValue2; } } return 0; } /** * Get the calendar instance of a given date * @param date * The date * @return The calendar instance */ public static Calendar getCalendarInstance(final Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); return calendar; } }