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

How to calculate date one week later using Java 8?

WBOY
Release: 2023-04-21 23:01:07
forward
1102 people have browsed it

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

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!

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