java date difference calculate date are much, there are ways a problem with my brother, this is the change in the basis of

import lombok.Builder;
import lombok.Data;

import java.util.Calendar;
import java.util.Date;

@Data
@Builder
public class DayCompare {
private int year;
private int month;
private int day;

/ **
* The date calculate how much difference between the two dates differ, there are ways a problem with my brother, this is the change in basis
* For example: a difference of 6 2011-02-02 to 2017-03-02 years, 1 month, 0 days
* @param fromDate
* @param toDate
* @return
* /
public static DayCompare dayComparePrecise (a Date fromDate, a Date toDate) {
Calendar Calendar.getInstance C1 = ();
Calendar Calendar.getInstance C2 = () ;
c1.setTime (fromDate);
c2.setTime (toDate);

int fromYear = c1.get(Calendar.YEAR);
int toYear = c2.get(Calendar.YEAR);
int year = toYear - fromYear;

// Map<String,String> map=new HashMap<>();
int day=c2.get(Calendar.DAY_OF_MONTH)-c1.get(Calendar.DAY_OF_MONTH);
int month=c2.get(Calendar.MONTH)-c1.get(Calendar.MONTH);
if(month<0){
month=12-c1.get(Calendar.MONTH)+c2.get(Calendar.MONTH);
year=year-1;
}
if(day<0){
day=c1.getActualMaximum(Calendar.DAY_OF_MONTH)-c1.get(Calendar.DAY_OF_MONTH)+c2.get(Calendar.DAY_OF_MONTH);
month=month-1;
}

return DayCompare.builder().day(day).month(month).year(year).build();
}

}

Guess you like

Origin www.cnblogs.com/zxg-blog/p/12188017.html