liukangdong
2024-11-27 a856cfc04747d4d8f3605168531b253240d2e87c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package com.doumee.core.utils;
 
 
import lombok.Builder;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.DateFormatSymbols;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;
import java.util.*;
 
/**
 * 日期工具类
 * @author: jiangping
 * @date:   2021年7月21日09:27:13
 */
@Builder
@Data
public class DateCompare {
    private int year;
    private int month;
    private int day;
    private BigDecimal yearFloat;
    private BigDecimal monthFloat;
    private int yearDays  ;
    private int monthDays ;
    /**
     * 计算2个日期之间相差的  以年、月、日为单位,各自计算结果是多少
     * 比如:2011-02-02 到  2017-03-02
     *                                以年为单位相差为:6年
     *                                以月为单位相差为:73个月
     *                                以日为单位相差为:2220天
     * @param fromDate
     * @param toDate
     * @return
     */
    public static DateCompare dayCompare(Date fromDate,Date toDate,Date freeStart,Date freeEnd){
        //开始时间往后延伸,除去有效时期
        fromDate = DateUtil.addDaysToDate(fromDate,getIntersectingDays(fromDate,toDate,freeStart,freeEnd));
        if(toDate.getTime()<= fromDate.getTime()){
            return DateCompare.builder().day(0).month(0).year(0).yearFloat(new BigDecimal(0)).monthFloat(new BigDecimal(0)).build();
        }
        Calendar  from  =  Calendar.getInstance();
        from.setTime(fromDate);
        Calendar  to  =  Calendar.getInstance();
        to.setTime(toDate);
        //只要年月
        int fromYear = from.get(Calendar.YEAR);
        int fromMonth = from.get(Calendar.MONTH);
 
        int toYear = to.get(Calendar.YEAR);
        int toMonth = to.get(Calendar.MONTH);
 
        int year = toYear  -  fromYear;
        int month = toYear *  12  + toMonth  -  (fromYear  *  12  +  fromMonth);
        int day = (int) ((to.getTimeInMillis()  -  from.getTimeInMillis())  /  (24  *  3600  *  1000));
        BigDecimal yearFloat = new BigDecimal(year) ;
        BigDecimal monthFloat = new BigDecimal(month) ;
 
        int yearDays = day - DateUtil.daysBetweenDates(DateUtil.addYearToDate(fromDate,year),fromDate) ;
        if(yearDays>0){
            yearFloat = yearFloat.add(new BigDecimal(yearDays).divide(new BigDecimal(365), 2,RoundingMode.FLOOR));
        }
        int monthDays = day - DateUtil.daysBetweenDates(DateUtil.addMonthToDate(fromDate,month),fromDate) ;
        if(monthDays>0){
            monthFloat = monthFloat.add(new BigDecimal(monthDays).divide(new BigDecimal(30), 2,RoundingMode.FLOOR));
        }
        return DateCompare.builder().day(day).month(month).year(year).yearFloat(yearFloat).monthFloat(monthFloat).yearDays(yearDays).monthDays(monthDays).build();
    }
    public static DateCompare monthYearCompare(Date fromDate,Date toDate ){
        //开始时间往后延伸,除去有效时期
        Calendar  from  =  Calendar.getInstance();
        from.setTime(fromDate);
        Calendar  to  =  Calendar.getInstance();
        to.setTime(toDate);
        //只要年月
        int fromYear = from.get(Calendar.YEAR);
        int fromMonth = from.get(Calendar.MONTH);
 
        int toYear = to.get(Calendar.YEAR);
        int toMonth = to.get(Calendar.MONTH);
 
        int year = toYear  -  fromYear;
        int month = toYear *  12  + toMonth  -  (fromYear  *  12  +  fromMonth);
        int day = (int) ((to.getTimeInMillis()  -  from.getTimeInMillis())  /  (24  *  3600  *  1000));
        BigDecimal yearFloat = new BigDecimal(year) ;
        BigDecimal monthFloat = new BigDecimal(month) ;
 
        int yearDays = day - DateUtil.daysBetweenDates(DateUtil.addYearToDate(fromDate,year),fromDate) ;
        if(yearDays>0){
            yearFloat = yearFloat.add(new BigDecimal(yearDays).divide(new BigDecimal(365), 2,RoundingMode.FLOOR));
        }
        int monthDays = day - DateUtil.daysBetweenDates(DateUtil.addMonthToDate(fromDate,month),fromDate) ;
        if(monthDays>0){
            monthFloat = monthFloat.add(new BigDecimal(monthDays).divide(new BigDecimal(30), 2,RoundingMode.FLOOR));
        }
        return DateCompare.builder().day(day).month(month).year(year).yearFloat(yearFloat).monthFloat(monthFloat).yearDays(yearDays).monthDays(monthDays).build();
    }
 
    public static int getIntersectingDays(Date start1, Date end1, Date start2, Date end2) {
        Date earlierStart = DateUtil.daysBetweenDates(start1,start2)>0? start1 : start2;
        Date laterEnd =  DateUtil.daysBetweenDates(end2,end1)>0 ? end1 : end2;
 
        int days =DateUtil.daysBetweenDates(laterEnd,earlierStart );
        return days>0?days:0 ;
    }
 
}