forked from nibnait/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateTimeUtil.java
More file actions
258 lines (214 loc) · 10.3 KB
/
DateTimeUtil.java
File metadata and controls
258 lines (214 loc) · 10.3 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
package common.util;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import common.exception.ClientViewException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.math.NumberUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
@Slf4j
public class DateTimeUtil {
public static final String TIME_MILLISECOND_FORMAT = "HH:mm:ss SSS";
public static final String DATE_FORMAT = "yyyy-MM-dd";
public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
private static ZoneId DEFAULT_ZONE_ID = ZoneId.systemDefault();
private static ZoneOffset BEIJING_ZONE_OFFSET = ZoneOffset.of("+8");
private static int MILLI_TIMESTAMP_LENGTH = 13;
private static int SECOND_TIMESTAMP_LENGTH = 10;
public static Long toMilliTimeStamp(long timeStamp) {
int length = String.valueOf(timeStamp).length();
if (MILLI_TIMESTAMP_LENGTH == length) {
return timeStamp;
}
if (SECOND_TIMESTAMP_LENGTH == length) {
return timeStamp * 1000;
}
throw new ClientViewException("非10位/13位时间戳");
}
public static Long toSecondTimeStamp(long timeStamp) {
int length = String.valueOf(timeStamp).length();
if (MILLI_TIMESTAMP_LENGTH == length) {
return timeStamp / 1000;
}
if (SECOND_TIMESTAMP_LENGTH == length) {
return timeStamp;
}
throw new ClientViewException("非10位/13位时间戳");
}
/************************** String <==> timestamp ******************************/
public static String timeStamp2String(long timeStamp, String format) {
return new SimpleDateFormat(format).format(timeStamp);
}
public static Long string2MilliSecond(String time) {
try {
return new SimpleDateFormat(DATE_TIME_FORMAT).parse(time).getTime();
} catch (ParseException e) {
throw new ClientViewException("timeStr:{}, format:{} DateTimeConvertUtils.转时间戳失败", time, DATE_TIME_FORMAT);
}
}
public static Long string2MilliSecond(String time, String format) {
try {
return new SimpleDateFormat(format).parse(time).getTime();
} catch (ParseException e) {
throw new ClientViewException("timeStr:{}, format:{} 转时间戳失败", time, format);
}
}
/************************** LocalDateTime <==> timestamp ******************************/
public static Long localDateTime2Second(LocalDateTime localDateTime) {
return localDateTime.toEpochSecond(BEIJING_ZONE_OFFSET);
}
public static Long localDateTime2MillSecond(LocalDateTime localDateTime) {
return localDateTime.toInstant(BEIJING_ZONE_OFFSET).toEpochMilli();
}
public static LocalDate timeStamp2LocalDate(long timeStamp) {
return Instant.ofEpochMilli(toMilliTimeStamp(timeStamp)).atZone(DEFAULT_ZONE_ID).toLocalDate();
}
public static LocalDateTime timeStamp2LocalDateTime(long timeStamp) {
return Instant.ofEpochMilli(toMilliTimeStamp(timeStamp)).atZone(DEFAULT_ZONE_ID).toLocalDateTime();
}
/************************** String <==> Date ********************************************/
public static Date string2Date(String dateString) {
try {
return new SimpleDateFormat(DATE_FORMAT).parse(dateString);
} catch (Exception e) {
throw new ClientViewException("timeStr:{}, format:{} DateTimeConvertUtils.string2Date error", dateString, DATE_FORMAT);
}
}
public static Date string2Date(String dateString, String dateFormat) throws ParseException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
return simpleDateFormat.parse(dateString);
}
public static String date2String(Date date) {
return new SimpleDateFormat(DATE_FORMAT).format(date);
}
public static String date2String(Date date, String dateFormat) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
return simpleDateFormat.format(date);
}
/************************** String <==> LocalDate ********************************************/
public static LocalDate string2LocalDate(String dateString) {
return LocalDate.parse(dateString, DateTimeFormatter.ofPattern(DATE_FORMAT));
}
public static LocalDate string2LocalDate(String dateString, String dateFormat) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(dateFormat);
return LocalDate.parse(dateString, dateTimeFormatter);
}
public static String localDate2String(LocalDate localDate) {
return localDate.format(DateTimeFormatter.ofPattern(DATE_FORMAT));
}
/************************** String <==> LocalDateTime ********************************************/
public static LocalDateTime string2LocalDateTime(String dateTimeString) {
return LocalDateTime.parse(dateTimeString, DateTimeFormatter.ofPattern(DATE_TIME_FORMAT));
}
public static LocalDateTime dateString2LocalDateTime(String dateString) {
LocalDate localDate = LocalDate.parse(dateString, DateTimeFormatter.ofPattern(DATE_FORMAT));
return localDate.atStartOfDay();
}
public static LocalDateTime dateTimeString2LocalDateTime(String dateString) {
return LocalDateTime.parse(dateString, DateTimeFormatter.ofPattern(DATE_TIME_FORMAT));
}
public static String localDateTime2String(LocalDateTime localDateTime) {
return localDateTime.format(DateTimeFormatter.ofPattern(DATE_TIME_FORMAT));
}
/************************** Date ==> LocalDate ********************************************/
public static LocalDate dateToLocalDate(Date date) {
ZonedDateTime zonedDateTime = date.toInstant().atZone(DEFAULT_ZONE_ID);
return zonedDateTime.toLocalDate();
}
/************************** Date ==> LocalDateTime ****************************************/
public static LocalDateTime dateToLocalDateTime(Date date) {
ZonedDateTime zonedDateTime = date.toInstant().atZone(DEFAULT_ZONE_ID);
return zonedDateTime.toLocalDateTime();
}
/******************* java.sql.Date, LocalDate, LocalDateTime ==> Date ********************/
public static Date toUtilDate(Object date) {
if (date == null) {
return null;
}
if (date instanceof java.sql.Date || date instanceof java.sql.Timestamp) {
return new Date(((Date) date).getTime());
}
if (date instanceof Date) {
return (Date) date;
}
if (date instanceof LocalDate) {
return Date.from(((LocalDate) date).atStartOfDay(DEFAULT_ZONE_ID).toInstant());
}
if (date instanceof LocalDateTime) {
return Date.from(((LocalDateTime) date).atZone(DEFAULT_ZONE_ID).toInstant());
}
if (date instanceof ZonedDateTime) {
return Date.from(((ZonedDateTime) date).toInstant());
}
if (date instanceof Instant) {
return Date.from((Instant) date);
}
throw new UnsupportedOperationException("Don't know hot to convert " + date.getClass().getName() + " to java.util.Date");
}
/**
* 校验时期合法性
*/
public static boolean isDateValid(List<String> date) {
boolean isvalid = true;
if (CollectionUtils.isEmpty(date)) {
return false;
}
try {
if (date.size() == 1) {
return NumberUtils.isParsable(date.get(0));
}
if (date.size() == 2) {
LocalDate.of(Integer.parseInt(date.get(0)), Integer.parseInt(date.get(1)), 1);
}
if (date.size() == 3) {
if (NumberUtils.isParsable(date.get(2))) {
LocalDate.of(Integer.parseInt(date.get(0)), Integer.parseInt(date.get(1)), Integer.parseInt(date.get(2)));
} else {
LocalDate.of(Integer.parseInt(date.get(0)), Integer.parseInt(date.get(1)), 1);
if (!Lists.newArrayList("上旬", "中上旬", "中旬", "中下旬", "下旬").contains(date.get(2))) {
isvalid = false;
}
}
}
if (date.size() > 3) {
isvalid = false;
}
} catch (Exception e) {
isvalid = false;
}
return isvalid;
}
public static void main(String[] args) {
List<String> date = Lists.newArrayList("2020", "02", "29");
System.out.println(isDateValid(date) + "\t" + JSON.toJSONString(date));
date = Lists.newArrayList("2021", "02", "29");
System.out.println(isDateValid(date) + "\t" + JSON.toJSONString(date));
date = Lists.newArrayList("2022", "02", "29");
System.out.println(isDateValid(date) + "\t" + JSON.toJSONString(date));
date = Lists.newArrayList("2023", "02", "29");
System.out.println(isDateValid(date) + "\t" + JSON.toJSONString(date));
date = Lists.newArrayList("2019", "02", "29");
System.out.println(isDateValid(date) + "\t" + JSON.toJSONString(date));
date = Lists.newArrayList("2024", "02", "29");
System.out.println(isDateValid(date) + "\t" + JSON.toJSONString(date));
date = Lists.newArrayList("2024", "04", "31");
System.out.println(isDateValid(date) + "\t" + JSON.toJSONString(date));
date = Lists.newArrayList("2024", "05", "31");
System.out.println(isDateValid(date) + "\t" + JSON.toJSONString(date));
date = Lists.newArrayList("2020", "02");
System.out.println(isDateValid(date) + "\t" + JSON.toJSONString(date));
date = Lists.newArrayList("2020");
System.out.println(isDateValid(date) + "\t" + JSON.toJSONString(date));
date = Lists.newArrayList("2020", "02", "上旬");
System.out.println(isDateValid(date) + "\t" + JSON.toJSONString(date));
date = Lists.newArrayList("2020", "02", "xx");
System.out.println(isDateValid(date) + "\t" + JSON.toJSONString(date));
date = Lists.newArrayList("2020", "02", "中旬", "lj");
System.out.println(isDateValid(date) + "\t" + JSON.toJSONString(date));
}
}