Posts

Showing posts with the label LocalDate

How to calculate days between two dates in Java 8?

In this section, we will show you h ow to calculate days between two dates in Java. 1. LocalDate Main.java import java.time.LocalDate ; import java.time.temporal.ChronoUnit ; public class Main { public static void main ( String [] args) { LocalDate from = LocalDate . now (); LocalDate to1 = from .plusDays( 10 ); long result1 = ChronoUnit . DAYS .between( from , to1 ); System . out .println( result1 ); // 10 LocalDate to2 = from .minusDays( 10 ); long result2 = ChronoUnit . DAYS .between( from , to2 ); System . out .println( result2 ); // -10 } } Console Output: 10 -10 2. LocalDateTime Main.java import java.time.LocalDateTime ; import java.time.temporal.ChronoUnit ; public class Main { public static void main ( String [] args) { LocalDateTime from = LocalDateTime . now (); LocalDateTime to1 = from .plusDays( 10 ); long result1 = ChronoUnit . DAYS .between( from , to1 ); System . ou