Example usage for java.util Date compareTo

List of usage examples for java.util Date compareTo

Introduction

In this page you can find the example usage for java.util Date compareTo.

Prototype

public int compareTo(Date anotherDate) 

Source Link

Document

Compares two Dates for ordering.

Usage

From source file:Main.java

public static void main(String[] args) {

    Date date = new Date();
    Date date2 = new Date();

    // make 3 comparisons with them
    int comparison = date.compareTo(date2);
    int comparison2 = date2.compareTo(date);
    int comparison3 = date.compareTo(date);

    // print the results
    System.out.println("Comparison Result:" + comparison);
    System.out.println("Comparison2 Result:" + comparison2);
    System.out.println("Comparison3 Result:" + comparison3);

}

From source file:Main.java

public static void main(String[] args) {
    Date d1 = new Date();

    Date d2 = new Date();
    System.out.println("First Date : " + d1);
    System.out.println("Second Date : " + d2);

    int results = d1.compareTo(d2);
    if (results > 0) {
        System.out.println("after");
    } else if (results < 0) {
        System.out.println("before");
    } else {/*from   ww  w . j  a v a 2 s.c  o m*/
        System.out.println("Both dates are equal");
    }
}

From source file:Main.java

public static void main(String[] args) {
    List<String> yearList = new ArrayList<>(25);
    yearList.add("042015");
    yearList.add("052015");
    yearList.add("062015");
    yearList.add("072015");
    yearList.add("082015");
    yearList.add("092015");
    yearList.add("102010");
    yearList.add("112010");
    yearList.add("122010");
    yearList.add("012015");
    yearList.add("022015");
    yearList.add("032015");

    Collections.sort(yearList, new Comparator<String>() {
        private DateFormat format = new SimpleDateFormat("MMyyyy");

        @Override//from  w  w  w . j a v a2  s.c om
        public int compare(String o1, String o2) {
            int result = 0;
            try {
                Date d1 = format.parse(o1);
                try {
                    Date d2 = format.parse(o2);
                    result = d1.compareTo(d2);
                } catch (ParseException ex) {
                    result = -1;
                }
            } catch (ParseException ex) {
                result = 1;
            }
            return result;
        }
    });
    System.out.println(yearList);
}

From source file:Main.java

public static void main(String[] args) {
    try {/*from  www . j a v a  2 s.c om*/
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = sdf.parse("2015-12-31");
        Date date2 = sdf.parse("2015-01-31");

        System.out.println(sdf.format(date1));
        System.out.println(sdf.format(date2));

        if (date1.compareTo(date2) > 0) {
            System.out.println("Date1 is after Date2");
        } else if (date1.compareTo(date2) < 0) {
            System.out.println("Date1 is before Date2");
        } else if (date1.compareTo(date2) == 0) {
            System.out.println("Date1 is equal to Date2");
        } else {
            System.out.println("How to get here?");
        }

    } catch (ParseException ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static boolean isEqual(Date date1, Date date2) {
    return date1.compareTo(date2) == 0;
}

From source file:Main.java

public static int compareTo(Date cal1, Date cal2) {
    return cal1.compareTo(cal2);
}

From source file:Main.java

public static boolean compareDates(Date thisDate, Date todayDate) {

    int results = todayDate.compareTo(thisDate);

    if (results > 0)
        System.out.println("First Date is after second");
    else if (results < 0)
        System.out.println("First Date is before second");
    else/*  w w w. j a v  a  2s  .  c om*/
        return true;

    return false;

}

From source file:Main.java

private static boolean isBeforeMonths(int months, Date aDate) {
    Calendar calendar = Calendar.getInstance();
    calendar.add(MONTH, months);//from ww w  .j  av  a  2 s . c  o m
    return aDate.compareTo(calendar.getTime()) < 0;
}

From source file:Main.java

private static Boolean currentGreaterThenOld(String currentDate, String oldDate) {
    if (currentDate.isEmpty() || oldDate.isEmpty())
        return false;

    try {/*from  w w  w . jav a 2s  .com*/
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
        Date current = formatter.parse(currentDate);
        Date old = formatter.parse(oldDate);

        if (old.compareTo(current) < 0) {
            Log.d(TAG, "compareDate current Date : " + current.toString() + " is greater then Old Date : "
                    + old.toString());
            return true;
        }

    } catch (ParseException e1) {
        e1.printStackTrace();
    }
    return false;
}

From source file:Main.java

public static int compare(String s1, String s2) {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
    try {// w w  w .j  av a  2 s  .  c  o  m
        Date d1 = format.parse(s1);
        Date d2 = format.parse(s2);
        return d1.compareTo(d2);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return -1;
}