-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDateUtils.java
377 lines (340 loc) · 10.5 KB
/
DateUtils.java
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
package com.hqx.common.utils;
import com.hqx.common.exception.MyException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
/**
* @ClassName DateUtils
* @Author ph
* @Version 1.0
* @Description
* @Date 2020/4/8 10:27
*/
@Slf4j
public class DateUtils {
public static String pattern = "yyyy-MM-dd";
public static SimpleDateFormat formatter = new SimpleDateFormat(pattern);
public static SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(pattern);
public static DateTimeFormatter getDateTimeFormatter() {
return dateFormatter;
}
/**
* 时间戳转日期
* @param timestamp
* @return
*/
public static String timestamp2DateString(Long timestamp){
Date date = new Date(timestamp);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.format(date);
}
/**
* 将日期转换为时间戳
* @param s
* @return
*/
public static String dateToStamp(String s) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date;
try {
date = format.parse(s);
} catch (ParseException e) {
throw MyException.newException("日期转换错误");
}
long ts = date.getTime();
return String.valueOf(ts);
}
/**
* 获取现在时间
*
* @return 返回时间类型 yyyy-MM-dd HH:mm:ss
*/
public static Date getNowDate() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(new Date());
ParsePosition pos = new ParsePosition(8);
Date currentTime_2 = formatter.parse(dateString, pos);
return currentTime_2;
}
/**
* 获取现在时间
*
* @return返回短时间格式 yyyy-MM-dd
*/
public static Date getNowDateShort() {
String dateString = formatter.format(new Date());
ParsePosition pos = new ParsePosition(8);
Date currentTime_2 = formatter.parse(dateString, pos);
return currentTime_2;
}
/**
* 获取现在时间
*
* @return返回字符串格式 yyyy-MM-dd HH:mm:ss
*/
public static String getStringDate() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(new Date());
return dateString;
}
/**
* 获取现在时间
*
* @return返回字符串格式 yyyyMMddHHmmss
*/
public static String getStringAllDate() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssSSS");
String dateString = formatter.format(new Date());
return dateString;
}
/**
* 获取现在时间
*
* @return 返回短时间字符串格式yyyy-MM-dd
*/
public static String getStringDateShort() {
String dateString = formatter.format(new Date());
return dateString;
}
/**
* 获取时间 小时:分;秒 HH:mm:ss
*
* @return
*/
public static String getTimeShort() {
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
String dateString = formatter.format(new Date());
return dateString;
}
/**
* 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
*
* @param strDate
* @return
*/
public static Date strToDateLong(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
/**
* 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss
*
* @param dateDate
* @return
*/
public static String dateToStrLong(Date dateDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(dateDate);
return dateString;
}
/**
* 将短时间格式时间转换为字符串 yyyy-MM-dd
*
* @param dateDate
* @param
* @return
*/
public static String dateToStr(Date dateDate) {
String dateString = formatter.format(dateDate);
return dateString;
}
public static String dateToStr(java.time.LocalDate dateDate) {
String dateString = dateFormatter.format(dateDate);
return dateString;
}
/**
* 将短时间格式字符串转换为时间 yyyy-MM-dd
*
* @param strDate
* @return
*/
public static Date strToDate(String strDate) {
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
/**
* 得到现在时间
*
* @return
*/
public static Date getNow() {
Date currentTime = new Date();
return currentTime;
}
/**
* 提取一个月中的最后一天
*
* @param day
* @return
*/
public static Date getLastDate(long day) {
Date date = new Date();
long date_3_hm = date.getTime() - 3600000 * 34 * day;
Date date_3_hm_date = new Date(date_3_hm);
return date_3_hm_date;
}
/**
* 得到现在时间
*
* @return 字符串 yyyyMMdd HHmmss
*/
public static String getStringToday() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmmss");
String dateString = formatter.format(currentTime);
return dateString;
}
/**
* 得到当前日期字符串 格式(yyyyMMdd)
*/
public static String getDate() {
return getDate("yyyyMMdd");
}
/**
* 得到当前日期字符串 格式(yyyy-MM-dd) pattern可以为:"yyyy-MM-dd" "HH:mm:ss" "E"
*/
public static String getDate(String pattern) {
return DateFormatUtils.format(new Date(), pattern);
}
/**
* Date 与Date之间相差是多少分钟
* @param endDate
* @param nowDate
* @return 返回分钟
*/
public static int dateFromDateReturnMin(Date endDate, Date nowDate) {
//每天毫秒数
long nd = 1000 * 24 * 60 * 60;
//每小时毫秒数
long nh = 1000 * 60 * 60;
//每分钟毫秒数
long nm = 1000 * 60;
// 获得两个时间的毫秒时间差异
long diff = endDate.getTime() - nowDate.getTime();
// 计算差多少分钟
long min = diff / nm;
return (int)min;
}
/**
* Date 与Date之间相差是多少小时
* @param endDate
* @param nowDate
* @return 返回小时
*/
public static int dateFromDateReturnHour(Date endDate, Date nowDate) {
//每分钟毫秒数
long nm = 1000 * 60;
//每天毫秒数
long nd = 1000 * 24 * 60 * 60;
//每小时毫秒数
long nh = 1000 * 60 * 60;
// 获得两个时间的毫秒时间差异
long diff = endDate.getTime() - nowDate.getTime();
// 计算差多少小时
long hour = diff % nd / nh;
return (int)hour;
}
/**
* Date 与Date之间相差是多少天
* @param endDate
* @param nowDate
* @return 返回天
*/
public static int dateFromDateReturnDay(Date endDate, Date nowDate) {
//每天毫秒数
long nd = 1000 * 24 * 60 * 60;
// 获得两个时间的毫秒时间差异
long diff = endDate.getTime() - nowDate.getTime();
// 计算差多少天
long day = diff / nd;
return (int)day;
}
/**
* 当前月第一天
* @return
*/
public static String firstDayFromMonth(){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//获取当前月第一天:
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, 0);
//设置为1号,当前日期既为本月第一天
c.set(Calendar.DAY_OF_MONTH,1);
String first = format.format(c.getTime());
log.info("===============first:"+first);
return first;
}
/**
* 当前月最后一天
* @return
*/
public static String lastDayFromMonth(){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//获取当前月最后一天
Calendar ca = Calendar.getInstance();
ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
String last = format.format(ca.getTime());
log.info("===============last:"+last);
return last;
}
/**
* 获取自定义距离今天的日期
* @param dayNum 前一天:-1 后一天:+1
* @return String
*/
public static String getDayFromNow(Integer dayNum){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
if(dayNum != null && dayNum != 0){
calendar.add(Calendar.DAY_OF_MONTH, dayNum);
}
date = calendar.getTime();
return sdf.format(date);
}
/**
* 获取当天日期前后天数的零点时间
* @param dayNum 加减的天数
* @return
*/
public static Date getZeroDate(Integer dayNum){
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
if(dayNum != null && dayNum != 0){
calendar.add(Calendar.DATE,dayNum);
}
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Date zero = calendar.getTime();
return zero;
}
/**
* 获取某天日期前后天数的零点时间
*
* @param dayNum 加减的天数
* @return
*/
public static Date getZeroByDate(Date date, Integer dayNum) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
if (dayNum != null && dayNum != 0) {
calendar.add(Calendar.DATE, dayNum);
}
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Date zero = calendar.getTime();
return zero;
}
}