Here you can find the source of compareDateTime(final Calendar firstCal, final Calendar secondCal)
public static int compareDateTime(final Calendar firstCal, final Calendar secondCal)
//package com.java2s; //License from project: Open Source License import java.util.Calendar; public class Main { public static int compareDateTime(final Calendar firstCal, final Calendar secondCal) { int result = 0; result = compareDates(firstCal, secondCal); if (result == 0) { result = compareTime(firstCal, secondCal); }/*from ww w . j a v a 2s . c o m*/ if (result == 1) System.out.println("first date is greater than the second"); else if (result == -1) System.out.println("second date is greater than the first"); else if (result == 0) System.out.println("both the first & second dates are equal"); return result; } public static int compareDates(final Calendar firstCal, final Calendar secondCal) { int result = 0; if (firstCal.get(Calendar.YEAR) > secondCal.get(Calendar.YEAR)) result = 1; else if (firstCal.get(Calendar.YEAR) < secondCal.get(Calendar.YEAR)) result = -1; else { if (firstCal.get(Calendar.MONTH) > secondCal.get(Calendar.MONTH)) result = 1; else if (firstCal.get(Calendar.MONTH) < secondCal.get(Calendar.MONTH)) result = -1; else { if (firstCal.get(Calendar.DATE) > secondCal.get(Calendar.DATE)) result = 1; else if (firstCal.get(Calendar.DATE) < secondCal.get(Calendar.DATE)) result = -1; } } return result; } public static int compareTime(final Calendar firstCal, final Calendar secondCal) { int result = 0; { { System.out.println("Calendar.AM_PM = 0, so comparing HOUR_OF_DAY"); System.out.println("firstCal HOUR_OF_DAY " + firstCal.get(Calendar.HOUR_OF_DAY)); System.out.println("secondCal HOUR_OF_DAY " + secondCal.get(Calendar.HOUR_OF_DAY)); if (firstCal.get(Calendar.HOUR_OF_DAY) > secondCal.get(Calendar.HOUR_OF_DAY)) result = 1; else if (firstCal.get(Calendar.HOUR_OF_DAY) < secondCal.get(Calendar.HOUR_OF_DAY)) result = -1; } if (result == 0) { System.out.println("Calendar HOURS are equal so , comparing MINUTES"); System.out.println("firstCal MINUTE " + firstCal.get(Calendar.MINUTE)); System.out.println("secondCal MINUTE" + secondCal.get(Calendar.MINUTE)); if (firstCal.get(Calendar.MINUTE) > secondCal.get(Calendar.MINUTE)) result = 1; else if (firstCal.get(Calendar.MINUTE) < secondCal.get(Calendar.MINUTE)) result = -1; if (result == 0) { System.out.println("Calendar MINUTES are equal so , comparing SECOND"); System.out.println("firstCal SECOND " + firstCal.get(Calendar.SECOND)); System.out.println("secondCal SECOND" + secondCal.get(Calendar.SECOND)); if (firstCal.get(Calendar.SECOND) > secondCal.get(Calendar.SECOND)) result = 1; else if (firstCal.get(Calendar.SECOND) < secondCal.get(Calendar.SECOND)) result = -1; if (result == 0) { System.out.println("Calendar SECONDS are equal so , comparing MILLI SECOND"); if (firstCal.get(Calendar.MILLISECOND) > secondCal.get(Calendar.MILLISECOND)) result = 1; else if (firstCal.get(Calendar.MILLISECOND) < secondCal.get(Calendar.MILLISECOND)) result = -1; } } } } if (result == 1) System.out.println("first date is greater than the second"); else if (result == -1) System.out.println("second date is greater than the first"); else if (result == 0) System.out.println("both the first & second dates are equal"); return result; } }