Java Calendar Compare compareTime(final Calendar firstCal, final Calendar secondCal)

Here you can find the source of compareTime(final Calendar firstCal, final Calendar secondCal)

Description

compare Time

License

Open Source License

Declaration

public static int compareTime(final Calendar firstCal, final Calendar secondCal) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Calendar;

public class Main {
    public static int compareTime(final Calendar firstCal, final Calendar secondCal) {
        int result = 0;
        {/* www .  j av  a  2 s  .co m*/
            {
                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;
    }
}

Related

  1. compareDates(Calendar date1, Calendar date2)
  2. compareDates(final Calendar firstCal, final Calendar secondCal)
  3. compareDateTime(final Calendar firstCal, final Calendar secondCal)
  4. compareDay(Calendar calendar, Calendar calendar1)
  5. compareSameDay(Calendar first, Calendar second)
  6. isEqual(Calendar calendar, Date date)
  7. isEquals(Calendar sourceDate, Calendar compareDate)
  8. isSameDate(Calendar d1, Calendar d2)
  9. isSameDate(java.util.Calendar date1, java.util.Calendar date2)