Home > Java > javaTutorial > body text

How to use Java to determine the order of dates?

PHPz
Release: 2023-04-23 18:43:08
forward
1815 people have browsed it

Use Java to determine whether a date is earlier or later than another date

Another common operation in work is how to determine whether a given date is greater than or less than a certain day sky? In Java 8, the LocalDate class has two methods isBefore() and isAfter() for comparing dates. When calling the isBefore() method, it returns true if the given date is less than the current date.

package com.shxt.demo02;  
import java.time.LocalDate;  
import java.time.temporal.ChronoUnit;  
public class Demo11 {      
public static void main(String[] args) {          
LocalDate today = LocalDate.now();          
LocalDate tomorrow = LocalDate.of(2018,2,6);          
if(tomorrow.isAfter(today)){              
System.out.println("之后的日期:"+tomorrow);          
}          
LocalDate yesterday = today.minus(1, ChronoUnit.DAYS);          
if(yesterday.isBefore(today)){              
System.out.println("之前的日期:"+yesterday);          
}      
}  
}
Copy after login

The above is the detailed content of How to use Java to determine the order of dates?. 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