◉◡◉ 您好,欢迎到访伊成个人站!

分享一个Joda-Time日期时间工具类

写在前面

在JDK1.8之前,处理日期和时间的方式比较单一,Java中提供了Calendar来处理日期,但是过程较为繁琐。
但是在JDK1.8之后,Java更新了time包提供了LocalDate,LocalTime,LocalDateTime等日期时间类来处理较为复杂的关于日期和时间的业务逻辑的方法。

现在介绍Joda-Time日期时间工具类,该类库的开发者参与了JDK1.8中time包的开发,所以在那些使用JDK1.8之前的项目,Joda-Time是一个不错的选择。而在JDK1.8之后,该工具类也是值得推荐使用的,其原因在于高效和安全。

完整代码展示

引入必要的依赖:

1
2
3
4
5
6
7
8
9
10
11
12
<!-- https://mvnrepository.com/artifact/joda-time/joda-time -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.9</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>

Joda-Time工具类

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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
package com.h.util.time;

import com.sun.istack.internal.Nullable;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.Days;
import org.joda.time.LocalDate;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/**
*
* 时间工具类
* Joda-Time提供了一组Java类包用于处理包括ISO8601标准在内的date和time.
* 可以利用它把JDK Date和Calendar类完全替换掉,而且仍然能够提供很好的集成.
* 尤其对时间的加减处理起来特别方便,快速.
*/
public class TimeUtil {
public static final long SECOND = 1000; //1秒,毫秒为单位
public static final long MINUTE = SECOND * 60; //一分钟
public static final long HOUR = MINUTE * 60; // 一小时
public static final long DAY= HOUR * 24; //一天
public static final long WEEK = DAY * 7; //一周
public static final long YEAR = DAY * 365; //一年
public static final String FORMAT_TIME = "yyyy-MM-dd HH:mm:ss"; //默认时间格式
public static final String FORMAT_TIME_MINUTE = "yyyy-MM-dd HH:mm"; //默认时间格式
public static final String FORTER_DATE = "yyyy-MM-dd"; //默认日

/**
* 获取当前系统时间
* @return yyyy-MM-dd HH:mm:ss
*/
public static String getCurrentTime() {
return getCurrentTimePattern(FORMAT_TIME);
}

/**
* 获取系统当前时间按照指定格式返回
* @param pattern yyyy/MM/dd hh:mm:ss
* @return
*/
public static String getCurrentTimePattern(String pattern) {
DateTime dt = new DateTime();
String time = dt.toString(pattern);
return time;
}

/**
* 按照时区转换时间
* @param date
* @param timeZone 时区
* @param parrten
* @return
*/
@Nullable
public static String format(Date date, TimeZone timeZone, String parrten) {
if (date == null) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat(parrten);
sdf.setTimeZone(timeZone);
return sdf.format(date);
}

/**
* 获取当前日期是星期几
* @return
*/
public static String getCurrentWeek(){
return getWeek(new DateTime());
}

/**
* 获取指定时间
* @param year 年
* @param month 月
* @param day 天
* @param hour 小时
* @param minute 分钟
* @param seconds 秒
* @return yyyy-MM-dd HH:mm:ss
*/
public static String getPointTime(Integer year, Integer month, Integer day, Integer hour, Integer minute, Integer seconds) {
return getPointTimePattern(year,month,day,hour,minute,seconds,FORMAT_TIME);
}

/**
*
* @param year 年
* @param month 月
* @param day 天
* @param hour 小时
* @param minute 分钟
* @param seconds 秒
* @param parrten 自定义格式
* @return parrten
*/
public static String getPointTimePattern(Integer year, Integer month, Integer day, Integer hour, Integer minute, Integer seconds, String parrten) {
DateTime dt = new DateTime(year, month, day, hour, minute, seconds);
String date = dt.toString(parrten);
return date;
}

/**
* 获取指定日期
* @param year
* @param month
* @param day
* @return
*/
public static String getPointDate(Integer year, Integer month, Integer day) {
return getPointDatParrten(year,month,day,FORTER_DATE);
}

/**
* 获取指定日期 返回指定格式
* @param year
* @param month
* @param day
* @param parrten
* @return
*/
public static String getPointDatParrten(Integer year, Integer month, Integer day, String parrten) {
LocalDate dt = new LocalDate(year, month, day);
String date = dt.toString(parrten);
return date;
}

/**
* 获取指定时间是一周的星期几
* @param year
* @param month
* @param day
* @return
*/
public static String getWeekPoint(Integer year, Integer month, Integer day) {
LocalDate dts = new LocalDate(year, month, day);
return getWeek(dts);
}


/**
* 获取指定日期是星期几
* @param dts
* @return 如星期一
*/
public static String getWeek(LocalDate dts){
return WeekEnum.getDesc(dts.getDayOfWeek());
}

/**
* 获取指定日期是星期几
* @param dts
* @return 如星期一
*/
public static String getWeek(DateTime dts){
return WeekEnum.getDesc(dts.getDayOfWeek());
}

/**
* 格式化日期
* @param date
* @return yyyy-MM-dd HH:mm:ss
*/
@Nullable
public static String format(Date date) {
return format(date,FORMAT_TIME);
}

/**
* 格式化日期字符串
* @param date 日期
* @param pattern 日期格式
* @return
*/
@Nullable
public static String format(Date date, String pattern) {
if (date == null) {
return null;
}
SimpleDateFormat format = new SimpleDateFormat(pattern);
return format.format(date);
}

/**
* 解析日期yyyy-MM-dd HH:mm:ss
* @param date 日期字符串
* @return
*/
@Nullable
public static Date parse(String date) {
return parse(date,FORMAT_TIME);
}

/**
* 解析日期
* @param date 日期字符串
* @param pattern 日期格式
* @return
*/
@Nullable
public static Date parse(String date, String pattern) {
if (date == null) {
return null;
}
Date resultDate = null;
try {
resultDate = new SimpleDateFormat(pattern).parse(date);
} catch (ParseException e) {
throw new RuntimeException("日期解析错误!");
}
return resultDate;
}

/**
* 解析日期 yyyy-MM-dd HH:mm:ss
* @param timestamp
* @return
*/
public static String format(Long timestamp) {
return format(timestamp,FORMAT_TIME);
}

/**
* 解析日期 yyyy-MM-dd HH:mm:ss
* @param timestamp
* @return
*/
public static String format(Long timestamp, String pattern) {
String dateStr = "";
if (null == timestamp || timestamp.longValue() < 0) {
return dateStr;
}
try {
Date date = new Date(timestamp);
SimpleDateFormat format = new SimpleDateFormat(pattern);
dateStr = format.format(date);
} catch (Exception e) {
// ignore
}

return dateStr;
}

/**
*获取当前时间前几天时间
* @param days
* @return
*/
public static Date forwardDay(int days) {
DateTime dt = new DateTime();
DateTime y = dt.minusDays(days);
return y.toDate();
}

/**
*获取当前时间前几天时间,按指定格式返回
* @param days
* @return
*/
public static String forwardDay(int days, String format) {
DateTime dt = new DateTime();
DateTime y = dt.minusDays(days);
return y.toString(format);
}

public static Date forwardDay(Date date,Integer days){
DateTime dt = new DateTime(date);
DateTime y = dt.minusDays(days);
return y.toDate();
}

public static String forwardDay(Date date,Integer days,String format){
DateTime dt = new DateTime(date);
DateTime y = dt.minusDays(days);
return y.toString(format);
}

/**
* 获取指定时间之后或者之前的某一天00:00:00 默认返回当天
* @param days
* @return
*/
public static Date day00(Integer days, String date, String zimeZone) throws Throwable {
DateTime dt;
TimeZone timeZone;
try {
if (StringUtils.isBlank(zimeZone)) {
timeZone = TimeZone.getDefault();
} else {
timeZone = TimeZone.getTimeZone(zimeZone);
}
if (StringUtils.isBlank(date)) {
dt = new DateTime().withZone(DateTimeZone.forTimeZone(timeZone)).toLocalDateTime().toDateTime();
} else {
dt = new DateTime(date).withZone(DateTimeZone.forTimeZone(timeZone)).toLocalDateTime().toDateTime();
}
} catch (Exception e) {
throw new Throwable(e);
}

DateTime y = dt.minusDays(days).withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0);
return y.toDate();
}

/**
*获取指定时间之后或者之前的某一天23:59:59 默认返回当天
* @param days 偏移量
* @return
*/
public static Date day59(Integer days, String date, String zimeZone) throws Throwable {
DateTime dt;
TimeZone timeZone;
try {
if (StringUtils.isBlank(zimeZone)) {
timeZone = TimeZone.getDefault();
} else {
timeZone = TimeZone.getTimeZone(zimeZone);
}
if (StringUtils.isBlank(date)) {

dt = new DateTime().withZone(DateTimeZone.forTimeZone(timeZone)).toLocalDateTime().toDateTime();
} else {
dt = new DateTime(date).withZone(DateTimeZone.forTimeZone(timeZone)).toLocalDateTime().toDateTime();
}
} catch (Exception e) {
throw new Throwable(e);
}
DateTime y = dt.minusDays(days).withHourOfDay(23).withMinuteOfHour(59).withSecondOfMinute(59);
return y.toDate();
}

/**
* 计算两个时间相差多少天
* @param startDate
* @param endDate
* @return
*/
@Nullable
public static Integer diffDay(Date startDate, Date endDate) {
if (startDate == null || endDate == null) {
return null;
}
DateTime dt1 = new DateTime(startDate);
DateTime dt2 = new DateTime(endDate);
int day = Days.daysBetween(dt1, dt2).getDays();
return Math.abs(day);
}

/**
* 获取某月之前,之后某一个月最后一天,24:59:59
* @return
*/
public static Date lastDay(Date date, Integer month) {
DateTime dt1;
if (month == null) {
month = 0;
}
if (date == null) {
dt1 = new DateTime().minusMonths(month);
} else {
dt1 = new DateTime(date).minusMonths(month);
}
DateTime lastDay = dt1.dayOfMonth().withMaximumValue().
withHourOfDay(23).withMinuteOfHour(59).withSecondOfMinute(59);
return lastDay.toDate();
}

/**
*获取某月月之前,之后某一个月第一天,00:00:00
* @return
*/
public static Date firstDay(Date date, Integer month) {
DateTime dt1;
if (month == null) {
month = 0;
}
if (date == null) {
dt1 = new DateTime().minusMonths(month);
} else {
dt1 = new DateTime(date).minusMonths(month);
}
DateTime lastDay = dt1.dayOfMonth().withMinimumValue().
withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0);
return lastDay.toDate();
}

public static Date addDay(Date date, int offset) {
DateTime dt1;
if (date == null) {
dt1 = new DateTime().plusDays(offset);
return dt1.toDate();
}
dt1 = new DateTime(date).plusDays(offset);
return dt1.toDate();

}

/**
* 传入日期时间与当前系统日期时间的比较,
* 若日期相同,则根据时分秒来返回 ,
* 否则返回具体日期
* @return 日期或者 xx小时前||xx分钟前||xx秒前
*/
@Nullable
public static String getNewUpdateDateString(Date now, Date createDate) {
if (now == null || createDate == null) {
return null;
}
Long time = (now.getTime() - createDate.getTime());
if (time > (24 * 60 * 60 * 1000)) {
return time / (24 * 60 * 60 * 1000) + "天前";
} else if (time > (60 * 60 * 1000)) {
return time / (60 * 60 * 1000) + "小时前";
} else if (time > (60 * 1000)) {
return time / (60 * 1000) + "分钟前";
} else if (time >= 1000) {
return time / 1000 + "秒前";
}
return "刚刚";
}

enum WeekEnum{
MONDAY(1,"星期一"),
TUESDAY(2,"星期二"),
WEDNESDAY(3,"星期三"),
THURSDAY(4,"星期四"),
FRIDAY(5,"星期五"),
SATURDAY(6,"星期六"),
SUNDAY(7,"星期日");

private int index;
private String desc;

WeekEnum(int index, String desc) {
this.index = index;
this.desc = desc;
}

public static String getDesc(int index){
String desc = "";
WeekEnum[] values = WeekEnum.values();
for (WeekEnum weekEnum:values){
if (weekEnum.index == index){
desc = weekEnum.desc;
}
}
return desc;
}
}
}

The end.

支付宝打赏 微信打赏