Date.before(), Date.after() and Date.equals()
A semantically friendly way to compare two java.util.Date
<code>@Test<br>void testDateCompare2( ) throws ParseException {<br> SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");<br> Date date1 = sdf.parse("2009-12-31");<br> Date date2 = sdf.parse("2019-01-31");<br><br> System.out.println("date1 : " + sdf.format(date1));<br> System.out.println("date2 : " + sdf.format(date2));<br><br> if (date1.after(date2)) {<br> System.out.println("Date1 时间在 Date2 之后");<br> }<br><br> if (date1.before(date2)) {<br> System.out.println("Date1 时间在 Date2 之前");<br> }<br><br> if (date1.equals(date2)) {<br> System.out.println("Date1 时间与 Date2 相等");<br> }<br>}<br></code>
Output result
<code>date1 : 2009-12-31<br>date2 : 2019-01-31<br>Date1 时间在 Date2 之前</code>
The above is the detailed content of You can use the before(), after() and equals() methods of the Date class to compare and judge dates. Among them, the before() method is used to determine whether the current date is before another date, the after() method is used to determine whether the current date is after another date, and the equals() method is used to determine whether two dates are equal. For example, comparing two dates d1 and d2: ``` Date d1 = new Date(); Date d2 = new Date(Syst. For more information, please follow other related articles on the PHP Chinese website!