You are viewing the article How to Compare Two Dates in Java at Lassho.edu.vn you can quickly access the necessary information in the table of contents of the article below.
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.
Steps
By compareTo . method


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

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


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

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

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


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 );

System . out . print ( cal1 . before ( cal2 )); //return true

System . out . print ( cal1 . after ( cal2 )); //return false

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


long time1 = getTime ( date1 ); //represent primitive time1 from date1 long time2 = getTime ( date2 ); //represent primitive time2 from date2

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" ); }

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" ); }

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 }
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 Lassho.edu.vn You can comment, see more related articles below and hope to help you with interesting information.
Related Search: