Here you can find the source of compareDates(Calendar date1, Calendar date2)
Parameter | Description |
---|---|
date1 | a parameter |
date2 | a parameter |
public static int compareDates(Calendar date1, Calendar date2)
//package com.java2s; //License from project: Open Source License import java.util.Calendar; public class Main { /**/* w ww . j a va2 s . c om*/ * Method that compares 2 dates, works similarly to the compareTo method from Calendar, but * ignores the time, just uses the year, month and day * @param date1 * @param date2 * @return 0 if the dates represent the same day, -1 if date1 is before date2, 1 if date1 is * after date2 */ public static int compareDates(Calendar date1, Calendar date2) { int result = 0; if (date1.get(Calendar.YEAR) == date2.get(Calendar.YEAR)) { if (date1.get(Calendar.MONTH) == date2.get(Calendar.MONTH)) { if (date1.get(Calendar.DAY_OF_MONTH) == date2 .get(Calendar.DAY_OF_MONTH)) { result = 0; } else if (date1.get(Calendar.DAY_OF_MONTH) < date2 .get(Calendar.DAY_OF_MONTH)) { result = -1; } else if (date1.get(Calendar.DAY_OF_MONTH) > date2 .get(Calendar.DAY_OF_MONTH)) { result = 1; } } else if (date1.get(Calendar.MONTH) < date2 .get(Calendar.MONTH)) { result = -1; } else if (date1.get(Calendar.MONTH) > date2 .get(Calendar.MONTH)) { result = 1; } } else if (date1.get(Calendar.YEAR) < date2.get(Calendar.YEAR)) { result = -1; } else if (date1.get(Calendar.YEAR) > date2.get(Calendar.YEAR)) { result = 1; } return result; } }