DateUtils.java
8.75 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
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
package com.irrigation.icl.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
public class DateUtils {
/**
* 当前时间,转换为{@link Date}对象
*
* @return 当前时间
*/
public static Date date() {
return new Date();
}
/**
* 创建Calendar对象,时间为默认时区的当前时间
*
* @return Calendar对象
* @since 4.6.6
*/
public static Calendar calendar() {
return Calendar.getInstance();
}
/**
* 转换为Calendar对象
*
* @param date 日期对象
* @return Calendar对象
*/
public static Calendar calendar(Date date) {
return calendar(date.getTime());
}
/**
* 转换为Calendar对象
*
* @param millis 时间戳
* @return Calendar对象
*/
public static Calendar calendar(long millis) {
final Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(millis);
return cal;
}
private static Calendar toCalendar(Date date) {
return toCalendar(TimeZone.getDefault(), Locale.getDefault(Locale.Category.FORMAT), date);
}
private static Calendar toCalendar(TimeZone zone, Locale locale, Date date) {
if (null == locale) {
locale = Locale.getDefault(Locale.Category.FORMAT);
}
final Calendar cal = (null != zone) ? Calendar.getInstance(zone, locale) : Calendar.getInstance(locale);
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.setTime(date);
return cal;
}
/**
* 当前时间的时间戳
*
* @return 时间
*/
public static long currentTimeMillis() {
return System.currentTimeMillis();
}
/**
* 当前时间的时间戳(秒)
*
* @return 当前时间秒数
*/
public static long currentSeconds() {
return System.currentTimeMillis() / 1000;
}
/**
* 获得年的部分
*
* @param date 日期
* @return 年的部分
*/
public static int year(Date date) {
return toCalendar(date).get(Calendar.YEAR);
}
/**
* 获得指定日期所属季度,从1开始计数
*
* @param date 日期
* @return 第几个季度
* @since 4.1.0
*/
public static int quarter(Date date) {
return month(date) / 3 + 1;
}
/**
* 获得月份,从0开始计数
*
* @param date 日期
* @return 月份,从0开始计数
*/
public static int month(Date date) {
return toCalendar(date).get(Calendar.MONTH);
}
/**
* 获得指定日期是所在年份的第几周<br>
*
* @param date 日期
* @return 周
*/
public static int weekOfYear(Date date) {
return toCalendar(date).get(Calendar.WEEK_OF_YEAR);
}
/**
* 获得指定日期是所在月份的第几周<br>
*
* @param date 日期
* @return 周
*/
public static int weekOfMonth(Date date) {
return toCalendar(date).get(Calendar.WEEK_OF_MONTH);
}
/**
* 获得指定日期是这个日期所在月份的第几天<br>
*
* @param date 日期
* @return 天
*/
public static int dayOfMonth(Date date) {
return toCalendar(date).get(Calendar.DAY_OF_MONTH);
}
/**
* 获得指定日期是星期几,1表示周日,2表示周一
*
* @param date 日期
* @return 天
*/
public static int dayOfWeek(Date date) {
return toCalendar(date).get(Calendar.DAY_OF_WEEK);
}
/**
* 获得指定日期的小时数部分<br>
*
* @param date 日期
* @return 小时数
*/
public static int hour(Date date) {
return toCalendar(date).get(Calendar.HOUR_OF_DAY);
}
/**
* 获得指定日期的分钟数部分<br>
* 例如:10:04:15.250 =》 4
*
* @param date 日期
* @return 分钟数
*/
public static int minute(Date date) {
return toCalendar(date).get(Calendar.MINUTE);
}
/**
* 获得指定日期的秒数部分<br>
*
* @param date 日期
* @return 秒数
*/
public static int second(Date date) {
return toCalendar(date).get(Calendar.SECOND);
}
/**
* 获得指定日期的毫秒数部分<br>
*
* @param date 日期
* @return 毫秒数
*/
public static int millsecond(Date date) {
return toCalendar(date).get(Calendar.MILLISECOND);
}
/**
* 当前时间,格式 yyyy-MM-dd HH:mm:ss
*
* @return 当前时间的标准形式字符串
*/
public static String now() {
return formatDateTime(new Date());
}
/**
* 当前日期,格式 yyyy-MM-dd
*
* @return 当前日期的标准形式字符串
*/
public static String today() {
return formatDate(new Date());
}
// ------------------------------------ Format start ----------------------------------------------
/**
* 根据特定格式格式化日期
*
* @param date 被格式化的日期
* @param format 日期格式,常用格式见:
* @return 格式化后的字符串
*/
public static String format(Date date, String format) {
if (null == date || ObjectUtils.stringIsEmpty(format)) {
return null;
}
return DateFormat.getInstance(format).format(date);
}
/**
* 格式化日期时间<br>
* 格式 yyyy-MM-dd HH:mm:ss
*
* @param date 被格式化的日期
* @return 格式化后的日期
*/
public static String formatDateTime(Date date) {
if (null == date) {
return null;
}
return DateFormat.NORM_DATETIME_FORMAT.format(date);
}
/**
* 格式化日期部分(不包括时间)<br>
* 格式 yyyy-MM-dd
*
* @param date 被格式化的日期
* @return 格式化后的字符串
*/
public static String formatDate(Date date) {
if (null == date) {
return null;
}
return DateFormat.NORM_DATE_FORMAT.format(date);
}
/**
* 格式化时间<br>
* 格式 HH:mm:ss
*
* @param date 被格式化的日期
* @return 格式化后的字符串
* @since 3.0.1
*/
public static String formatTime(Date date) {
if (null == date) {
return null;
}
return DateFormat.NORM_TIME_FORMAT.format(date);
}
// ------------------------------------ Format end ----------------------------------------------
// ------------------------------------ Parse start ----------------------------------------------
/**
* 构建DateTime对象
*
* @param dateStr Date字符串
* @param format 格式
* @return DateTime对象
*/
public static Date parse(String dateStr, String format) {
try {
return DateFormat.getInstance(format).parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 将特定格式的日期转换为Date对象
*
* @param dateStr 特定格式的日期
* @param format 格式,例如yyyy-MM-dd
* @param locale 区域信息
* @return 日期对象
* @since 4.5.18
*/
public static Date parse(String dateStr, String format, Locale locale) {
try {
return DateFormat.getInstance(format, locale).parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 格式yyyy-MM-dd HH:mm:ss
*
* @param dateString 标准形式的时间字符串
* @return 日期对象
*/
public static Date parseDateTime(String dateString) {
return parse(dateString, DatePattern.NORM_DATETIME_PATTERN);
}
/**
* 解析格式为yyyy-MM-dd的日期,忽略时分秒
*
* @param dateString 标准形式的日期字符串
* @return 日期对象
*/
public static Date parseDate(String dateString) {
return parse(dateString, DatePattern.NORM_DATE_PATTERN);
}
/**
* 解析时间,格式HH:mm:ss,日期部分默认为1970-01-01
*
* @param timeString 标准形式的日期字符串
* @return 日期对象
*/
public static Date parseTime(String timeString) {
return parse(timeString, DatePattern.NORM_TIME_PATTERN);
}
// ------------------------------------ Parse end ----------------------------------------------
/**
* date 转换为 long
*
* @param date
* @return
*/
public static Long dateToLong(Date date) {
return ObjectUtils.isNull(date) ? null : date.getTime();
}
}