/ **
* calculated two points in time difference between the number of days
* @param first time DT1
* @param DT2 second time point
* @return int, i.e. the difference between the number of days to be calculated
* /
public static int DateDiff (DT1 the LocalDateTime , the LocalDateTime DT2) {
// get the first time stamp corresponding to the number of seconds
Long T1 = dt1.toEpochSecond (ZoneOffset.ofHours (0));
// get the first time in January 1, 1970 the first few days later
Long DAY1 = T1 / (60 * 60 * 24);
// Get a second time stamp corresponding to the number of seconds
Long T2 = dt2.toEpochSecond (ZoneOffset.ofHours (0));
// gets the second time in the first few days in January 1970 after 1
Long DAY2 = T2 / (60 * 60 * 24-);
// return point two times the number of days difference between the
return (int) (day2 - day1 ) ;
}
@Test
public void testDay () {
the LocalDateTime OF1 = LocalDateTime.of (2018,. 9, 25,. 1,. 1); // 2018-9-25 01:01
LocalDateTime of2 = LocalDateTime.of(2019, 9, 25, 23, 16); //2019-9-25 23:16
System.out.println(dateDiff(of1,of2));//365
}
/ **
* Get the difference between two month time points
* The first time dt1 @param
* @param DT2 second time point
* @return int, that is, the difference between the demand for several months
* /
public static int monthDiff (the LocalDateTime dt1 , the LocalDateTime DT2) {
// get the first month time point
int month1 = dt1.getMonthValue ();
Year // get the first time point
int = YEAR1 dt1.getYear ();
// get the first time point of the month
int month2 = dt2.getMonthValue ();
Year // gets the first point in time
int = YEAR2 dt2.getYear ();
// return point two months time difference
return (year2 - year1) * 12 + (MONTH2 - MONTH1);
}
@Test
public void testMonth () {
the LocalDateTime OF1 = LocalDateTime.of (2018,. 9, 25,. 1,. 1); // 2018-9-25 01:01
LocalDateTime of2 = LocalDateTime.of(2019, 9, 25, 23, 16); //2019-9-25 23:16
System.out.println(monthDiff(of1,of2));//12
}