Home > Java > javaTutorial > How to calculate the number of days and months between two dates in java

How to calculate the number of days and months between two dates in java

WBOY
Release: 2023-04-18 11:01:02
forward
3190 people have browsed it

Calculate the number of days and months between two dates

A common date operation is to calculate the number of days, weeks, or months between two dates. In Java 8, you can use the java.time.Period class to do calculations.

In the following example, we calculate the number of months between today and a future day.

package com.shxt.demo02;  
import java.time.LocalDate;  
import java.time.Period;  
public class Demo15 {      
public static void main(String[] args) {          
LocalDate today = LocalDate.now();          
LocalDate java8Release = LocalDate.of(2018, 12, 14);          
Period periodToNextJavaRelease = Period.between(today, java8Release);          
System.out.println("Months left between today and Java 8 release : "                  + periodToNextJavaRelease.getMonths() );      
}  
}
Copy after login

The above is the detailed content of How to calculate the number of days and months between two dates in java. 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