Home  >  Article  >  Java  >  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
WBOYforward
2023-04-18 11:01:023165browse

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() );      
}  
}

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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete