Java Date Compare compareDate(Date d1, Date d2)

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

Description

tra ve 1 neu date1 sau date2

License

Open Source License

Parameter

Parameter Description
d1 a parameter
d2 a parameter

Declaration

public static int compareDate(Date d1, Date d2) 

Method Source Code

//package com.java2s;
/*//  w w w  .  j  ava 2 s. co  m
 * Copyright (C) 2010 Viettel Telecom. All rights reserved.
 * VIETTEL PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

import java.util.Calendar;
import java.util.Date;

public class Main {
    /**
     * tra ve 1 neu date1 sau date2
     *
     * @param d1
     * @param d2
     * @return
     */
    public static int compareDate(Date d1, Date d2) {
        int result = 0;
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        cal1.setTime(d1);
        cal1.set(Calendar.HOUR_OF_DAY, 0);
        cal1.set(Calendar.MINUTE, 0);
        cal1.set(Calendar.SECOND, 0);
        cal1.set(Calendar.MILLISECOND, 0);
        cal2.setTime(d2);
        cal2.set(Calendar.HOUR_OF_DAY, 0);
        cal2.set(Calendar.MINUTE, 0);
        cal2.set(Calendar.SECOND, 0);
        cal2.set(Calendar.MILLISECOND, 0);
        if (cal1.after(cal2)) {
            result = 1;
        } else if (cal1.before(cal2)) {
            result = -1;
        }
        return result;
    }
}

Related

  1. compare2Dates(Date d1, Date d2)
  2. compare2DateString(String DateStringType, String dateStr1, String dateStr2)
  3. compare_date2(String finalDate, String enddate)
  4. compareCalendars(Date fechaAComparar, Date fechaBase)
  5. compareDate(Date d1, Date d2)
  6. compareDate(Date d1, Date d2)
  7. CompareDate(Date date, Date otherdate)
  8. compareDate(Date date1)
  9. compareDate(Date date1, Date date2)