Java Date Compare compareDate(Date date1, Date date2)

Here you can find the source of compareDate(Date date1, Date date2)

Description

compare Date

License

Apache License

Declaration

public static int compareDate(Date date1, Date date2) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

public class Main {
    public static int compareDate(Date date1, Date date2) {
        Calendar c1 = Calendar.getInstance();
        c1.setTime(date1);/*from w  w  w  . j av  a 2  s. com*/

        Calendar c2 = Calendar.getInstance();
        c2.setTime(date2);

        if (c1.get(Calendar.YEAR) != c2.get(Calendar.YEAR))
            return c1.get(Calendar.YEAR) - c2.get(Calendar.YEAR);
        if (c1.get(Calendar.MONTH) != c2.get(Calendar.MONTH))
            return c1.get(Calendar.MONTH) - c2.get(Calendar.MONTH);
        return c1.get(Calendar.DAY_OF_MONTH) - c2.get(Calendar.DAY_OF_MONTH);
    }
}

Related

  1. compareDate(Date date1, Date date2)
  2. compareDate(Date DATE1, Date DATE2)
  3. compareDate(Date date1, Date date2)
  4. compareDate(Date date1, Date date2)
  5. compareDate(Date date1, Date date2)
  6. compareDate(Date date1, Date date2)
  7. compareDate(Date date1, Date date2, String pattern)
  8. compareDate(Date first, Date second)
  9. compareDate(Date sourceDate, Date targetDate)