Helius
2021-02-26 de04085526eda992155716eda98af621ad681e4e
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
package com.matrix.codeGeneration.plugin;
 
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
 
/**
 * 日期格式化,转换工具类
 * 
 * @author 姜友瑶
 * @createTime 2014.08.30
 */
public class DateUtils {
 
 
    public static final String DATE_FORMAT_SS = "yyyy-MM-dd HH:mm:ss";
 
    public static final String DATE_FORMAT_MONGO = "yyyy-MM-dd'T'HH:mm:ss.SSS";
 
    public static final String DATE_FORMAT_STS = "yyyy-MM-dd'T'HH:mm:ss";
 
    public static final String DATE_FORMAT_MM = "yyyy-MM-dd HH:mm";
 
    public static final String DATE_FORMAT_HH = "yyyy-MM-dd HH";
 
    public static final String DATE_FORMAT_DD = "yyyy-MM-dd";
 
    public static final String DATE_FORMAT_SPLITE_DD = "yyyy.MM.dd";
 
    public static final String DATE_FORMAT_NO_SPLITE_DD = "yyyyMMdd";
 
    public static final String DATE_FORMAT_MM_NO_DD = "yyyyMM";
 
    public static final String DATE_FORMAT_NO_SPLITE_MM = "yyyyMMddHHmm";
 
    public static final String DATE_FORMAT_NO_SPLITE_MM_HH = "yyyyMMddHH";
 
    public static final String YEAR = "yyyy";
 
    public static final String DATE_FORMAT_MMDD = "M月d日";
 
    public static final String DATE_FORMAT_WEEK = "星期";
 
    public static final String DATE_TIME_MORNING = "早上";
 
    public static final String DATE_TIME_AFTERNOON = "下午";
 
    public static final String DATE_TIME_NIGHT = "晚上";
 
    public static final String CENTRE_SCRIBING = "-";
 
    protected static final String EMPTY = "";
 
    protected static final String ZERO = "0";
 
    protected static final String SPLITE_CHAR = ":";
 
    protected static final String START_TIME = " 00:00:00";// 空格不能删除
 
    protected static final String END_TIME = " 23:59:59";// 空格不能删除
 
    protected static final int WEEK_DAYS = 7;
 
    public static String yyyy_MM_dd_HH_mm_ss = "yyyy-MM-dd HH:mm:ss";
 
    protected static final String[] weeks = { "一", "二", "三", "四", "五", "六", "日" };
 
    /**
     * 返回年份
     * 
     * @param date
     *            日期
     * @return 返回年份
     */
    public static int getYear(java.util.Date date) {
 
        try {
            java.util.Calendar c = java.util.Calendar.getInstance();
            c.setTime(date);
            return c.get(java.util.Calendar.YEAR);
        } catch (Exception e) {
        }
 
        return 0;
    }
 
    /**
     * 返回月份
     * 
     * @param date
     *            日期
     * @return 返回月份
     */
    public static int getMonth(java.util.Date date) {
 
        try {
            java.util.Calendar c = java.util.Calendar.getInstance();
            c.setTime(date);
            return c.get(java.util.Calendar.MONTH) + 1;
        } catch (Exception e) {
            
        }
        return 0;
    }
 
    /**
     * 日期转字符串
     * 
     * @param date
     * @param format
     * @return
     */
    public static String dateToString(Date date, String format) {
 
        if (date == null) {
            return EMPTY;
        }
        DateFormat fmt = new SimpleDateFormat(format);
        return fmt.format(date);
    }
 
    /**
     * 字符串转日期
     * 
     * @param dateStr
     * @param format
     * @return
     */
    public static Date stringToDate(String dateStr, String format) {
 
        DateFormat fmt = new SimpleDateFormat(format);
        try {
            return fmt.parse(dateStr);
        } catch (ParseException e) {
            
        }
        return null;
    }
 
 
    /**
     * 判断给定的日期是一周中的第几天,注意:按照中国的习惯,周日是第七天
     * 
     * @param date
     * @return
     */
    public static int dateToWeek(Date date) {
 
        if (date == null) {
            return 0;
        }
 
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        if (cal.get(Calendar.DAY_OF_WEEK) == 1) {
            return 7;
        } else {
            return cal.get(Calendar.DAY_OF_WEEK) - 1;
        }
    }
 
    public static String dateOfWeek(Date date) {
 
        return DATE_FORMAT_WEEK + weeks[dateToWeek(date) - 1];
    }
 
    /**
     * 指定时间的下一天
     * 
     * @param date
     * @return
     */
    public static Date nextDate(Date date) {
 
        if (date == null) {
            return date;
        }
 
        Calendar cal = Calendar.getInstance();
        try {
            cal.setTime(date);
            cal.add(Calendar.DATE, 1);
            return cal.getTime();
        } catch (Exception e) {
            
        }
 
        return null;
    }
 
    /**
     * 指定时间的前一天
     * 
     * @param date
     * @return
     */
    public static Date previousDate(Date date) {
 
        if (date == null) {
            return date;
        }
 
        Calendar cal = Calendar.getInstance();
        try {
            cal.setTime(date);
            cal.add(Calendar.DATE, -1);
            return cal.getTime();
        } catch (Exception e) {
            
        }
 
        return null;
    }
 
    /**
     * 指定时间的下N天
     * 
     * @param date
     * @return
     */
    public static Date nextNDate(Date date, int nDay) {
 
        if (date == null) {
            return date;
        }
 
        Calendar cal = Calendar.getInstance();
        try {
            cal.setTime(date);
            cal.add(Calendar.DATE, nDay);
            return cal.getTime();
        } catch (Exception e) {
            
        }
 
        return null;
    }
 
    /**
     * 指定时间的前N天
     * 
     * @param date
     * @return
     */
    public static Date previousNDate(Date date, int nDay) {
 
        if (date == null) {
            return date;
        }
 
        Calendar cal = Calendar.getInstance();
        try {
            cal.setTime(date);
            cal.add(Calendar.DATE, -nDay);
            return cal.getTime();
        } catch (Exception e) {
            
        }
 
        return null;
    }
 
    /**
     * 获取一天的起始时间
     * 
     * @param date
     * @return
     */
    public static Date getStartDate(Date date) {
 
        if (date == null) {
            return date;
        }
 
        DateFormat fmt = new SimpleDateFormat(DATE_FORMAT_DD);
        String dateStr = fmt.format(date);
        dateStr = dateStr + START_TIME;
        fmt = new SimpleDateFormat(DATE_FORMAT_SS);
        try {
            return fmt.parse(dateStr);
        } catch (ParseException e) {
            
        }
 
        return date;
    }
 
    /**
     * 获取一天的结束时间
     * 
     * @param date
     * @return
     */
    public static Date getEndDate(Date date) {
 
        if (date == null) {
            return date;
        }
 
        DateFormat fmt = new SimpleDateFormat(DATE_FORMAT_DD);
        String dateStr = fmt.format(date);
        dateStr = dateStr + END_TIME;
        fmt = new SimpleDateFormat(DATE_FORMAT_SS);
        try {
            return fmt.parse(dateStr);
        } catch (ParseException e) {
            
        }
 
        return date;
    }
 
    /**
     * currentDat是否在referenceDate日期之前
     * 
     * @param referenceDate
     * @param currentDat
     * @return
     */
    public static boolean isBeforeDate(Date referenceDate, Date currentDate) {
 
        if (currentDate == null) {
            return false;
        }
        if (referenceDate == null) {
            return true;
        }
        return currentDate.before(referenceDate);
    }
 
    /**
     * currentDat是否在referenceDate日期之后
     * 
     * @param referenceDate
     * @param currentDat
     * @return
     */
    public static boolean isAffterDate(Date referenceDate, Date currentDate) {
 
        if (currentDate == null) {
            return false;
        }
        if (referenceDate == null) {
            return true;
        }
        return currentDate.after(referenceDate);
    }
 
    /**
     * 判断currentDate是否在startDate和endDate之间,不包括startDate和endDate
     * 
     * @param startDate
     * @param endDate
     * @param currentDate
     * @return
     */
    public static boolean isDuringDate(Date startDate, Date endDate, Date currentDate) {
 
        if (currentDate == null) {
            return false;
        }
 
        if (isAffterDate(startDate, currentDate) && isBeforeDate(endDate, currentDate)) {
            return true;
        }
        return false;
    }
 
     
 
    /**
     * 获取startDate到endDate之间的星期day(中文星期)不包括startDate和endDate
     * 
     * @param startDate
     * @param endDate
     * @param day
     * @return
     */
    public static List<Date> findDayDuringDate(Date startDate, Date endDate, int day) {
 
        List<Date> listDate = new ArrayList<Date>();
        int startDay = dateToWeek(startDate);
 
        Date date = null;
        if (startDay == day) {
            date = nextNDate(startDate, WEEK_DAYS);
        } else {
            date = nextNDate(startDate, day - startDay);
        }
        while (isDuringDate(startDate, endDate, date)) {
            listDate.add(date);
            date = nextNDate(date, WEEK_DAYS);
        }
 
        return listDate;
    }
 
     
 
    /**
     * date转换成Timestamp
     * 
     * @param date
     * @param format
     * @return
     */
    public static Timestamp dateToTimestamp(Date date, String format) {
 
        if (date == null) {
            return null;
        }
 
        format = DATE_FORMAT_SS;
 
        DateFormat fmt = new SimpleDateFormat(format);
 
        return Timestamp.valueOf(fmt.format(date));
    }
 
    /**
     * 获取早中晚
     * 
     * @param time
     * @return
     */
    public static String getDateTime(int time) {
 
        // 早上
        if (time == 1) {
            return DateUtils.DATE_TIME_MORNING;
        }
        // 下午
        else if (time == 2) {
            return DateUtils.DATE_TIME_AFTERNOON;
        }
        // 晚上
        else if (time == 3) {
            return DateUtils.DATE_TIME_NIGHT;
        }
        return null;
    }
 
    /**
     * 获取早中晚的开始时间
     * 
     * @param date
     * @param time
     * @return
     */
    public static Date getMeetTimeStart(String date, int time) {
 
        // 早上
        if (time == 1) {
            return DateUtils.stringToDate(date + " 06:00", DateUtils.DATE_FORMAT_MM);
        }
        // 下午
        else if (time == 2) {
            return DateUtils.stringToDate(date + " 13:00", DateUtils.DATE_FORMAT_MM);
        }
        // 晚上
        else if (time == 3) {
            return DateUtils.stringToDate(date + " 19:00", DateUtils.DATE_FORMAT_MM);
        }
        return null;
    }
 
    /**
     * 获取早中晚的结束时间
     * 
     * @param date
     * @param time
     * @return
     */
 
    public static Date getMeetTimeEnd(String date, int time) {
        // 早上
        if (time == 1) {
            return DateUtils.stringToDate(date + " 13:00", DateUtils.DATE_FORMAT_MM);
        }
        // 下午
        else if (time == 2) {
            return DateUtils.stringToDate(date + " 19:00", DateUtils.DATE_FORMAT_MM);
        }
        // 晚上
        else if (time == 3) {
            return DateUtils.stringToDate(date + " 23:00", DateUtils.DATE_FORMAT_MM);
        }
        return null;
    }
 
    /**
     * 得到几天前的时间
     * 
     * @param d
     * @param day
     * @return
     */
    public static Timestamp getDateBefore(Date d, int day) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
        return new Timestamp(now.getTime().getTime());
    }
 
    /**
     * 得到几天后的时间
     * 
     * @param d
     * @param day
     * @return
     */
    public static Timestamp getDateAfter(Date d, int day) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
        return new Timestamp(now.getTime().getTime());
    }
 
    /**
     * 将日期类型格式化成字符串
     * 
     * @param date
     * @return 格式化后日期字符串
     * @throws ParseException
     */
    public static String dateFormatStr(Date date, String dateStyle) {
        String dateStr = null;
        if (date != null) {
            SimpleDateFormat sdf = new SimpleDateFormat(dateStyle);
            dateStr = sdf.format(date);
        }
        return dateStr;
    }
 
    /**
     * 获取时间戳
     * 
     * @return
     */
    public static String getTimeMark() {
        Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int mouth = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);
        int second = c.get(Calendar.SECOND);
        int haomiao = c.get(Calendar.MILLISECOND);
        return "" + year + mouth + day + hour + minute + second + haomiao;
    }
 
}