Home  >  Article  >  Java  >  How to calculate date one year ago or one year later in Java 8?

How to calculate date one year ago or one year later in Java 8?

WBOY
WBOYforward
2023-04-26 09:22:072209browse

Java 8 calculates the date one year ago or one year later

Use the minus() method to calculate the date one year ago

package com.shxt.demo02;  
import java.time.LocalDate;  
import java.time.temporal.ChronoUnit;  
public class Demo09 {      
public static void main(String[] args) {          
LocalDate today = LocalDate.now();          
LocalDate previousYear = today.minus(1, ChronoUnit.YEARS);          
System.out.println("一年前的日期 : " + previousYear);          
LocalDate nextYear = today.plus(1, ChronoUnit.YEARS);          
System.out.println("一年后的日期:"+nextYear);      
}  
}

The above is the detailed content of How to calculate date one year ago or one year later in Java 8?. 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