Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the date comparison methods in Java8

Shulou Source: shulou.com Published: 2022-06-01 03:05:22 10月02日 Update

This article will explain in detail what are the date comparison methods in Java8. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Java 8 date comparison method

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

@ Test

Void testDateCompare4 () throws ParseException {

DateTimeFormatter sdf = DateTimeFormatter.ofPattern ("yyyy-MM-dd")

LocalDate date1 = LocalDate.of (2009, 12, 31)

LocalDate date2 = LocalDate.of (2019, 1,31)

System.out.println ("date1:" + sdf.format (date1))

System.out.println ("date2:" + sdf.format (date2))

System.out.println ("Is...")

If (date1.isAfter (date2)) {

System.out.println ("Date1 time is after Date2")

}

If (date1.isBefore (date2)) {

System.out.println ("Date1 time is before Date2")

}

If (date1.isEqual (date2)) {

System.out.println ("Date1 time equals Date2")

}

}

Output result

Date1: 2009-12-31

Date2: 2019-01-31

Is...

Date1 time before Date2 on "what are the date comparison methods in Java8" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it out for more people to see.

Tags: Time method date article more good practical two content article knowledge example result reference help related output Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Docker MySQL Redmi Huawei