• About
  • Contact
  • Cookie
  • Disclaimer
  • Privacy Policy
  • Change the purpose of use

Tnhelearning.edu.vn - Various useful general information portal

  • Photo
  • Bio
  • How To
  • Tech

How to Compare Two Dates in Java

February 8, 2024 by admin Category: How To

You are viewing the article How to Compare Two Dates in Java  at Tnhelearning.edu.vn you can quickly access the necessary information in the table of contents of the article below.

X

wikiHow is a “wiki” site, which means that many of the articles here are written by multiple authors. To create this article, volunteer authors have edited and improved the article over time.

This article has been viewed 22,475 times.

There are many ways to compare dates in Java. In essence, the date represents a (long) time and is written as the number of milliseconds that have passed since 1/1/1970. In Java, Date is an object with many comparison methods. Any method that compares two dates is essentially comparing the time of day.

Table of Contents

  • Steps
    • By compareTo . method
    • By equals, after and before . methods
    • By class calendar
    • By method getTime

Steps

By compareTo . method

Image titled 4301351 1

Image titled 4301351 1

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/6/6a/4301351-1.jpg/v4-728px-4301351-1.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/6/6a/4301351-1.jpg/v4-728px-4301351-1.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
Use compareTo method. Date is the comparable successor of <Date> and so two dates can be compared directly using the compareTo method. If these dates have the same time, the method will return 0. If the date to be compared is before the other date, a value less than 0 will be returned. If the date to be compared is after the remaining date, the return value will be greater than 0. If the two dates are equal, the value 0 will be returned. [1] X Research Source
Image titled 4301351 2

Image titled 4301351 2

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/8/8c/4301351-2.jpg/v4-728px-4301351-2.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/8/8c/4301351-2.jpg/v4-728px-4301351-2.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
Create a date object. You will need to create each date object before you start comparing them. One of the ways to do this is to use the SimpleDateFormat class. This class allows to enter date values into date object easily.

     SimpleDateFormat sdf = new SimpleDateFormat ( "yyyy-MM-dd" ); //To declare new values in the new date object, use the same format when creating the date. Date date1 = sdf . parse ( "1995-02-23" ); //date1 is 23/2/1995 Date date2 = sdf . parse ( "2001-10-31" ); //date2 is 31/10/2001 Date date3 = sdf . parse ( "1995-02-23" ); //date3 is 23/2/1995 
Image titled 4301351 3

READ More:   How to Set Timer Baby Clock

Image titled 4301351 3

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/5/5d/4301351-3.jpg/v4-728px-4301351-3.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/5/5d/4301351-3.jpg/v4-728px-4301351-3.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
Compare date objects. The code below will show you each case: less than, equal to and more than.

     date1 . compareTo ( date2 ); //date1 < date2, returns a value less than 0 date2 . compareTo ( date1 ); //date2 > date1, returns a value greater than 0 date1 . compareTo ( date3 ); //date1 = date3, display value 0 

By equals, after and before . methods

Image titled 4301351 4

Image titled 4301351 4

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/7/70/4301351-4.jpg/v4-728px-4301351-4.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/7/70/4301351-4.jpg/v4-728px-4301351-4.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
Use equals, after and before methods. You can compare dates using the equals, after and before methods. If two dates have the same time, the equals method will return true. The examples below will use the date generated from the previous compareTo method. [2] X Research Source
Image titled 4301351 5

Image titled 4301351 5

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/d/dd/4301351-5.jpg/v4-728px-4301351-5.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/d/dd/4301351-5.jpg/v4-728px-4301351-5.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
Compare using the before method. This code has two cases: true and false. If date1 is before date2, before will return true. Otherwise, before will return false.

     System . out . print ( date1 . before ( date2 )); //return true System . out . print ( date2 . before ( date2 )); //return false value 
Image titled 4301351 6

Image titled 4301351 6

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/a/ad/4301351-6.jpg/v4-728px-4301351-6.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/a/ad/4301351-6.jpg/v4-728px-4301351-6.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
Compare using the after method. This code has two cases: true and false. If date2 is present after date1, after will return true. Otherwise, after will display false value.

     System . out . print ( date2 . after ( date1 )); //return true System . out . print ( date1 . after ( date2 )); //return false value 
Image titled 4301351 7

Image titled 4301351 7

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/e/ed/4301351-7.jpg/v4-728px-4301351-7.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/e/ed/4301351-7.jpg/v4-728px-4301351-7.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
Compare using equals method. This code has two cases: true and false. If these two dates are at the same time, equals will return true. Otherwise, equals will return false.

     System . out . print ( date1 . equals ( date3 )); //return true System . out . print ( date1 . equals ( date2 )); //return false value 

By class calendar

Image titled 4301351 8

Image titled 4301351 8

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/6/6f/4301351-8.jpg/v4-728px-4301351-8.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/6/6f/4301351-8.jpg/v4-728px-4301351-8.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
Use a calendar. The calendar class also has compareTo, equals, after and before methods, which work similarly to the date class described above. So if the date information is in the calendar then you don’t need to extract the date just for comparison. [3] X Research Sources
Image titled 4301351 9

Image titled 4301351 9

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/5/5c/4301351-9.jpg/v4-728px-4301351-9.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/5/5c/4301351-9.jpg/v4-728px-4301351-9.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
Create a case in Calendar. To use the methods in the calendar, you’ll need some instance of Calendar. Luckily you only need to get the time from the built-in Date instances.

     Calendar cal1 = Calendar . getInstance (); // denotes cal1 Calendar cal2 = Calendar . getInstance (); //indicates as cal2 Calendar cal3 = Calendar . getInstance (); // denotes cal3 cal1 . setTime ( date1 ); //apply date to cal1 cal2 . setTime ( date2 ); cal3 . setTime ( date3 ); 
Image titled 4301351 10

READ More:   How to Clean the Nespresso Capsule Machine

Image titled 4301351 10

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/8/8d/4301351-10.jpg/v4-728px-4301351-10.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/8/8d/4301351-10.jpg/v4-728px-4301351-10.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
Compare cal1 and cal2 using the before method. The code below will return true because cal1 is before cal2.

     System . out . print ( cal1 . before ( cal2 )); //return true 
Image titled 4301351 11

Image titled 4301351 11

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/2/29/4301351-11.jpg/v4-728px-4301351-11.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/2/29/4301351-11.jpg/v4-728px-4301351-11.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
Compare cal1 and cal2 using the after method. The code below will return false because cal1 is before cal2.

     System . out . print ( cal1 . after ( cal2 )); //return false 
Image titled 4301351 12

Image titled 4301351 12

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/f/f4/4301351-12.jpg/v4-728px-4301351-12.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/f/f4/4301351-12.jpg/v4-728px-4301351-12.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
Compare cal1 and cal2 using the equals method. The code below will show examples of both true and false. The condition depends on the case where the calendar is compared. This code will return “true”, then “false” on the next line.

     System . out . println ( cal1 . equals ( cal3 )); //return true: cal1 == cal3 System . out . print ( cal1 . equals ( cal2 )); //return false: cal1 != cal2 

By method getTime

Image titled 4301351 13

Image titled 4301351 13

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/2/24/4301351-13.jpg/v4-728px-4301351-13.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/2/24/4301351-13.jpg/v4-728px-4301351-13.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
Use getTime method. This method can also compare two dates directly, but the methods above are easier to read and prefer. This is a comparison between two primitive data types, so can return “<“, “>” and “==”.
Image titled 4301351 14

Image titled 4301351 14

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/c/c3/4301351-14.jpg/v4-728px-4301351-14.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/c/c3/4301351-14.jpg/v4-728px-4301351-14.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
Create a long time object. Before comparing dates, you need to create a long integer of type long that takes data from previously created Date objects. Luckily the getTime() method will do most of the work for you.

     long time1 = getTime ( date1 ); //represent primitive time1 from date1 long time2 = getTime ( date2 ); //represent primitive time2 from date2 
Image titled 4301351 15

Image titled 4301351 15

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/6/62/4301351-15.jpg/v4-728px-4301351-15.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/6/62/4301351-15.jpg/v4-728px-4301351-15.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
Do the smaller comparison. Use less than sign (<) to compare two integer values. Because time1 precedes time 2, the first result will be displayed. The remaining statement will be included for proper syntax.

     if ( time1 < time2 ){ System . out . println ( "date1 is before date2" ); //this result will show because time1 <time2 } else { System . out . println ( "date1 is not before date2" ); } 
Image titled 4301351 16

READ More:   How to Write a Scientific Research Methodology

Image titled 4301351 16

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/3/3a/4301351-16.jpg/v4-728px-4301351-16.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/3/3a/4301351-16.jpg/v4-728px-4301351-16.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
Apply greater than comparison. Use the greater than sign (>) to compare the two integer values above. Because time1 is greater than time 2, the first result will be returned. The remaining statement will be included for proper syntax.

     if ( time2 > time1 ){ System . out . println ( "date2 is after date1" ); //this result will be visible because time2 > time1 } else { System . out . println ( "date2 is not after date1" ); } 
  • Image titled 4301351 17

    Image titled 4301351 17

    {“smallUrl”:”https://www.wikihow.com/images_en/thumb/d/dd/4301351-17.jpg/v4-728px-4301351-17.jpg”,”bigUrl”:”https:// www.wikihow.com/images/thumb/d/dd/4301351-17.jpg/v4-728px-4301351-17.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,” bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></div>”}
    Perform equality comparison. Use the equality check mark (==) to compare if two integer values are equal or not. Because time1 is equal to time3, the first result will be returned. If the program receives a different statement, it means that the times are not equal. [4] X Research Sources

       if ( time1 == time2 ){ System . out . println ( "the dates are equal" ); } else { System . out . println ( "the dates are not equal" ); //this result will be visible because time1 != time2 } 
  • X

    wikiHow is a “wiki” site, which means that many of the articles here are written by multiple authors. To create this article, volunteer authors have edited and improved the article over time.

    This article has been viewed 22,475 times.

    There are many ways to compare dates in Java. In essence, the date represents a (long) time and is written as the number of milliseconds that have passed since 1/1/1970. In Java, Date is an object with many comparison methods. Any method that compares two dates is essentially comparing the time of day.

    Thank you for reading this post How to Compare Two Dates in Java at Tnhelearning.edu.vn You can comment, see more related articles below and hope to help you with interesting information.

    Related Search:

    Related Posts

    How to Create Curved Text in Photoshop
    How to fall asleep faster
    How to Install FBReader to Read eBooks

    Category: How To

    Previous Post: « Top 10 Phu Yen tourist destinations with the most beautiful beaches
    Next Post: Top 9 most fragrant women’s Burberry perfume bottles that you can’t miss »

    Copyright © 2025 · Tnhelearning.edu.vn - Useful Knowledge