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 How to use Date.before(), Date.after() and Date.equals() methods in java. For more information, please follow other related articles on the PHP Chinese website!