forked from kidgrow-microservices-platform

luliqiang
2020-12-31 6fb14149d62199cfcc0448c82eb2f51f9c5181de
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
package com.kidgrow.common.utils;
 
import org.apache.commons.lang3.StringUtils;
 
import java.text.SimpleDateFormat;
import java.util.*;
 
/**
 * 石家庄喜高科技有限责任公司 版权所有 © Copyright 2020<br>
 *
 * @Description: <br>
 * @Project: <br>
 * @CreateDate: Created in 2020/2/28 11:07 <br>
 * @Author: <a href="4345453@kidgrow.com">liuke</a>
 */
public class DateCalUtil {
    /**
     * 将字符串转换成具体的时间(Calendar)
     * @return Calendar 时间
     * @param str  为要格式化字符串
     */
    public static Calendar getCalendarByStr(String str) {
        if (str == null) return null;
        Date date = getDateByStr(str);
        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.setTime(date);
        return cal;
    }
 
    /**
     * 将Calendar类型转化为字符串类型
     * @param calendar 要转化的Calendar类型变量
     * @param hasTime true :yyyy-MM-dd HH:mm:ss;false:yyyy-MM-dd
     * @return
     */
    public static String getCalendarStr(Calendar calendar ,boolean hasTime ) {
        if (calendar == null) return "";
        return getDateStr(calendar.getTime() ,hasTime );
 
    }
 
    /**
     * 将具体的时间转换成字符串
     * @return 转换后的字符串格式为yyyy-MM-dd HH:mm:ss 或 yyyy-MM-dd
     * @param aDate 为要格式化的时间
     * @param hasTime :返回的字符串是否包括时间 true:包括时间 false:只有日期
     */
    public static String getDateStr(Date aDate,boolean hasTime ) {
        if (aDate == null) return "";
        String dateStr = "";
        if(hasTime)
            dateStr = getDateStrByDefineStyle(aDate,"yyyy-MM-dd HH:mm:ss");
        else
            dateStr = getDateStrByDefineStyle(aDate,"yyyy-MM-dd");
        return dateStr;
    }
 
    /**
     * 返回由指定字符串转换成的时间,字符串格式类似"2006-2-12 14:22:30" 或"2006-2-12"
     * @return 转换后的时间
     * @param aStr 为要转换成时间的字符串
     */
    public static Date getDateByStr(String aStr) {
        SimpleDateFormat df =null;
        if (aStr == null) return null;
        if(aStr.indexOf(":")>0 )//如果有时间 
            df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        else //如果没有时间
            df = new SimpleDateFormat("yyyy-MM-dd");
        try
        {
            Date result = df.parse(aStr);
            return result;
        }
        catch (Exception e)
        {
            return null;
        }
    }
 
    /**
     * 将具体的时间根据要求的格式,转换成字符串
     * @param aDate 为要格式化的时间
     * @param style 要转化的格式
     * @return
     */
    public static String getDateStrByDefineStyle(Date aDate,String style){
        if (aDate == null) return "";
        SimpleDateFormat df= new SimpleDateFormat(style);
        String s = df.format(aDate);
        return s;
    }
 
    /**
     * 将具体的时间转换成字符串
     * @return 转换后的字符串,格式为yyyy-MM-dd HH:mm:ss
     * @param aDate 为要格式化的时间
     */
    public static String getDateStr(Date aDate) {
        if (aDate == null) return "";
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String s = df.format(aDate);
        return s;
//        return getDateStr(aDate,true);
    }
 
    /**
     * 获取昨日日期字符串
     * @return 昨日日期字符串,格式为yyyy-MM-dd
     */
    public static String getYesterdayStr() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE,   -1);
        return getCalendarStr(cal, false);
    }
 
    /**
     * 获取一周前日期字符串
     * @return 一周前日期字符串,格式为yyyy-MM-dd
     */
    public static String getWeekAgoStr() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE,   -7);
        return getCalendarStr(cal, false);
    }
 
    /**
     * 获取某日期半年后日期字符串
     * @return 半年后日期字符串,格式为yyyy-MM-dd HH:mm:ss
     */
    public static String getHalfYearStr(Date aDate) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(aDate);
        cal.add(Calendar.MONTH, 6);
        return getCalendarStr(cal, true);
    }
 
    /**
     * 获取本周第一天日期字符串
     * @return 本周第一天日期字符串,格式为yyyy-MM-dd
     */
    public static String getWeekFirstStr() {
        Calendar cal = Calendar.getInstance();
        int day_of_week = cal.get(Calendar.DAY_OF_WEEK) - 2;
        cal.add(Calendar.DATE, -day_of_week);
        return getCalendarStr(cal, false);
    }
    /**
     * 获取本周最后一天日期字符串
     * @return 本周最后一天日期字符串,格式为yyyy-MM-dd
     */
    public static String getWeekLastStr() {
        Calendar cal = Calendar.getInstance();
        int day_of_week = cal.get(Calendar.DAY_OF_WEEK) - 2;
        cal.add(Calendar.DATE, -day_of_week);
        cal.add(Calendar.DATE, 6);
        return getCalendarStr(cal, false);
    }
    /**
     * 获取本月第一天日期字符串
     * @return 本月第一天日期字符串,格式为yyyy-MM-dd
     */
    public static String getMonthFirstStr() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
        return getCalendarStr(cal, false);
    }
    /**
     * 获取本月最后一天日期字符串
     * @return 本月最后一天日期字符串,格式为yyyy-MM-dd
     */
    public static String getMonthLastStr() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        return getCalendarStr(cal, false);
    }
    /**
     * 获取本年第一天日期字符串
     * @return 本年第一天日期字符串,格式为yyyy-MM-dd
     */
    public static String getYearFirstStr() {
        Calendar cal = Calendar.getInstance();
        int currentYear = cal.get(Calendar.YEAR);
        cal.clear();
        cal.set(Calendar.YEAR, currentYear);
        return getCalendarStr(cal, false);
    }
    /**
     * 获取本年最后一天日期字符串
     * @return 本年最后一天日期字符串,格式为yyyy-MM-dd
     */
    public static String getYearLastStr() {
        Calendar cal = Calendar.getInstance();
        int currentYear = cal.get(Calendar.YEAR);
        cal.clear();
        cal.set(Calendar.YEAR, currentYear);
        cal.roll(Calendar.DAY_OF_YEAR, -1);
        return getCalendarStr(cal, false);
    }
 
    /**
     * 获取本月第一天日期时间字符串
     * @author Han Guohong
     * @return
     */
    public static String getNowMonthFirstStr() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为上月第一天
        return getCalendarStr(cal, false)+" 00:00:00";
    }
 
    /**
     * 获取本月最后一天日期时间字符串
     * @author Han Guohong
     * @return
     */
    public static String getNowMonthLastStr() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        return getCalendarStr(cal, false)+" 23:59:59";
    }
 
    /**
     * 获取上月第一天日期字符串
     * @return 上月第一天日期字符串,格式为yyyy-MM-dd
     */
    public static String getPreMonthFirstStr() {
        Calendar cal = Calendar.getInstance();
        int currentMonth = cal.get(Calendar.MONTH);
        cal.set(Calendar.MONTH, currentMonth-1);
        cal.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为上月第一天
        return getCalendarStr(cal, false);
    }
    /**
     * 获取上月最后一天日期字符串
     * @return 上月最后一天日期字符串,格式为yyyy-MM-dd
     */
    public static String getPreMonthLastStr() {
        Calendar cal = Calendar.getInstance();
        int currentMonth = cal.get(Calendar.MONTH);
        cal.set(Calendar.MONTH, currentMonth-1);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        return getCalendarStr(cal, false);
    }
 
    /**
     * 根据用户生日计算年龄
     */
    public static int getAgeByBirthday(Date birthday) {
        Calendar cal = Calendar.getInstance();
        if (cal.before(birthday)) {
            throw new IllegalArgumentException(
                    "The birthDay is before Now.It's unbelievable!");
        }
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH) + 1;
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
        cal.setTime(birthday);
        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH) + 1;
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
        int age = yearNow - yearBirth;
        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                // monthNow==monthBirth
                if (dayOfMonthNow < dayOfMonthBirth) {
                    age--;
                }
            } else {
                // monthNow>monthBirth
                age--;
            }
        }
        return age;
    }
 
    //获得全球唯一性的id
    public static String getId(){
        String id= UUID.randomUUID().toString();
        id=id.replace("-", "");//替换掉中间的那个斜杠
        return id;
    }
 
    /**
     * 计算两个时间差值(分钟)
     */
    public static int dateDiffMin(Date date1,Date date2) {
        long diff;
        long nd = 1000*24*60*60;//一天的毫秒数
        long nh = 1000*60*60;//一小时的毫秒数
        long nm = 1000*60;//一分钟的毫秒数
        //获得两个时间的毫秒时间差异
        diff = date1.getTime() - date2.getTime();
        long min = diff%nd%nh/nm;//计算差多少分钟
        return new Long(min).intValue();
    }
 
    /**
     * 获取指定日期前几天或者后几天的日期
     * @author Han Guohong
     * @param date
     * @param days
     * @return
     */
    public static String getBeforeOrAfterDaysDate(Date date,int days) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE,   days);
        return getCalendarStr(cal, false);
    }
 
    /**
     * 获取指定时间前几分钟或者后几分钟的时间
     * @param date
     * @param minute
     * @return
     */
    public static String getBeforeOrAfterMinuteTime(Date date,int minute) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MINUTE,   minute);
        return getCalendarStr(cal, false);
    }
 
    /**
     * 获取指定日期中间的所有日期
     */
    public static List<Date> findDates(Date dBegin, Date dEnd){
        List lDate = new ArrayList();
        lDate.add(dBegin);
        Calendar calBegin = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calBegin.setTime(dBegin);
        Calendar calEnd = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calEnd.setTime(dEnd);
        // 测试此日期是否在指定日期之后
        while (dEnd.after(calBegin.getTime())){
            // 根据日历的规则,为给定的日历字段添加或减去指定的时间量
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            lDate.add(calBegin.getTime());
        }
        return lDate;
    }
 
    public static String removeLast(String dateStr){
        if(StringUtils.isNotBlank(dateStr.trim())){
            return dateStr.substring(0, 19);
        }
        return null;
    }
 
    /**
     * 获取某天的开始时间
     * @author Han Guohong
     * @param date
     * @return
     */
    public static Date getDayBegin(Date date){
        return getDateByStr(getDateStr(date, false)+" 00:00:00");
    }
 
    /**
     * 获取某天的结束时间
     * @author Han Guohong
     * @param date
     * @return
     */
    public static Date getDayEnd(Date date){
        String dateString = getBeforeOrAfterDaysDate(date, +1);
        date = getDateByStr(dateString);
        return getDateByStr(getDateStr(date, false)+" 00:00:00");
    }
 
    /*public static void main(String [] args){
        Date now = new Date();
        Date end = getDateByStr(getBeforeOrAfterDaysDate(now, -5));
        Date begin = getDateByStr(getBeforeOrAfterDaysDate(now, -10));
        List<Date> list = findDates(begin, end);
        for (Date date : list) {
            System.out.println(getDateStr(date, true));
        }
        System.out.println(getDayEnd(now));
    }*/
}