admin
2020-08-12 149de6341fd8892cd19ca04885e7f75727e98c8d
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
package com.lcjian.library.util.common;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import android.annotation.SuppressLint;
import android.util.Log;
 
/**
 * 日期工具类
 * 
 * @author LCJIAN
 */
public class DateUtils {
 
    private static final String TAG = "DateUtils";
 
    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
 
    public static final String YYYY_MM_DD_HH_MM_SS_2 = "yyMMddHHmmss";
    /** 年-月-日,默认日期格式 */
    public static final String YYYY_MM_DD = "yyyy-MM-dd";
 
    public static final String YYYY_MM_DD_2 = "yyyyMMdd";
 
    public static final String YY_MM_DD = "yy-MM-dd";
 
    public static final String HH_MM_SS = "HH:mm:ss";
 
    public static final long DAY = 24 * 60 * 60 * 1000L;
 
    private static final Map<String, SimpleDateFormat> DFS = new HashMap<String, SimpleDateFormat>();
 
    public static SimpleDateFormat getFormat(String pattern) {
        SimpleDateFormat format = DFS.get(pattern);
        if (format == null) {
            format = new SimpleDateFormat(pattern, Locale.CHINA);
            DFS.put(pattern, format);
        }
        return format;
    }
 
    /** protected构造方法 */
    protected DateUtils() {
    }
 
    /**
     * 使用默认格式将日期转换成字符串
     * 
     * @param date
     *            需要转换的日期
     * @return 转换后的字符串,如果发生异常,返回空字符串
     */
    public static String convertDateToStr(Date date) {
        if (date == null) {
            return "";
        } else {
            return convertDateToStr(date, YYYY_MM_DD);
        }
    }
 
    /**
     * 使用自定义格式,将日期转换成字符串
     * 
     * @param date
     *            需要转换的日期
     * @param pattern
     *            自定义格式
     * @return 转换后的字符串,如果发生异常,返回空字符串
     */
    public static String convertDateToStr(Date date, String pattern) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat dateFormat = getFormat(pattern);
        return dateFormat.format(date);
    }
 
    /**
     * 使用默认格式,将字符串转换成日期
     * 
     * @param str
     *            需要转换的字符串
     * @return 转换后的日期,如果发生异常,返回null
     */
    public static Date convertStrToDate(String str) {
        if (str == null || str.equals("")) {
            return null;
        } else {
            return convertStrToDate(str, YYYY_MM_DD);
        }
    }
 
    /**
     * 根据自定义pattern将字符串日期转换成Date类型
     * 
     * @param str
     *            需要转换的字符串
     * @param pattern
     *            自定义格式
     * @return 转换后的日期,如果发生异常,返回null
     */
    public static Date convertStrToDate(String str, String pattern) {
        if (str == null || str.equals("")) {
            return null;
        }
        SimpleDateFormat dateFormat = getFormat(pattern);
        try {
            return dateFormat.parse(str);
        } catch (ParseException ex) {
            Log.i(TAG, "convertStrToDate: " + ex.getMessage());
            return null;
        }
    }
 
    /**
     * 将patternSrc格式的str转换为patterDst的字符串
     * 
     * @param str
     *            需要转换的字符串
     * @param patternSrc
     *            原格式
     * @param patterDst
     *            目标格式
     * @return
     */
    public static String formatDateStr(String str, String patternSrc,
            String patterDst) {
        return convertDateToStr(convertStrToDate(str, patternSrc), patterDst);
    }
 
    /**
     * 判断原日期是否在目标日期之前
     * 
     * @param src
     * @param dst
     * @return
     */
    public static boolean isBefore(Date src, Date dst) {
        return src.before(dst);
    }
 
    /**
     * 判断原日期是否在目标日期之后
     * 
     * @param src
     * @param dst
     * @return
     */
    public static boolean isAfter(Date src, Date dst) {
        return src.after(dst);
    }
 
    /**
     * 返回两个日期间的差异天数
     * 
     * @param date1
     *            参照日期
     * @param date2
     *            比较日期
     * @return 参照日期与比较日期之间的天数差异,正数表示参照日期在比较日期之后,0表示两个日期同天,负数表示参照日期在比较日期之前
     */
    public static int dayDiff(Date date1, Date date2) {
        Date fromDate = convertStrToDate(convertDateToStr(date1));
        Date toDate = convertStrToDate(convertDateToStr(date2));
        return (int) ((fromDate.getTime() - toDate.getTime()) / (DAY));
    }
 
    /*
     * 获取第二天凌晨时间
     */
    @SuppressLint("SimpleDateFormat")
    public static Long getSecondDay(Long time) {
        Date d = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String s = format.format(d);
        try {
            d = format.parse(s);
            time = d.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
 
        return time + 86400000;
    }
 
    public static boolean isSameDay(Date date1, Date date2) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINESE);
        String sp_time = sf.format(date1);
        String current_time = sf.format(date2);
 
        if (sp_time.equals(current_time)) {
            return true;
        } else {
            return false;
        }
    }
 
    /**
     * 判断两日期是否相同(毫秒)
     * 
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isEqual(Date date1, Date date2) {
        return date1.compareTo(date2) == 0;
    }
 
    @SuppressLint("SimpleDateFormat")
    public static String getRelativeTimeStr(Date date) {
        long l = now().getTime() - date.getTime();
        long oneDay = 1000 * 60 * 60 * 24;
        String str = "";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        str = format.format(new Date(date.getTime() + oneDay));
        long secondDay = getTime(str) * 1000;
        long timeLag = secondDay - date.getTime();
        if (l < 1000 * 60) {
            return "刚刚";
        } else if (l < 1000 * 60 * 60) {
            return (l / (1000 * 60) + 1) + "分钟前";
        } else if (l < oneDay) {
            return (l / (1000 * 60 * 60)) + "小时前";
        } else if (l < oneDay * 2) {
            if (now().getTime() < secondDay + oneDay) {
                return "昨天";
            } else {
                return "前天";
            }
        } else if (l < oneDay * 3) {
            if (now().getTime() < secondDay + oneDay * 2) {
                return "前天";
            } else {
                return "3天前";
            }
        } else if (l < (oneDay * 7)) {
            return "3天前";
        } else if (l < (oneDay * 30)) {
            return "一个星期以前";
        } else if (l < (oneDay * 183)) {
            return "一个月以前";
        } else if (l < (oneDay * 365)) {
            return "半年前";
        } else {
            return format.format(new Date(date.getTime()));
        }
        // {
        // return convertDateToStr(date);
        // }
    }
 
    /**
     * 字符串转化为时间戳
     * 
     * @param user_time
     * @return
     */
    public static long getTime(String user_time) {
        long re_time = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date d;
        try {
            d = sdf.parse(user_time);
            long l = d.getTime();
            String str = String.valueOf(l);
            re_time = Long.parseLong(str.substring(0, 10));
 
        } catch (Exception e) {
            e.printStackTrace();
        }
        return re_time;
    }
 
    /**
     * 获得当前时间的Date对象
     * 
     * @return
     */
    public static Date now() {
        return new Date();
    }
 
    /**
     * 获取星期几
     */
    public static String getWeekDay(Date date) {
        // String[] weeks = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        // Calendar cal = Calendar.getInstance();
        // cal.setTime(date);
        // int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
        // if (week_index < 0) {
        // week_index = 0;
        // }
        // return weeks[week_index];
        SimpleDateFormat sdf = getFormat("EEEE");
        String week = sdf.format(date);
        return week;
    }
 
    /**
     * 返回当月第一天的日期
     */
    public static Date firstDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE, 1);
        return calendar.getTime();
    }
 
    /**
     * 返回当月最后一天的日期
     */
    public static Date lastDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
        return calendar.getTime();
    }
 
    /**
     * 增加月份
     */
    public static Date addMonths(Date src, int num) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(src);
        calendar.add(Calendar.MONTH, num);
        return calendar.getTime();
    }
 
    /**
     * 增加天数
     */
    public static Date addDays(Date src, int num) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(src);
        calendar.add(Calendar.DATE, num);
        return calendar.getTime();
    }
 
    /**
     * @param year
     *            年
     * @param month
     *            月(1-12)
     * @param day
     *            日(1-31)
     * @return 输入的年、月、日是否是有效日期
     */
    public static boolean isValid(int year, int month, int day) {
        if (month > 0 && month < 13 && day > 0 && day < 32) {
            // month of calendar is 0-based
            int mon = month - 1;
            Calendar calendar = new GregorianCalendar(year, mon, day);
            if (calendar.get(Calendar.YEAR) == year
                    && calendar.get(Calendar.MONTH) == mon
                    && calendar.get(Calendar.DAY_OF_MONTH) == day) {
                return true;
            }
        }
        return false;
    }
 
    /**
     * 时间戳转化定位
     * 
     * @param date
     * @return
     */
    public static String getTimeToString(Long date) {
        String str = "";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        str = format.format(new Date(date));
        return str;
    }
}