DateUtils.java
8.09 KB
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
/**
* Copyright 2015-2025 FLY的狐狸(email:jflyfox@sina.com qq:369191470).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.jflyfox.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
/**
* 日期处理
*
*
* 2014年5月5日 下午12:00:00
* flyfox 330627517@qq.com
*/
public class DateUtils {
public static final int SECOND = 1;
public static final int MINUTE_SECOND = 60 * SECOND;
public static final int HOUR_SECOND = 60 * MINUTE_SECOND;
public static final int DAY_SECOND = 24 * HOUR_SECOND;
public static final int WEEK_SECOND = 7 * DAY_SECOND;
/** 日期格式:yyyy-MM-dd HH:mm:ss.SSS */
public static final String YMD_HMSSS = "yyyy-MM-dd HH:mm:ss.SSS";
/** 日期格式:yyyyMMddHHmmssSSS */
public static final String YMDHMSSS = "yyyyMMddHHmmssSSS";
/** 日期格式:yyyy-MM-dd HH:mm:ss */
public static final String YMD_HMS = "yyyy-MM-dd HH:mm:ss";
/** 日期格式:yyyy-MM-dd HH:mm */
public static final String YMD_HM = "yyyy-MM-dd HH:mm";
/** 日期格式:yyyyMMddHHmmss */
public static final String YMDHMS = "yyyyMMddHHmmss";
/** 日期格式:yyyy-MM-dd */
public static final String YMD = "yyyy-MM-dd";
/** 时间格式:HH:mm:ss */
public static final String HMS = "HH:mm:ss";
/**
* 默认的日期格式 .
*/
public static final String DEFAULT_REGEX = "yyyy-MM-dd";
/**
* 默认的日期格式 .
*/
public static final String DEFAULT_REGEX_YYYYMMDD = "yyyyMMdd";
/**
* 默认的日期格式 .
*/
public static final String DEFAULT_REGEX_YYYY_MM_DD_HH_MIN_SS = "yyyy-MM-dd HH:mm:ss";
/**
* 默认的DateFormat 实例
*/
private static final EPNDateFormat DEFAULT_FORMAT = new EPNDateFormat(DEFAULT_REGEX);
/**
* 默认的DateFormat 实例
*/
private static final EPNDateFormat DEFAULT_FORMAT_YYYY_MM_DD_HH_MIN_SS = new EPNDateFormat(
DEFAULT_REGEX_YYYY_MM_DD_HH_MIN_SS);
/**
* 默认的DateFormat 实例
*/
private static final EPNDateFormat DEFAULT_FORMAT_YYYYMMDD = new EPNDateFormat(DEFAULT_REGEX_YYYYMMDD);
private static Map<String, EPNDateFormat> formatMap = new HashMap<String, EPNDateFormat>();
static {
formatMap.put(DEFAULT_REGEX, DEFAULT_FORMAT);
formatMap.put(DEFAULT_REGEX_YYYY_MM_DD_HH_MIN_SS, DEFAULT_FORMAT_YYYY_MM_DD_HH_MIN_SS);
formatMap.put(DEFAULT_REGEX_YYYYMMDD, DEFAULT_FORMAT_YYYYMMDD);
}
private DateUtils() {
}
/**
* 时间对象格式化成String ,等同于java.text.DateFormat.format();
*
* @param date
* 需要格式化的时间对象
*
* 2014年5月5日 下午12:00:00
* flyfox 330627517@qq.com
* @return 转化结果
*/
public static String format(java.util.Date date) {
return DEFAULT_FORMAT.format(date);
}
/**
* 时间对象格式化成String ,等同于java.text.SimpleDateFormat.format();
*
* @param date
* 需要格式化的时间对象
* @param regex
* 定义格式的字符串
*
* 2014年5月5日 下午12:00:00
* flyfox 330627517@qq.com
* @return 转化结果
*/
public static String format(java.util.Date date, String regex) {
return getDateFormat(regex).format(date);
}
private static EPNDateFormat getDateFormat(String regex) {
EPNDateFormat fmt = formatMap.get(regex);
if (fmt == null) {
fmt = new EPNDateFormat(regex);
formatMap.put(regex, fmt);
}
return fmt;
}
/**
* 尝试解析时间字符串 ,if failed return null;
*
* @author wangp
* @since 2008.12.20
* @param time
*
* 2014年5月5日 下午12:00:00
* flyfox 330627517@qq.com
* @return
*/
public static Date parseByAll(String time) {
Date stamp = null;
if (time == null || "".equals(time))
return null;
Pattern p3 = Pattern.compile("\\b\\d{2}[.-]\\d{1,2}([.-]\\d{1,2}){0,1}\\b");
if (p3.matcher(time).matches()) {
time = (time.charAt(0) == '1' || time.charAt(0) == '0' ? "20" : "19") + time;
}
stamp = DateUtils.parse(time, "yyyy-MM-ddHH:mm:ss");
if (stamp == null)
stamp = DateUtils.parse(time, "yyyy-MM-dd");
if (stamp == null)
stamp = DateUtils.parse(time, "yyyy.MM.dd");
if (stamp == null)
stamp = DateUtils.parse(time, "yyyy-MM");
if (stamp == null)
stamp = DateUtils.parse(time, "yyyy.MM");
if (stamp == null)
stamp = DateUtils.parse(time, "yyyy-MM-dd");
if (stamp == null)
stamp = DateUtils.parse(time, "yy-MM-dd");
if (stamp == null)
stamp = DateUtils.parse(time, "yyyy.MM.dd");
if (stamp == null)
stamp = DateUtils.parse(time, "yyyy-MM.dd");
if (stamp == null)
stamp = DateUtils.parse(time, "yyyy.MM-dd");
if (stamp == null)
stamp = DateUtils.parse(time, "yyyyMMdd");
if (stamp == null)
stamp = DateUtils.parse(time, "yyyy年MM月dd日");
if (stamp == null)
stamp = DateUtils.parse(time, "yyyyMM");
if (stamp == null)
stamp = DateUtils.parse(time, "yyyy年MM月");
if (stamp == null)
stamp = DateUtils.parse(time, "yyyy");
if (stamp == null)
stamp = DateUtils.parse(time, "yyyy年");
return stamp;
}
/**
* 解析字符串成时间 ,遇到错误返回null不抛异常
*
* @param source
*
* 2014年5月5日 下午12:00:00
* flyfox 330627517@qq.com
* @return 解析结果
*/
public static java.util.Date parse(String source) {
try {
return DEFAULT_FORMAT.parse(source);
} catch (Exception e) {
return null;
}
}
/**
* 解析字符串成时间 ,遇到错误返回null不抛异常
*
* @param source
* @param 格式表达式
*
* 2014年5月5日 下午12:00:00
* flyfox 330627517@qq.com
* @return 解析结果
*/
public static java.util.Date parse(String source, String regex) {
try {
EPNDateFormat fmt = getDateFormat(regex);
return fmt.parse(source);
} catch (Exception e) {
return null;
}
}
/**
* 取得当前时间的Date对象 ;
*
* 2014年5月5日 下午12:00:00
* flyfox 330627517@qq.com
* @return
*/
public static Date getNowDate() {
return new Date(System.currentTimeMillis());
}
/**
* 获取当前时间字符串
*
* 2014年5月5日 下午12:00:00
* flyfox 330627517@qq.com
* @return
*/
public static String getNow() {
return getNow(DEFAULT_REGEX);
}
/**
* 获取当前时间字符串
*
* 2014年7月4日 下午11:47:10
* flyfox 330627517@qq.com
* @param regex 格式表达式
* @return
*/
public static String getNow(String regex) {
return format(getNowDate(), regex);
}
/***
* 获取指定时间所在天的开始时间
*
* @param date
* @return
*/
public static String getCurrenDayBeginTime(Date date) {
Calendar ca = Calendar.getInstance();
ca.setTime(date);
ca.set(Calendar.HOUR_OF_DAY, 0);
ca.set(Calendar.MINUTE, 0);
ca.set(Calendar.SECOND, 0);
return format(ca.getTime(), "yyyy-MM-dd HH:mm:ss");
}
/***
* 获取指定时间所在天的结束时间
*
* @param date
* @return
*/
public static String getCurrenDayEndTime(Date date) {
Calendar ca = Calendar.getInstance();
ca.setTime(date);
ca.set(Calendar.HOUR_OF_DAY, 23);
ca.set(Calendar.MINUTE, 59);
ca.set(Calendar.SECOND, 59);
return format(ca.getTime(), "yyyy-MM-dd HH:mm:ss");
}
}
class EPNDateFormat {
private SimpleDateFormat instance;
EPNDateFormat() {
instance = new SimpleDateFormat(DateUtils.DEFAULT_REGEX);
instance.setLenient(false);
}
EPNDateFormat(String regex) {
instance = new SimpleDateFormat(regex);
instance.setLenient(false);
}
synchronized String format(java.util.Date date) {
if (date == null)
return "";
return instance.format(date);
}
synchronized java.util.Date parse(String source) throws ParseException {
return instance.parse(source);
}
}