Java Date Compare compareDate(Date DATE1, Date DATE2)

Here you can find the source of compareDate(Date DATE1, Date DATE2)

Description

compare Date

License

Open Source License

Declaration

public static int compareDate(Date DATE1, Date DATE2) 

Method Source Code


//package com.java2s;
import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {

    public static int compareDate(String DATE1, String DATE2) {

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {/* w  w  w.  jav  a 2  s . c o m*/
            Date dt1 = df.parse(DATE1);
            Date dt2 = df.parse(DATE2);
            if (dt1.getTime() > dt2.getTime()) {
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }

    public static int compareDate(Date DATE1, Date DATE2) {

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date dt1 = df.parse(df.format(DATE1));
            Date dt2 = df.parse(df.format(DATE2));
            if (dt1.getTime() > dt2.getTime()) {
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }
}

Related

  1. compareDate(Date d1, Date d2)
  2. compareDate(Date d1, Date d2)
  3. CompareDate(Date date, Date otherdate)
  4. compareDate(Date date1)
  5. compareDate(Date date1, Date date2)
  6. compareDate(Date date1, Date date2)
  7. compareDate(Date date1, Date date2)
  8. compareDate(Date date1, Date date2)
  9. compareDate(Date date1, Date date2)