Home > Java > javaTutorial > What are the date comparison methods in Java8?

What are the date comparison methods in Java8?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2023-04-29 16:46:07
forward
2491 people have browsed it

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>
Copy after login

Output results

<code>date1 : 2009-12-31<br>date2 : 2019-01-31<br>Is...<br>Date1 时间在 Date2 之前</code>
Copy after login

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!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template