3 Representative To Compare 2 Dates Inwards Java

comparing dates inwards Java is mutual postulate for Java programmer, straight off as well as and then nosotros postulate to compare 2 dates to banking concern tally whether two dates are equals, less than or greater than each other, roughly fourth dimension nosotros every bit good needs to banking concern tally if i dates comes inwards betwixt 2 dates. Java provides multiple ways to compare 2 Dates inwards Java which is capable of performing Date comparing as well as letting y'all know whether 2 dates are same, i engagement come upward earlier or afterward roughly other engagement inwards Java. In in conclusion brace of article nosotros convey seen how to convert String to Date inwards Java as well as Converting  Date to String inwards Java as well as hither nosotros volition encounter iii unlike example of comparing 2 dates inwards Java.

How to compare 2 dates inwards Java 

There are multiple ways to compare 2 dates inwards Java and below is 3 ways I used to compare multiple dates inwards Java
  1) yesteryear Using classic CompareTo method of Date class.
  2) yesteryear using equals(), before() as well as afterward method of Date class.
  3) yesteryear using equals(), before() as well as afterward method of Calendar bird inwards Java.

 straight off as well as and then nosotros postulate to compare 2 dates to banking concern tally whether  3 Example to Compare 2 Dates inwards Javahow nosotros compare 2 objects inwards Java or yesteryear using Date's equals(), earlier as well as after() method because that sounds to a greater extent than natural to me.  Here is code illustration of comparing unlike dates inwards Java.


import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class HashtableDemo {

    public static void main(String args[]) throws AssertionError, ParseException {

        DateFormat df = new SimpleDateFormat("dd-MM-yyyy");

        //comparing engagement using compareTo method inwards Java
        System.out.println("Comparing 2 Date inwards Java using CompareTo method");
        compareDatesByCompareTo(df, df.parse("01-01-2012"), df.parse("01-01-2012"));
        compareDatesByCompareTo(df, df.parse("02-03-2012"), df.parse("04-05-2012"));
        compareDatesByCompareTo(df, df.parse("02-03-2012"), df.parse("01-02-2012"));

        //comparing dates inwards coffee using Date.before, Date.after as well as Date.equals
        System.out.println("Comparing 2 Date inwards Java using Date's before, afterward as well as equals method");
        compareDatesByDateMethods(df, df.parse("01-01-2012"), df.parse("01-01-2012"));
        compareDatesByDateMethods(df, df.parse("02-03-2012"), df.parse("04-05-2012"));
        compareDatesByDateMethods(df, df.parse("02-03-2012"), df.parse("01-02-2012"));

        //comparing dates inwards coffee using Calendar.before(), Calendar.after as well as Calendar.equals()
        System.out.println("Comparing 2 Date inwards Java using Calendar's before, afterward as well as equals method");
        compareDatesByCalendarMethods(df, df.parse("01-01-2012"), df.parse("01-01-2012"));
        compareDatesByCalendarMethods(df, df.parse("02-03-2012"), df.parse("04-05-2012"));
        compareDatesByCalendarMethods(df, df.parse("02-03-2012"), df.parse("01-02-2012"));

    }

    public static void compareDatesByCompareTo(DateFormat df, Date oldDate, Date newDate) {
        //how to banking concern tally if date1 is equal to date2
        if (oldDate.compareTo(newDate) == 0) {
            System.out.println(df.format(oldDate) + " as well as " + df.format(newDate) + " are equal to each other");
        }

        //checking if date1 is less than engagement 2
        if (oldDate.compareTo(newDate) < 0) {
            System.out.println(df.format(oldDate) + " is less than " + df.format(newDate));
        }

        //how to banking concern tally if date1 is greater than date2 inwards java
        if (oldDate.compareTo(newDate) > 0) {
            System.out.println(df.format(oldDate) + " is greater than " + df.format(newDate));
        }
    }

    public static void compareDatesByDateMethods(DateFormat df, Date oldDate, Date newDate) {
        //how to banking concern tally if 2 dates are equals inwards java
        if (oldDate.equals(newDate)) {
            System.out.println(df.format(oldDate) + " as well as " + df.format(newDate) + " are equal to each other");
        }

        //checking if date1 comes earlier date2
        if (oldDate.before(newDate)) {
            System.out.println(df.format(oldDate) + " comes earlier " + df.format(newDate));
        }

        //checking if date1 comes afterward date2
        if (oldDate.after(newDate)) {
            System.out.println(df.format(oldDate) + " comes afterward " + df.format(newDate));
        }
    }

    public static void compareDatesByCalendarMethods(DateFormat df, Date oldDate, Date newDate) {

        //creating calendar instances for engagement comparision
        Calendar oldCal = Calendar.getInstance();
        Calendar newCal = Calendar.getInstance();

        oldCal.setTime(oldDate);
        newCal.setTime(newDate);

        //how to banking concern tally if 2 dates are equals inwards coffee using Calendar
        if (oldCal.equals(newCal)) {
            System.out.println(df.format(oldDate) + " as well as " + df.format(newDate) + " are equal to each other");
        }

        //how to banking concern tally if i engagement comes earlier roughly other using Calendar
        if (oldCal.before(newCal)) {
            System.out.println(df.format(oldDate) + " comes earlier " + df.format(newDate));
        }

        //how to banking concern tally if i engagement comes afterward roughly other using Calendar
        if (oldCal.after(newCal)) {
            System.out.println(df.format(oldDate) + " comes afterward " + df.format(newDate));
        }
    }
}

Output:

Comparing 2 Date inwards Java using CompareTo method
01-01-2012 as well as 01-01-2012 are equal to each other
02-03-2012 is less than 04-05-2012
02-03-2012 is greater than 01-02-2012
Comparing 2 Date inwards Java using Date's before, afterward as well as equals method
01-01-2012 as well as 01-01-2012 are equal to each other
02-03-2012 comes earlier 04-05-2012
02-03-2012 comes afterward 01-02-2012
Comparing 2 Date inwards Java using Calendar's before, afterward as well as equals method
01-01-2012 as well as 01-01-2012 are equal to each other
02-03-2012 comes earlier 04-05-2012
02-03-2012 comes afterward 01-02-2012

That’s all on comparing 2 dates inwards Java. Dates are real of import for writing whatever firm coffee application as well as practiced agreement of Dates as well as Calendar classes, Date Formating ever help.

Further Learning
Complete Java Masterclass
How to discovery DeadLock inwards Java

0 Response to "3 Representative To Compare 2 Dates Inwards Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel