Java Date Compare compareDatesOnly(long date1, long date2)

Here you can find the source of compareDatesOnly(long date1, long date2)

Description

Compares the two dates.

License

LGPL

Parameter

Parameter Description
date1 the first date to compare
date2 the second date to compare with

Return

Returns 0 if the two dates are equals, a value < 0 if the first date is before the second one and > 0 if the first date is after the second one

Declaration

public static int compareDatesOnly(long date1, long date2) 

Method Source Code


//package com.java2s;
/*//from w  w w.j  a v a2s  .  c om
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

import java.util.*;

public class Main {
    private static final Calendar c1 = Calendar.getInstance();
    private static final Calendar c2 = Calendar.getInstance();

    /**
     * Compares the two dates. The comparison is based only on the day, month
     * and year values. Returns 0 if the two dates are equals, a value < 0 if
     * the first date is before the second one and > 0 if the first date is
     * after the second one.
     * @param date1 the first date to compare
     * @param date2 the second date to compare with
     * @return Returns 0 if the two dates are equals, a value < 0 if
     * the first date is before the second one and > 0 if the first date is
     * after the second one
     */
    public static int compareDatesOnly(long date1, long date2) {
        c1.setTimeInMillis(date1);
        c2.setTimeInMillis(date2);

        int day1 = c1.get(Calendar.DAY_OF_MONTH);
        int month1 = c1.get(Calendar.MONTH);
        int year1 = c1.get(Calendar.YEAR);

        int day2 = c2.get(Calendar.DAY_OF_MONTH);
        int month2 = c2.get(Calendar.MONTH);
        int year2 = c2.get(Calendar.YEAR);

        if (year1 < year2) {
            return -1;
        } else if (year1 == year2) {
            if (month1 < month2)
                return -1;
            else if (month1 == month2) {
                if (day1 < day2)
                    return -1;
                else if (day1 == day2)
                    return 0;
                else
                    return 1;
            } else
                return 1;
        } else {
            return 1;
        }
    }

    /**
     * Compares the two dates. The comparison is based only on the day, month
     * and year values. Returns 0 if the two dates are equals, a value < 0 if
     * the first date is before the second one and > 0 if the first date is
     * after the second one.
     * @param date1 the first date to compare
     * @param date2 the second date to compare with
     * @return Returns 0 if the two dates are equals, a value < 0 if
     * the first date is before the second one and > 0 if the first date is
     * after the second one
     */
    public static int compareDatesOnly(Date date1, Date date2) {
        return compareDatesOnly(date1.getTime(), date2.getTime());
    }
}

Related

  1. compareDates(Date date1, Date date2)
  2. compareDates(Date date1, Date date2)
  3. compareDates(final Date dateA, final Date dateB)
  4. compareDates(final String format, final Date date1, final Date date2)
  5. compareDateSequence(String startDate, String endDate)
  6. compareDateTimeNull(Date d1, Date d2)
  7. compareDay(Date date, Date anotherDate)
  8. compareDay(Date date1, int compday)
  9. compareDays(final Date date1, final Date date2)