Java 8 date comparison methods
In Java 8, you can use the new isBefore(), isAfter(), isEqual() and compareTo() to compare LocalDate, LocalTime and LocalDateTime. The following example compares two java.time.LocalDate
<code>@Test<br>void testDateCompare4( ) throws ParseException {<br> DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyy-MM-dd");<br> LocalDate date1 = LocalDate.of(2009, 12, 31);<br> LocalDate date2 = LocalDate.of(2019, 1, 31);<br><br> System.out.println("date1 : " + sdf.format(date1));<br> System.out.println("date2 : " + sdf.format(date2));<br><br> System.out.println("Is...");<br> if (date1.isAfter(date2)) {<br> System.out.println("Date1 时间在 Date2 之后");<br> }<br><br> if (date1.isBefore(date2)) {<br> System.out.println("Date1 时间在 Date2 之前");<br> }<br><br> if (date1.isEqual(date2)) {<br> System.out.println("Date1 时间与 Date2 相等");<br> }<br>}<br></code>
Output results
<code>date1 : 2009-12-31<br>date2 : 2019-01-31<br>Is...<br>Date1 时间在 Date2 之前</code>
The above is the detailed content of What are the date comparison methods in Java8?. For more information, please follow other related articles on the PHP Chinese website!