3 Event To Compare 2 Dates Inwards Java

comparing dates inward Java is mutual require for Java programmer, right away together with thence nosotros require to compare 2 dates to depository fiscal establishment fit whether two dates are equals, less than or greater than each other, or thence fourth dimension nosotros likewise needs to depository fiscal establishment fit if i dates comes inward betwixt 2 dates. Java provides multiple ways to compare 2 Dates inward Java which is capable of performing Date comparing together with letting yous know whether 2 dates are same, i engagement come upwards earlier or later or thence other engagement inward Java. In lastly couplet of article nosotros convey seen how to convert String to Date inward Java together with Converting  Date to String inward Java together with hither nosotros volition run into iii unlike example of comparing 2 dates inward Java.

How to compare 2 dates inward Java 

There are multiple ways to compare 2 dates inward Java and below is 3 ways I used to compare multiple dates inward Java
  1) past times Using classic CompareTo method of Date class.
  2) past times using equals(), before() together with later method of Date class.
  3) past times using equals(), before() together with later method of Calendar shape inward Java.

 right away together with thence nosotros require to compare 2 dates to depository fiscal establishment fit whether  3 Example to Compare 2 Dates inward Javahow nosotros compare 2 objects inward Java or past times using Date's equals(), earlier together with after() method because that sounds to a greater extent than natural to me.  Here is code representative of comparing unlike dates inward 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 inward Java
        System.out.println("Comparing 2 Date inward 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 inward coffee using Date.before, Date.after together with Date.equals
        System.out.println("Comparing 2 Date inward Java using Date's before, later together with 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 inward coffee using Calendar.before(), Calendar.after together with Calendar.equals()
        System.out.println("Comparing 2 Date inward Java using Calendar's before, later together with 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 depository fiscal establishment fit if date1 is equal to date2
        if (oldDate.compareTo(newDate) == 0) {
            System.out.println(df.format(oldDate) + " together with " + 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 depository fiscal establishment fit if date1 is greater than date2 inward 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 depository fiscal establishment fit if 2 dates are equals inward java
        if (oldDate.equals(newDate)) {
            System.out.println(df.format(oldDate) + " together with " + 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 later date2
        if (oldDate.after(newDate)) {
            System.out.println(df.format(oldDate) + " comes later " + 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 depository fiscal establishment fit if 2 dates are equals inward coffee using Calendar
        if (oldCal.equals(newCal)) {
            System.out.println(df.format(oldDate) + " together with " + df.format(newDate) + " are equal to each other");
        }

        //how to depository fiscal establishment fit if i engagement comes earlier or thence other using Calendar
        if (oldCal.before(newCal)) {
            System.out.println(df.format(oldDate) + " comes earlier " + df.format(newDate));
        }

        //how to depository fiscal establishment fit if i engagement comes later or thence other using Calendar
        if (oldCal.after(newCal)) {
            System.out.println(df.format(oldDate) + " comes later " + df.format(newDate));
        }
    }
}

Output:

Comparing 2 Date inward Java using CompareTo method
01-01-2012 together with 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 inward Java using Date's before, later together with equals method
01-01-2012 together with 01-01-2012 are equal to each other
02-03-2012 comes earlier 04-05-2012
02-03-2012 comes later 01-02-2012
Comparing 2 Date inward Java using Calendar's before, later together with equals method
01-01-2012 together with 01-01-2012 are equal to each other
02-03-2012 comes earlier 04-05-2012
02-03-2012 comes later 01-02-2012

That’s all on comparing 2 dates inward Java. Dates are real of import for writing whatever company coffee application together with skillful agreement of Dates together with Calendar classes, Date Formating ever help.

Further Learning
Complete Java Masterclass
How to uncovering DeadLock inward Java

Komentar

Postingan populer dari blog ini

Fixing Java.Net.Bindexception: Cannot Assign Requested Address: Jvm_Bind Inwards Tomcat, Jetty

5 Deviation Betwixt Constructor In Addition To Static Mill Method Inward Java- Pros In Addition To Cons

Top V Websites For Practicing Information Structures Together With Algorithms For Coding Interviews Free