Calculating the number of days difference between two dates

 

 

 

public  class the Test { 

    public  static  void main (String [] args) { 

        the LocalDate LD = LocalDate.of (2018,. 6,. 6 ); 
        the LocalDate LD2 = LocalDate.now (); 
        Period period = Period.between (LD, LD2); 
        
        
        / ** 
         * difference is merely an example of day 6, the results are 2018,6,6 2019,12,6 0 
         * 2018,6,1 2019,12,6 results. 5 
         * / 
        System.out.println (period. getDays ()); 
        
        
        
        // calculate the number of days between two dates differ 2018,6,6 2019,12,6 example results 548 
        System.out.println (ld2.toEpochDay () - ld.toEpochDay ()); 
        
        // results a difference of 31 days
        System.out.println (getDifferenceDayCount ( "2019-08-02", "2019-09-02" ));
         // difference between the results four days 
        System.out.println (getDifferenceDayCount ( "2019-09-06", "2019 -09-02 " )); 
        
    } 

    // string rotation number of days difference between the date-based 
    public  static  int getDifferenceDayCount (string startDateStr, endDateStr string) { 

        the LocalDate startDate = LocalDate.parse (startDateStr); 
        the LocalDate endDate = LocalDate.parse (endDateStr) ;
         // take positive 
        return the Math.abs (( int ) (endDate.toEpochDay () - startDate.toEpochDay ())); 

    } 

}

 

Guess you like

Origin www.cnblogs.com/wf-zhang/p/11995847.html