jyy
2021-06-19 756e16e090b15c7fd8648f55f5451367face6abc
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
package com.matrix.system.hive.plugin.util;
 
import com.matrix.core.tools.DateUtil;
import com.matrix.core.tools.StringUtils;
 
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
 
public class TimeUtils {
    
    /**
     * 获取 当前年、半年、季度、月、日、小时 开始结束时间
     */
    private static SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");;
    
    private static SimpleDateFormat longHourSdf = new SimpleDateFormat("yyyy-MM-dd HH");
    
    private static SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
    @SuppressWarnings("static-access")
    public TimeUtils() {
        this.shortSdf = new SimpleDateFormat("yyyy-MM-dd");
        this.longHourSdf = new SimpleDateFormat("yyyy-MM-dd HH");
        this.longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    }
    
    /**
     * 获得周的开始时间
     * 
     * @param checkrule 数字规则类型 0 从权重单位时间进行计算 1从当前时间开始往前计算
     * @return
     */
    public static Date getCurrentWeekDayStartTime(String checkrule) {
        Calendar c = Calendar.getInstance();
        try {
            int weekday = c.get(Calendar.DAY_OF_WEEK) - 2;
            c.add(Calendar.DATE, -weekday);
            if (checkrule.equals("1")) {
                c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00"));
                return c.getTime();
            }
            
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return c.getTime();
    }
    
    /**
     * 获得本天的开始时间,即2012-01-01 00:00:00
     * 
     * @return
     */
    public static Date getCurrentDayStartTime(String checkrule) {
        Date now = new Date();
        try {
            if (checkrule.equals("1")) {
                now = shortSdf.parse(shortSdf.format(now));
                return now;
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }
    
    /**
     * 获得本小时的开始时间,即2012-01-01 23:59:59
     * 
     * @return
     */
    public static Date getCurrentHourStartTime(String checkrule) {
        Date now = new Date();
        try {
            if (checkrule.equals("1")) {
                now = longHourSdf.parse(longHourSdf.format(now));
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }
    
    /**
     * 获得本月的开始时间,即2012-01-01 00:00:00
     * 
     * @return
     */
    public static Date getCurrentMonthStartTime(String checkrule) {
        Calendar c = Calendar.getInstance();
        Date now = null;
        try {
            
            if (checkrule.equals("1")) {
                c.set(Calendar.DATE, 1);
                now = shortSdf.parse(shortSdf.format(c.getTime()));
                return now;
            }
            
            now = c.getTime();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }
    
    /**
     * 当前年的开始时间,即2012-01-01 00:00:00
     * 
     * @return
     */
    public static Date getCurrentYearStartTime(String checkrule) {
        Calendar c = Calendar.getInstance();
        Date now = null;
        try {
            if (checkrule.equals("1")) {
                
                c.set(Calendar.MONTH, 0);
                c.set(Calendar.DATE, 1);
                now = shortSdf.parse(shortSdf.format(c.getTime()));
                return now;
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return c.getTime();
    }
    
    /**
     * 当前季度的开始时间,即2012-01-1 00:00:00
     * 
     * @return
     */
    public static Date getCurrentQuarterStartTime(String checkrule) {
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
        Date now = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 3)
                c.set(Calendar.MONTH, 0);
            else if (currentMonth >= 4 && currentMonth <= 6)
                c.set(Calendar.MONTH, 3);
            else if (currentMonth >= 7 && currentMonth <= 9)
                c.set(Calendar.MONTH, 6);
            else if (currentMonth >= 10 && currentMonth <= 12)
                c.set(Calendar.MONTH, 9);
            c.set(Calendar.DATE, 1);
            if (checkrule.equals("1")) {
                now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
                return now;
            }
            
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return c.getTime();
    }
    
    /**
     * 获取前/后半年的开始时间
     * 
     * @return
     */
    public static Date getHalfYearStartTime() {
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
        Date now = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 6) {
                c.set(Calendar.MONTH, 0);
            }
            else if (currentMonth >= 7 && currentMonth <= 12) {
                c.set(Calendar.MONTH, 6);
            }
            c.set(Calendar.DATE, 1);
            now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return now;
        
    }
    
    /**
     * 获取前/后半年的结束时间
     * 
     * @return
     */
    public static Date getHalfYearEndTime() {
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
        Date now = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 6) {
                c.set(Calendar.MONTH, 5);
                c.set(Calendar.DATE, 30);
            }
            else if (currentMonth >= 7 && currentMonth <= 12) {
                c.set(Calendar.MONTH, 11);
                c.set(Calendar.DATE, 31);
            }
            now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }
    
    /*--------------------------------------------------------------------------------*/
    /**
     * 获得年的结束时间
     * 
     * @param data 权重的参数
     * @param checkrule 权重计算类型
     * @return 单位权重的结束时间
     */
    public static Date getEndYearTime(String data, String checkrule) {
        // 获得当前时间
        Calendar c = Calendar.getInstance();
        int dataInt = StringUtils.stringToInt(data);
        try {
            if (checkrule.equals("0")) {
                
                c.add(Calendar.YEAR, -dataInt);
                return c.getTime();
            }
            else {
                // 获得年结束时间
                
                if (dataInt > 1) {
                    dataInt -= 1;
                    c.add(Calendar.YEAR, dataInt);
                }
                c.set(Calendar.MONTH, 11);
                c.set(Calendar.DATE, 31);
                
                c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59"));
                
                return c.getTime();
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return c.getTime();
    }
    
    /**
     * 权重单位规则 获取季度的结束时间
     * 
     * @param data 规则参数
     * @param checkrule 计算类型
     * @return 规则权限结束时间
     */
    public static Date getQuarter(String data) {
        
        // 获得当前时间
        Calendar c = Calendar.getInstance();
        try {
            
            int dataInt = StringUtils.stringToInt(data);
            
            if (dataInt == 1) {
                c.set(Calendar.MONTH, 2);
                c.set(Calendar.DATE, 31);
            }
            if (dataInt == 2) {
                c.set(Calendar.MONTH, 4);
                c.set(Calendar.DATE, 31);
            }
            if (dataInt == 3) {
                c.set(Calendar.MONTH, 7);
                c.set(Calendar.DATE, 31);
            }
            if (dataInt == 4) {
                c.set(Calendar.MONTH, 11);
                c.set(Calendar.DATE, 31);
            }
            c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59"));
        }
        catch (Exception e) {
        }
        return c.getTime();
        
    }
    
    /**
     * 权重单位为 天
     * 
     * @param data 权重参数
     * @param checkrule 类型
     * @return 结束时间
     */
    public static Date getDay(String data, String checkrule) {
        
        // 获得当前时间
        Calendar c = Calendar.getInstance();
        int endDay = StringUtils.stringToInt(data);
        try {
            
            if (checkrule.equals("1")) {
                c.add(Calendar.DATE, endDay);
                c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59"));
                return c.getTime();
            }
            else if (checkrule.equals("0")) {
                c.add(Calendar.DATE, -endDay);
                
                return c.getTime();
            }
        }
        catch (Exception e) {
        }
        
        return c.getTime();
    }
    
    /**
     * 单位权重为月 时 获得 到期时间
     * 
     * @param data 权重参数
     * @param checkrule 计算类型
     * @return 结束时间
     */
    public static Date getendTimeToMonth(String data, String checkrule) {
        
        // 获得当前时间
        Date createTime = new Date();
        Calendar c = Calendar.getInstance();
        try {
            int dataInt = StringUtils.stringToInt(data);
            int month = DateUtil.getMonth(createTime);
            int year = DateUtil.getYear(createTime);
            if (dataInt > 12 - month && dataInt < 12) {
                month += dataInt;
                year += 1;
                c.set(Calendar.YEAR, year);
                c.set(Calendar.MONTH, month - 12 - 1);
                
            }
            else if (dataInt >= 12) {
                int trad = dataInt / 12;
                int residue = dataInt % 12;
                year += trad;
                month += residue;
                if (month > 12) {
                    month -= 12;
                    year += 1;
                    c.set(Calendar.YEAR, year);
                    c.set(Calendar.MONTH, month - 1);
                }
                else {
                    c.set(Calendar.YEAR, year);
                    c.set(Calendar.MONTH, month - 1);
                }
            }
            if (checkrule.equals("0")) {
                c.set(Calendar.DATE, 30);
            }
        }
        catch (Exception e) {
            
        }
        return c.getTime();
    }
    
    /**
     * 获得小时的结束时间
     * 
     * @param data
     * @return 小时的结束时间
     */
    public static Date getEndHours(String data, String checkrule) {
        int dataInt = StringUtils.stringToInt(data);
        Calendar c = Calendar.getInstance();
        try {
            
            if (checkrule.equals("1")) {
                c.add(Calendar.HOUR, dataInt - 1);
                c.setTime(longSdf.parse(longHourSdf.format(c.getTime()) + ":59:59"));
            }
            else if (checkrule.equals("0")) {
                c.add(Calendar.HOUR, -dataInt);
            }
        }
        
        catch (Exception e) {
            e.printStackTrace();
        }
        return c.getTime();
    }
    
    /**
     * 周的结束时间
     * 
     * @param date
     * @return 结束时间
     */
    public static Date getEndWeekDayTime(String data, String checkrule) {
        Calendar c = Calendar.getInstance();
        try {
            
            int weekday = c.get(Calendar.DAY_OF_WEEK);
            int dataInt = StringUtils.stringToInt(data);
            if (checkrule.equals("0")) {
                c.add(Calendar.DATE, -(7 * dataInt));
                
            }
            else if (checkrule.equals("1")) {
                if (dataInt == 1) {
                    c.add(Calendar.DATE, 8 - weekday);
                    c.setTime(longSdf.parse(longHourSdf.format(c.getTime()) + "23:59:59"));
                }
                if (dataInt > 1) {
                    c.add(Calendar.DATE, (8 - weekday) + (dataInt - 1) * 7);
                    c.setTime(longSdf.parse(longHourSdf.format(c.getTime()) + "23:59:59"));
                }
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return c.getTime();
    }
    
    public static void main(String[] args) {
        
        String data = "3";
        String checkrule = "1";
        
        Date resultDate = TimeUtils.getEndYearTime(data, checkrule);
        String time = DateUtil.dateToString(resultDate, DateUtil.DATE_FORMAT_SS);
        System.out.println(time);
    }
}