Duration Between Two LocalDate in Java



Here is a Java example to calculate the duration or difference between two LocalDate classes.
import java.time.LocalDate;
import java.time.Duration;
/**
 * @author javaguides.net
 *
 */
public class DateDurationExample {
 
    public static void main(String[] args) {
         getInstances();
    }

    private static void getInstances() {
        //First LocalDate
        LocalDate today = LocalDate.now();      //1
   
        //Second LocalDate
        LocalDate sameDayNextMonth = LocalDate.now().plusMonths(1);     //2
   
        //Difference between dates
        Duration duration = Duration.between( today.atStartOfDay(),
        sameDayNextMonth.atStartOfDay() );
   
        //Verify differences
        System.out.println(duration.toDays());          
        System.out.println(duration.toHours());         
        System.out.println(duration.toMinutes());       
        System.out.println(duration.toMillis());       
    }
}
30
720
43200
2592000000

Comments