Home > Java > javaTutorial > 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

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

王林
Release: 2023-05-09 19:01:33
forward
1799 people have browsed it

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

Output result

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

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!

Related labels:
source:yisu.com
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