There are multiple ways to compare 2 dates inwards Java, it besides depends on upon what exactly comparing means. If you lot are looking to compare 2 dates to discovery out whether they are equal or not, as well as thus you lot tin just purpose 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 discovery out whether a engagement comes earlier or later to a greater extent than or less other engagement as well as thus you lot receive got 3 choices, you lot tin purpose compareTo() method of java.util.Date class, or you lot tin purpose before() as well as after() method of Date class, or you lot tin purpose before() as well as after() method of Calendar class. I advise leveraging before() as well as after() method of Date class, they are to a greater extent than readable as well as tardily to use.
BTW, all these methods compare engagement at the millisecond level, which agency they accept both engagement as well as fourth dimension into account. If you lot desire to compare just engagement business office without considering time, you lot require to purpose DateFormat cast to format engagement into to a greater extent than or less format as well as and thus compare their String value.
Alternatively, you lot tin purpose joda-time which provides a cast LocalDate, which represents engagement without time, similar to Java 8's LocalDate class. If you lot are running inwards Java 8, as well as thus at that topographic point is no argue non to purpose novel Date as well as Time API. If you lot are inwards a hurry, as well as thus run across Java SE 8 for Really Impatient by Cay S. Horstmann to larn how to purpose novel Date as well as Time API of Java 8 inwards your former as well as novel Java application.
If you lot are doing equality cheque as well as thus it brand feel to purpose equals() method. It does comparing past times checking millisecond values of given dates every bit shown below :
So, if 2 dates receive got 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() as well as after() method - volition render truthful or false
2) Calendar's before() as well as after() method - render truthful or false
3) Date's compareTo() method - render -1 if this engagement event is less than given engagement event as well as +1 if this engagement event is greater than given engagement instance.
All these methods perform a comparing based upon millisecond values only, every bit shown inwards below code of before() as well as after() methods of java.util.Date cast :
See Core Java, Volume 1 - Fundamentals past times Cay S. Horstmann to larn to a greater extent than most comparing Dates inwards Java.
Java Program to compare Dates inwards Java
That's all most how to compare 2 dates inwards Java. When you lot compare dates, it takes both engagement as well as fourth dimension into consideration, because it compares on long millisecond value. Also yell back that Date cast doesn't incorporate whatever timezone information. Date agree seat out of millisecond passed from 1st Jan 1970 inwards UTC. If you lot desire to compare dates without taking fourth dimension into consideration, visit using Java 8 novel Date as well as Time API or joda-time, if you lot are running on Java SE 5, half dozen or 7.
Related Java Date as well as Time tutorial for Programmers
References
Java SE 8 documentation of Date class
Java Se 8 documentation of Timestamp class
BTW, all these methods compare engagement at the millisecond level, which agency they accept both engagement as well as fourth dimension into account. If you lot desire to compare just engagement business office without considering time, you lot require to purpose DateFormat cast to format engagement into to a greater extent than or less format as well as and thus compare their String value.
Alternatively, you lot tin purpose joda-time which provides a cast LocalDate, which represents engagement without time, similar to Java 8's LocalDate class. If you lot are running inwards Java 8, as well as thus at that topographic point is no argue non to purpose novel Date as well as Time API. If you lot are inwards a hurry, as well as thus run across Java SE 8 for Really Impatient by Cay S. Horstmann to larn how to purpose novel Date as well as Time API of Java 8 inwards your former as well as novel Java application.
How to cheque if 2 dates are equal inwards Java
There are 2 ways to cheque if 2 dates are equal inwards Java :- Date's equals() method - render truthful if 2 dates are equal
- Date's compareTo() method - render nada if 2 dates are equal
If you lot are doing equality cheque as well as thus it brand feel to purpose equals() method. It does comparing past times checking millisecond values of given dates every bit shown below :
public boolean equals(Object obj) { return obj instanceof Date && getTime() == ((Date) obj).getTime(); }
So, if 2 dates receive got same millisecond they are considered equal. See Java How to Program past times Dietel for to a greater extent than examples.
How to cheque if a engagement comes earlier or later another
There are 3 ways to cheque if 1 engagement comes earlier or later to a greater extent than or less other engagement inwards JDK, without using whatever 3rd political party library similar joda.1) Date's before() as well as after() method - volition render truthful or false
2) Calendar's before() as well as after() method - render truthful or false
3) Date's compareTo() method - render -1 if this engagement event is less than given engagement event as well as +1 if this engagement event is greater than given engagement instance.
All these methods perform a comparing based upon millisecond values only, every bit shown inwards below code of before() as well as after() methods of java.util.Date cast :
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 larn to a greater extent than most comparing Dates inwards Java.
Java Program to compare Dates inwards Java
package dto; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; /** * 2 ways to compare given dates inwards Java without using * whatever 3rd 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 inwards Java // today as well as 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 besides cheque if 2 dates are equal or non using // compareTo() method, java.util.Date implements // Comparable as well as 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 unlike "); } // checking which dates comes starting fourth dimension // you lot tin purpose before() as well as after() method of Calendar class if(todayCalendar.after(yesterdayCalendar)){ System.out.println("first engagement comes later instant date"); } // Date cast besides has their ain before() as well as after() method if(yesterday.before(today)){ System.out.println("yesterday comes earlier today"); } } } Output 2 dates are not equal to each other Given dates are unlike the first engagement comes later the instant engagement yesterday comes earlier today
That's all most how to compare 2 dates inwards Java. When you lot compare dates, it takes both engagement as well as fourth dimension into consideration, because it compares on long millisecond value. Also yell back that Date cast doesn't incorporate whatever timezone information. Date agree seat out of millisecond passed from 1st Jan 1970 inwards UTC. If you lot desire to compare dates without taking fourth dimension into consideration, visit using Java 8 novel Date as well as Time API or joda-time, if you lot are running on Java SE 5, half dozen or 7.
Related Java Date as well as Time tutorial for Programmers
- How to convert java.sql.Date to java.util.Date inwards Java? (example)
- How to acquire the electrical flow Timestamp inwards Java? (answer)
- Difference between java.util.Date as well as java.sql.Timestamp inwards JDBC? (answer)
- How to format Date inwards Java using SimpleDateFormat? (example)
- How to convert String to Date inwards Java? (answer)
- How to display Date inwards multiple timezones inwards Java? (example)
- How to convert java.util.Date to java.sql.Date inwards JDBC? (answer)
- How to convert java.util.Date to java.sql.Timestamp inwards JDBC? (example)
- How to parse String to Date inwards Java using JodaTime library? (example)
References
Java SE 8 documentation of Date class
Java Se 8 documentation of Timestamp class

0 Response to "How to compare ii dates inward Java? Examples"
Posting Komentar