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

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

Description

compare Date Time

License

Open Source License

Declaration

public static int compareDateTime(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 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;
    }
}

Related

  1. compare(Calendar c1, Calendar c2)
  2. compare(Calendar c1, Calendar c2, int what)
  3. compareCalendar(Calendar cal1, Calendar cal2)
  4. compareDates(Calendar date1, Calendar date2)
  5. compareDates(final Calendar firstCal, final Calendar secondCal)
  6. compareDay(Calendar calendar, Calendar calendar1)
  7. compareSameDay(Calendar first, Calendar second)
  8. compareTime(final Calendar firstCal, final Calendar secondCal)
  9. isEqual(Calendar calendar, Date date)