Java Date Compare compare2Dates(Date d1, Date d2)

Here you can find the source of compare2Dates(Date d1, Date d2)

Description

Compare two days and returns -1 if d1 is less than d2 0 if both are equal, 1 if d1 is greater than d2

License

Open Source License

Parameter

Parameter Description
d1 First date to compare
d2 Second date to compre

Return

-1 if d1d2

Declaration

public static int compare2Dates(Date d1, Date d2) 

Method Source Code

//package com.java2s;

import java.util.Calendar;

import java.util.Date;

public class Main {
    /**/* w w w.j  av a 2  s .com*/
     * Compare two days and returns -1 if d1 is less than d2 
     * 0 if both are equal, 1 if d1 is greater than d2
     * @param d1 First date to compare
     * @param d2 Second date to compre
     * @return -1 if d1<d2, 0 if d1==d2, 1 if d1>d2
     */
    public static int compare2Dates(Date d1, Date d2) {
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(d1);
        c2.setTime(d2);
        if (c1.before(c2)) {
            return -1;
        } else if (c1.after(c2)) {
            return 1;
        }
        return 0;
    }
}

Related

  1. compare(Date start, String end)
  2. compare(Date startDate, Date endDate, Date targetDate)
  3. compare(final Date lhs, final Date rhs)
  4. compare(final Date one, final Date another)
  5. compare2Date(Date date1, Date date2)
  6. compare2DateString(String DateStringType, String dateStr1, String dateStr2)
  7. compare_date2(String finalDate, String enddate)
  8. compareCalendars(Date fechaAComparar, Date fechaBase)
  9. compareDate(Date d1, Date d2)