P15GEN2\59518
2025-10-10 9f6890646993d16260d4201d613c092132856127
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package foundation.util;
 
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
 
public class DateTimeUtil {
    
    private static String DATEFORMAT_YYYYMMDD = "yyyy-MM-dd";
    private static String TIMEFORMAT = "yyyy-MM-dd HH:mm:ss";
    
    // 获取指定秒数后的日期
    public static String addSecond(String dateStr, int seconds) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat(TIMEFORMAT);
        Calendar calendar = Calendar.getInstance();
        
        Date date = sdf.parse(dateStr);
        
        calendar.setTime(date);
        
        calendar.add(Calendar.SECOND, seconds);
        
        return sdf.format(calendar.getTime());
    }
        
    // 获取指定天数后的日期
    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 int getYearInt(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(1);
        return (year);
    }
    
    // 获取指定日期的季节
    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 Integer getQuarter(Date date)  {
        String monthStr = getMonth(date, false);
        Integer month = Integer.valueOf(monthStr);
        if (month == null) {
            return null;
        }
        return (((month -1) / 3) + 1);
    }
 
    public static int getMonth(Date date) throws Exception {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int month = calendar.get(2) + 1;
        return (month);
    }
    
    // 获取指定日期的月份
    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) {
        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 int getIntervalDays(Date startDate, Date endDate) {
        if (startDate == null) {
            startDate = new Date();
        }
        if (endDate == null) {
            endDate = new Date();
        }
        
        return (int) ((endDate.getTime() - startDate.getTime()) / (1000*3600*24));
    }
    
    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();
    }
 
    public static Date getQuarterEndDate(Date now) throws Exception {
        if (now == null) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
 
        calendar.set(getYearInt(now), (getQuarter(now) * 3 - 1) , 1);
 
        int actualMaximum = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        calendar.set(Calendar.DAY_OF_MONTH, actualMaximum);
        return calendar.getTime();
    }
    
    public static Date getQuarterStartDate(Date now) {
        if (now == null) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
 
        calendar.set(getYearInt(now), ((getQuarter(now) - 1 )* 3) , 1);
 
        return calendar.getTime();
    }
    
    public static Date StringToDate(String date, String format) throws ParseException {
        if (date == null) {
            return null;
        }
 
        Date result = null;
        DateFormat dateFormat = new SimpleDateFormat(format);
 
        result = dateFormat.parse(date);
 
        return result;
    }
}