Saturday, November 9, 2019

How To Compare 2 Dates Inwards Java? Banking Corporation Gibe If They Are Equal, Afterward Or Earlier

There are multiple ways to compare ii dates inwards Java, it too depends on upon what exactly comparing means. If y'all are looking to compare ii dates to abide by out whether they are equal or not, thence y'all tin just utilisation equals() method of java.util.Date class. This method volition render truthful if both dates are equal, just both thave same millisecond value. If y'all are looking to abide by out whether a engagement comes earlier or afterward about other engagement thence y'all receive got 3 choices, y'all tin utilisation compareTo() method of java.util.Date class, or y'all tin utilisation before() in addition to after() method of Date class, or y'all tin utilisation before() in addition to after() method of Calendar class. I propose leveraging before() in addition to after() method of Date class, they are to a greater extent than readable in addition to tardily to use.

BTW, all these methods compare engagement at the millisecond level, which agency they accept both engagement in addition to fourth dimension into account. If y'all desire to compare just engagement purpose without considering time, y'all bespeak to utilisation DateFormat shape to format engagement into about format in addition to thence compare their String value.

Alternatively, y'all tin utilisation joda-time which provides a shape LocalDate, which represents engagement without time, similar to Java 8's LocalDate class. If y'all are running inwards Java 8, thence at that spot is no argue non to utilisation novel Date in addition to Time API.  If y'all are inwards a hurry, thence come across Java SE 8 for Really Impatient by Cay S. Horstmann to larn how to utilisation novel Date in addition to Time API of Java 8 inwards your onetime in addition to novel Java application.



How to depository fiscal establishment jibe if ii dates are equal inwards Java

There are ii ways to depository fiscal establishment jibe if ii dates are equal inwards Java :
  • Date's equals() method - render truthful if ii dates are equal
  • Date's compareTo() method - render nil if ii dates are equal



If y'all are doing equality depository fiscal establishment jibe thence it brand feel to utilisation 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 ii 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 depository fiscal establishment jibe if a engagement comes earlier or afterward another

There are 3 ways to depository fiscal establishment jibe if i engagement comes earlier or afterward about other engagement inwards JDK, without using whatever tertiary political party library similar joda.

1) Date's before() in addition to after() method - volition render truthful or false
2) Calendar's before() in addition to after() method - render truthful or false
3) Date's compareTo() method - render -1 if this engagement representative is less than given engagement representative in addition to +1 if this engagement representative 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() in addition to 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 larn to a greater extent than almost 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 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 inwards Java        // today in addition to 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 ");        }                        // y'all tin too depository fiscal establishment jibe if ii dates are equal or non using        // compareTo() method, java.util.Date implements         // Comparable in addition to 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 start         // y'all tin utilisation before() in addition to after() method of Calendar class        if(todayCalendar.after(yesterdayCalendar)){            System.out.println("first engagement comes afterward mo date");        }                // Date shape too has their ain before() in addition to 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 afterward the mo engagement yesterday comes earlier today


That's all almost how to compare ii dates inwards Java. When y'all compare dates, it takes both engagement in addition to fourth dimension into consideration, because it compares on long millisecond value. Also scream back that Date shape doesn't comprise whatever timezone information. Date concord let out of millisecond passed from 1st Jan 1970 inwards UTC. If y'all desire to compare dates without taking fourth dimension into consideration, take in using Java 8 novel Date in addition to Time API or joda-time, if y'all are running on Java SE 5, half dozen or 7.

Related Java Date in addition to Time tutorial for Programmers
  • How to convert java.sql.Date to java.util.Date inwards Java? (example)
  • How to acquire the electrical current Timestamp inwards Java? (answer)
  • Difference between java.util.Date in addition to 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)


Further Learning
Complete Java Masterclass
Java Fundamentals: The Java Language
Java In-Depth: Become a Complete Java Engineer!


No comments:

Post a Comment