Home >Java >javaTutorial >How to use Java to determine the order of dates?

How to use Java to determine the order of dates?

PHPz
PHPzforward
2023-04-23 18:43:081827browse

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);          
}      
}  
}

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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete