How to calculate the number of days between two dates in Java

3 Answers

0 votes
import java.time.LocalDate; 
import java.time.temporal.ChronoUnit;

public class MyClass {
    public static void main(String args[]) {
        LocalDate start = LocalDate.parse("2021-03-01");
        LocalDate end = LocalDate.parse("2021-03-19");

        long diff = ChronoUnit.DAYS.between(start, end);
        
        System.out.println(diff);  
    }
}
    
    
    
    
/*
run:
    
18
    
*/

 



answered Mar 19, 2021 by avibootz
0 votes
import java.time.LocalDate; 
import java.time.temporal.ChronoUnit;

public class MyClass {
    public static void main(String args[]) {
        LocalDate start = LocalDate.parse("2021-03-05");
        LocalDate now = LocalDate.now();

        long diff = ChronoUnit.DAYS.between(start, now);
        
        System.out.println(diff);  
    }
}
    
    
    
    
/*
run:
    
14
    
*/

 



answered Mar 19, 2021 by avibootz
0 votes
import java.time.LocalDate; 
import java.time.Period;
 
public class MyClass {
    public static void main(String args[]) {
        LocalDate start = LocalDate.of(2021, 3, 1);
        LocalDate end = LocalDate.of(2021, 3, 19);
 
        Period period = Period.between(start, end);
        
        int days = period.getDays();
        
        System.out.println(days);
    }
}
     
     
     
     
/*
run:
     
18
     
*/

 



answered Nov 8, 2023 by avibootz
...