黄潞潞
2024-06-07 482f807361c9bc0dce2db949a29c755cf858548b
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package foundation.util;
 
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
 
public class DateTimeUtil {
    
    private static String DATEFORMAT_YYYYMMDD = "yyyy-MM-dd";
    // 获取指定天数后的日期
    public static String addDays(String dateStr, int days) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMAT_YYYYMMDD);
        Calendar calendar = Calendar.getInstance();
        
        Date date = sdf.parse(dateStr);
        
        calendar.setTime(date);
        
        calendar.add(Calendar.DAY_OF_MONTH, days);
        
        return sdf.format(calendar.getTime());
    }
    
    // 获取指定月份数后的日期
    public static String addMonths(String dateStr, int months) throws Exception {
            SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMAT_YYYYMMDD);
            Calendar calendar = Calendar.getInstance();
            
            Date date = sdf.parse(dateStr);
            
            calendar.setTime(date);
            
            calendar.add(Calendar.MONTH, months);
            
            return sdf.format(calendar.getTime());
    }
    
    
    // 获取指定年份数后的日期
    public static String addYears(String dateStr, int years) throws Exception {
            SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMAT_YYYYMMDD);
            Calendar calendar = Calendar.getInstance();
            
            Date date = sdf.parse(dateStr);
            
            calendar.setTime(date);
            
            calendar.add(Calendar.YEAR, years);
            
            return sdf.format(calendar.getTime());
    }
    
    // 获取指定日期的季节
    public static String getSeason(String dateStr) throws Exception {
            SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMAT_YYYYMMDD);
            Calendar calendar = Calendar.getInstance();
            
            Date date = sdf.parse(dateStr);
            
            calendar.setTime(date);
            
            int season = calendar.get(Calendar.MONTH)/3 + 1;
            return String.valueOf(season);
    }
    
    // 获取指定日期的季节
    public static String getSeason(Date date) throws Exception {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            
            int season = calendar.get(Calendar.MONTH)/3 + 1;
            return String.valueOf(season);
    }
    
    // 获取指定日期的月份
    public static String getMonth(String dateStr, boolean withZero) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMAT_YYYYMMDD);
        Calendar calendar = Calendar.getInstance();
        
        Date date = sdf.parse(dateStr);
        
        calendar.setTime(date);
        
        int month = calendar.get(Calendar.MONTH) + 1;
        
        if(!withZero) {
            return String.valueOf(month);
        }
        
        if(month < 10) {
            return "0" + month; 
        }
        return String.valueOf(month);
    }
    
    public static String getMonth(Date date, boolean withZero) throws Exception {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
 
        int month = calendar.get(Calendar.MONTH) + 1;
        
        if(!withZero) {
            return String.valueOf(month);
        }
        
        if(month < 10) {
            return "0" + month; 
        }
        return String.valueOf(month);
    }
    
    // 获取指定日期的年份
    public static String getYear(String dateStr) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMAT_YYYYMMDD);
        Calendar calendar = Calendar.getInstance();
        
        Date date = sdf.parse(dateStr);
        
        calendar.setTime(date);
        
        int year = calendar.get(Calendar.YEAR);
        return String.valueOf(year);
    }
    
    // 获取指定日期的年份
    public static String getYear(Date date) throws Exception {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        
        int year = calendar.get(Calendar.YEAR);
        return String.valueOf(year);
    }
    
    // 获取两个日期之间相隔月份
    @SuppressWarnings("deprecation")
    public static int getIntervalMonth(Date startDate, Date endDate) throws Exception {
        if (startDate == null) {
            startDate = new Date();
        }
        if (endDate == null) {
            endDate = new Date();
        }
        return (endDate.getYear() - startDate.getYear()) * 12 + endDate.getMonth() - startDate.getMonth();
    }
 
    public static String addMonths(Date date, int months, String returnYM) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int month = calendar.get(Calendar.MONTH) + 1;
        month = month + months;
        int year = calendar.get(Calendar.YEAR);
        
        if(month > 12) {
            month = month/12;
            year = year + 1;
        }
        
        if (month < 10) {
            return year + "0" + month;
        }
 
        return year + month + "";
    }
 
    public static Date getMonthStartDate(String monthly) throws Exception {
        String date = monthly + "01";
        return Util.StringToDate(date);
    }
 
    public static Date getMonthEndDate(String monthly) throws Exception {
        String date = monthly + "01";
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(Util.StringToDate(date));
        int month = calendar.get(Calendar.MONTH);
        
        calendar.set(Calendar.MONTH, month + 1);
        calendar.set(Calendar.DATE, 1);
        calendar.add(Calendar.DATE, -1);
        
        return calendar.getTime();
    }
}