Home > Java > javaTutorial > 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
Release: 2023-04-26 09:22:07
forward
2249 people have browsed it

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);      
}  
}
Copy after login

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!

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