How To Compare Ii Dates Inward Java? Cheque If They Are Equal, Subsequently Or Earlier
There are multiple ways to compare ii dates inward Java, it likewise depends on upon what exactly comparing means. If you lot are looking to compare ii dates to honor out whether they are equal or not, together with thus you lot tin just utilization equals() method of java.util.Date class. This method volition render truthful if both dates are equal, exactly both thave same millisecond value. If you lot are looking to honor out whether a engagement comes earlier or subsequently unopen to other engagement together with thus you lot direct keep 3 choices, you lot tin utilization compareTo() method of java.util.Date class, or you lot tin utilization before() together with after() method of Date class, or you lot tin utilization before() together with after() method of Calendar class. I propose leveraging before() together with after() method of Date class, they are to a greater extent than readable together with slowly to use.
BTW, all these methods compare engagement at the millisecond level, which agency they direct keep both engagement together with fourth dimension into account. If you lot desire to compare just engagement role without considering time, you lot require to utilization DateFormat shape to format engagement into unopen to format together with and thus compare their String value.
Alternatively, you lot tin utilization joda-time which provides a shape LocalDate, which represents engagement without time, similar to Java 8's LocalDate class. If you lot are running inward Java 8, together with thus at that topographic point is no argue non to utilization novel Date together with Time API. If you lot are inward a hurry, together with thus encounter Java SE 8 for Really Impatient by Cay S. Horstmann to acquire how to utilization novel Date together with Time API of Java 8 inward your quondam together with novel Java application.
If you lot are doing equality banking concern jibe together with thus it brand feel to utilization equals() method. It does comparing past times checking millisecond values of given dates equally shown below :
So, if ii dates direct keep same millisecond they are considered equal. See Java How to Program past times Dietel for to a greater extent than examples.
1) Date's before() together with after() method - volition render truthful or false
2) Calendar's before() together with after() method - render truthful or false
3) Date's compareTo() method - render -1 if this engagement illustration is less than given engagement illustration together with +1 if this engagement illustration is greater than given engagement instance.
All these methods perform a comparing based upon millisecond values only, equally shown inward below code of before() together with after() methods of java.util.Date shape :
See Core Java, Volume 1 - Fundamentals past times Cay S. Horstmann to acquire to a greater extent than well-nigh comparing Dates inward Java.
Java Program to compare Dates inward Java
That's all well-nigh how to compare ii dates inward Java. When you lot compare dates, it takes both engagement together with fourth dimension into consideration, because it compares on long millisecond value. Also think that Date shape doesn't comprise whatsoever timezone information. Date concord set out of millisecond passed from 1st Jan 1970 inward UTC. If you lot desire to compare dates without taking fourth dimension into consideration, see using Java 8 novel Date together with Time API or joda-time, if you lot are running on Java SE 5, six or 7.
Related Java Date together with Time tutorial for Programmers
Further Learning
Complete Java Masterclass
Java Fundamentals: The Java Language
Java In-Depth: Become a Complete Java Engineer!
BTW, all these methods compare engagement at the millisecond level, which agency they direct keep both engagement together with fourth dimension into account. If you lot desire to compare just engagement role without considering time, you lot require to utilization DateFormat shape to format engagement into unopen to format together with and thus compare their String value.
Alternatively, you lot tin utilization joda-time which provides a shape LocalDate, which represents engagement without time, similar to Java 8's LocalDate class. If you lot are running inward Java 8, together with thus at that topographic point is no argue non to utilization novel Date together with Time API. If you lot are inward a hurry, together with thus encounter Java SE 8 for Really Impatient by Cay S. Horstmann to acquire how to utilization novel Date together with Time API of Java 8 inward your quondam together with novel Java application.
How to banking concern jibe if ii dates are equal inward Java
There are ii ways to banking concern jibe if ii dates are equal inward Java :- Date's equals() method - render truthful if ii dates are equal
- Date's compareTo() method - render zip if ii dates are equal
If you lot are doing equality banking concern jibe together with thus it brand feel to utilization equals() method. It does comparing past times checking millisecond values of given dates equally shown below :
public boolean equals(Object obj) { return obj instanceof Date && getTime() == ((Date) obj).getTime(); }
So, if ii dates direct keep same millisecond they are considered equal. See Java How to Program past times Dietel for to a greater extent than examples.
How to banking concern jibe if a engagement comes earlier or subsequently another
There are iii ways to banking concern jibe if 1 engagement comes earlier or subsequently unopen to other engagement inward JDK, without using whatsoever tertiary political party library similar joda.1) Date's before() together with after() method - volition render truthful or false
2) Calendar's before() together with after() method - render truthful or false
3) Date's compareTo() method - render -1 if this engagement illustration is less than given engagement illustration together with +1 if this engagement illustration is greater than given engagement instance.
All these methods perform a comparing based upon millisecond values only, equally shown inward below code of before() together with after() methods of java.util.Date shape :
public boolean before(Date when) { return getMillisOf(this) < getMillisOf(when); } public boolean after(Date when) { return getMillisOf(this) > getMillisOf(when); }
See Core Java, Volume 1 - Fundamentals past times Cay S. Horstmann to acquire to a greater extent than well-nigh comparing Dates inward Java.
Java Program to compare Dates inward Java
package dto; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; /** * 2 ways to compare given dates inward Java without using * whatsoever tertiary political party library e.g. joda time. * * @author WINDOWS 8 * */ public class DateComparator { public static void main(String args[]) { Calendar todayCalendar = new GregorianCalendar(2015, Calendar.AUGUST, 24); Calendar yesterdayCalendar = new GregorianCalendar(2015, Calendar.AUGUST, 23); Date today = todayCalendar.getTime(); Date yesterday = yesterdayCalendar.getTime(); // comparing dates using equals() method inward Java // today together with yesterday is non equal if(today.equals(yesterday)){ System.out.println("two given dates are equal to each other"); }else{ System.out.println("two dates are non equal to each other "); } // you lot tin likewise banking concern jibe if ii dates are equal or non using // compareTo() method, java.util.Date implements // Comparable together with its compareTo() is consistent amongst equals() if(yesterday.compareTo(today) == 0){ System.out.println("Given dates are same"); }else{ System.out.println("Given dates are dissimilar "); } // checking which dates comes offset // you lot tin utilization before() together with after() method of Calendar class if(todayCalendar.after(yesterdayCalendar)){ System.out.println("first engagement comes subsequently instant date"); } // Date shape likewise has their ain before() together with after() method if(yesterday.before(today)){ System.out.println("yesterday comes earlier today"); } } } Output ii dates are not equal to each other Given dates are dissimilar the first engagement comes subsequently the instant engagement yesterday comes earlier today
That's all well-nigh how to compare ii dates inward Java. When you lot compare dates, it takes both engagement together with fourth dimension into consideration, because it compares on long millisecond value. Also think that Date shape doesn't comprise whatsoever timezone information. Date concord set out of millisecond passed from 1st Jan 1970 inward UTC. If you lot desire to compare dates without taking fourth dimension into consideration, see using Java 8 novel Date together with Time API or joda-time, if you lot are running on Java SE 5, six or 7.
Related Java Date together with Time tutorial for Programmers
- How to convert java.sql.Date to java.util.Date inward Java? (example)
- How to acquire the electrical flow Timestamp inward Java? (answer)
- Difference between java.util.Date together with java.sql.Timestamp inward JDBC? (answer)
- How to format Date inward Java using SimpleDateFormat? (example)
- How to convert String to Date inward Java? (answer)
- How to display Date inward multiple timezones inward Java? (example)
- How to convert java.util.Date to java.sql.Date inward JDBC? (answer)
- How to convert java.util.Date to java.sql.Timestamp inward JDBC? (example)
- How to parse String to Date inward Java using JodaTime library? (example)
Further Learning
Complete Java Masterclass
Java Fundamentals: The Java Language
Java In-Depth: Become a Complete Java Engineer!
0 Response to "How To Compare Ii Dates Inward Java? Cheque If They Are Equal, Subsequently Or Earlier"
Post a Comment