Blame view

src/main/java/com/huaheng/common/utils/DateUtils.java 4.39 KB
tangying authored
1
2
3
4
package com.huaheng.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
周鸿 authored
5
import java.util.Calendar;
tangying authored
6
import java.util.Date;
mahuandong authored
7
tangying authored
8
9
10
11
12
13
import org.apache.commons.lang3.time.DateFormatUtils;

/**
 * 时间工具类
 * @author huaheng
 */
周鸿 authored
14
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
tangying authored
15
16
17
18
19
20
21
22
    public static String YYYY = "yyyy";

    public static String YYYY_MM = "yyyy-MM";

    public static String YYYY_MM_DD = "yyyy-MM-dd";

    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
23
24
    public static String HH_MM_SS = "HH:mm:ss";
tangying authored
25
26
    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
周鸿 authored
27
28
    private static String[] parsePatterns = {"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss",
        "yyyy/MM/dd HH:mm", "yyyy/MM", "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
mahuandong authored
29
tangying authored
30
31
32
33
    /**
     * 获取当前Date型日期
     * @return Date() 当前日期
     */
周鸿 authored
34
    public static Date getNowDate() {
tangying authored
35
36
37
38
39
40
41
        return new Date();
    }

    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     * @return String
     */
周鸿 authored
42
    public static String getDate() {
tangying authored
43
44
45
        return dateTimeNow(YYYY_MM_DD);
    }
46
47
48
49
50
51
52
53
    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     * @return String
     */
    public static String getTimes() {
        return dateTimeNow(HH_MM_SS);
    }
周鸿 authored
54
    public static final String getTime() {
tangying authored
55
56
57
        return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
    }
周鸿 authored
58
    public static final String dateTimeNow() {
tangying authored
59
60
61
        return dateTimeNow(YYYYMMDDHHMMSS);
    }
周鸿 authored
62
    public static final String dateTimeNow(final String format) {
tangying authored
63
64
65
        return parseDateToStr(format, new Date());
    }
周鸿 authored
66
    public static final String dateTime(final Date date) {
tangying authored
67
68
69
        return parseDateToStr(YYYY_MM_DD, date);
    }
周鸿 authored
70
    public static final String parseDateToStr(final String format, final Date date) {
tangying authored
71
72
73
        return new SimpleDateFormat(format).format(date);
    }
周鸿 authored
74
75
    public static final Date dateTime(final String format, final String ts) {
        try {
tangying authored
76
            return new SimpleDateFormat(format).parse(ts);
周鸿 authored
77
        } catch (ParseException e) {
tangying authored
78
79
80
81
82
83
84
            throw new RuntimeException(e);
        }
    }

    /**
     * 日期路径 即年// 2018/08/08
     */
周鸿 authored
85
    public static final String datePath() {
tangying authored
86
87
88
89
90
91
92
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

    /**
     * 日期路径 即年// 20180808
     */
周鸿 authored
93
    public static final String dateTime() {
tangying authored
94
95
96
97
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyyMMdd");
    }
mahuandong authored
98
99
100
    /**
     * 日期型字符串转化为日期 格式
     */
周鸿 authored
101
102
    public static Date parseDate(Object str) {
        if (str == null) {
mahuandong authored
103
104
            return null;
        }
周鸿 authored
105
        try {
mahuandong authored
106
            return parseDate(str.toString(), parsePatterns);
周鸿 authored
107
        } catch (ParseException e) {
mahuandong authored
108
109
110
111
112
113
            return null;
        }
    }

    /**
     * 计算两个时间差
周鸿 authored
114
115
     * @param  date1
     * @param  date2
mahuandong authored
116
117
118
119
120
     * @return
     */
    public static long difference(Date date1, Date date2) {
        long from3 = date1.getTime();
        long to3 = date2.getTime();
周鸿 authored
121
        int second = (int)((to3 - from3) / 1000);
mahuandong authored
122
123
124
        return Math.abs(second);
    }
周鸿 authored
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
    public static String getNowPreWeek(String pattern) {
        Date dNow = new Date(); // 当前时间
        Date dBefore = new Date();
        Calendar calendar = Calendar.getInstance(); // 得到日历
        calendar.setTime(dNow);// 把当前时间赋给日历
        calendar.add(Calendar.DAY_OF_MONTH, -7); // 设置为前一天
        dBefore = calendar.getTime(); // 得到前一天的时间

        SimpleDateFormat sdf = new SimpleDateFormat(pattern); // 设置时间格式
        String defaultStartDate = sdf.format(dBefore);
        return defaultStartDate;
    }

    public static String getNowPreDays(String pattern, int days) {
        Date dNow = new Date(); // 当前时间
        Date dBefore = new Date();
        Calendar calendar = Calendar.getInstance(); // 得到日历
        calendar.setTime(dNow);// 把当前时间赋给日历
        calendar.add(Calendar.DAY_OF_MONTH, -days); // 设置为前一天
        dBefore = calendar.getTime(); // 得到前一天的时间

        SimpleDateFormat sdf = new SimpleDateFormat(pattern); // 设置时间格式
        String defaultStartDate = sdf.format(dBefore);
        return defaultStartDate;
    }
tangying authored
151
}