sunshine
2024-11-05 53bd8a8d8184b3f19695b756ee78f343cdb9b9b9
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
package com.nova.sankuai.domain.api.yinsheng;
 
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
 
/**
 * 类名:UtilDate
 * 功能:日期工具类
 */
public class DateUtil {
 
    /**
     * 年月日时分秒毫秒(无下划线) yyyyMMddHHmmssSSS\
     *
     */
    public static final String dtLongs = "yyyyMMddHHmmssSSS";
 
    /**
     * 年月日时分秒(无下划线) yyyyMMddHHmmss
     */
    public static final String dtLong = "yyyyMMddHHmmss";
 
    /**
     * 完整时间 yyyy-MM-dd HH:mm:ss
     */
    public static final String simple = "yyyy-MM-dd HH:mm:ss";
 
    /**
     * 年月日   yyyy-MM-dd
     */
    public static final String dtShort_ = "yyyy-MM-dd";
 
    /**
     * 年月日(无下划线) yyyyMMdd
     */
    public static final String dtShort = "yyyyMMdd";
 
    /**
     * 时分秒(无下划线) HHmmss
     */
    public static final String dtTime = "HHmmss";
 
    /**
     * 获当前日期
     *
     * @param dateFormat
     * @return String
     */
    public static String getCurrentDate(String dateFormat) {
        return new SimpleDateFormat(dateFormat).format(new Date());
    }
 
    /**
     * 获取自定义格式化日期
     *
     * @param date
     * @param dateFormat
     * @return String
     */
    public static String getDateFormat(Date date, String dateFormat) {
        return new SimpleDateFormat(dateFormat).format(date);
    }
 
    /**
     * 获取当前日期前一天
     * 格式:YYYYMMDD
     */
    public static String getCurrentDateFront() {
        String strDate = new SimpleDateFormat("yyyyMMdd").format(addDays(new Date(), -1));
        strDate = strDate.substring(0, 4) + strDate.substring(4, 6) + strDate.substring(6);
        return strDate;
    }
 
    /**
     * 按日加减日期
     *
     * @param date:日期
     * @param num:要加减的日数
     * @return:成功,则返回加减后的日期;失败,则返回null
     */
    public static Date addDays(Date date, int num) {
        if (date == null) {
            return null;
        }
 
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_MONTH, num);
 
        return c.getTime();
    }
 
    /**
     * 按月加减日期
     *
     * @param date:日期
     * @param num:要加减的月数
     * @return:成功,则返回加减后的日期;失败,则返回null
     */
    public static Date addMonths(Date date, int num) {
        if (date == null) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, num);
        return c.getTime();
    }
 
    /**
     * 按年加减日期
     *
     * @param date:日期
     * @param num:要加减的年数
     * @return:成功,则返回加减后的日期;失败,则返回null
     */
    public static Date addYears(Date date, int num) {
        if (date == null) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.YEAR, num);
        return c.getTime();
    }
 
    /**
     * 按秒 加减日期
     *
     * @param date:日期
     * @param num:要加减的秒
     * @return:成功,则返回加减后的日期;失败,则返回null
     */
    public static Date addSeconds(Date date, int num) {
        if (date == null) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.SECOND, num);
        return c.getTime();
    }
 
    /**
     * 取出一个指定长度大小的随机正整数.
     *
     * @param length int 设定所取出随机数的长度。length小于11
     * @return int 返回生成的随机数。
     */
    public static int getRandom(int length) {
        int num = 1;
        double random = Math.random();
        if (random < 0.1) {
            random = random + 0.1;
        }
        for (int i = 0; i < length; i++) {
            num = num * 10;
        }
        return (int) ((random * num));
    }
 
 
 
    /**
     * 检查日期字符串是否合法
     *
     * @param dateStr 日期字符串
     * @param pattern 日期格式
     * @return 布尔
     */
    //'yyyyMMdd'  'HHmmss' 所以年月日不是yyyymmdd
    @SuppressWarnings("unused")
    public static boolean isValidDate(String dateStr, String pattern) {
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        df.setLenient(false);//来强调严格遵守该格式
        Date date = null;
        try {
            date = df.parse(dateStr);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
 
    /**
     * 取当前时间 格式为 yyyy-MM-dd HH:mm:ss
     * 
     * @return
     */
    public static String getDateNow() {
        SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        return myFormat.format(calendar.getTime());
    }
 
    /**
     * 取当前时间 格式为 yyyyMMdd
     *
     * @return
     */
    public static String getDateNowYmd() {
        SimpleDateFormat myFormat = new SimpleDateFormat("yyyyMMdd");
        Calendar calendar = Calendar.getInstance();
        return myFormat.format(calendar.getTime());
    }
 
    /**
     * @return
     * @功能描述:生成msgId
     */
    public static String getMsgId() {
        int ran = getRandom(10);
        String msgId = getCurrentDate(dtLong) + "-" + String.valueOf(ran);
        return msgId;
    }
 
}