Home  >  Article  >  Java  >  How to calculate date one week later using Java 8?

How to calculate date one week later using Java 8?

WBOY
WBOYforward
2023-04-21 23:01:071017browse

How to calculate the date one week later in Java 8

This example will calculate the date one week later. The LocalDate date does not contain time information. Its plus() method is used to add days, weeks, and months. The ChronoUnit class declares these time units. Since LocalDate is also an immutable type, you must use variables to assign values ​​after returning.

package com.shxt.demo02;  
import java.time.LocalDate;  
import java.time.temporal.ChronoUnit;  
public class Demo08 {      
public static void main(String[] args) {          
LocalDate today = LocalDate.now();          
System.out.println("今天的日期为:"+today);          
LocalDate nextWeek = today.plus(1, ChronoUnit.WEEKS);          
System.out.println("一周后的日期为:"+nextWeek);      
}  
}

You can see that the new date is 7 days from today’s date, which is one week. You can add 1 month, 1 year, 1 hour, 1 minute or even a century in the same way. For more options, check out the ChronoUnit class in the Java 8 API

The above is the detailed content of How to calculate date one week later using 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