forked from shapeblue/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateUtil.java
More file actions
349 lines (310 loc) · 14.4 KB
/
Copy pathDateUtil.java
File metadata and controls
349 lines (310 loc) · 14.4 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
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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.cloud.utils;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.YearMonth;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.OffsetDateTime;
import com.cloud.utils.exception.CloudRuntimeException;
import com.cronutils.descriptor.CronDescriptor;
import com.cronutils.model.CronType;
import com.cronutils.model.definition.CronDefinition;
import com.cronutils.model.definition.CronDefinitionBuilder;
import com.cronutils.parser.CronParser;
import org.springframework.scheduling.support.CronExpression;
public class DateUtil {
public static final int HOURS_IN_A_MONTH = 30 * 24;
public static final TimeZone GMT_TIMEZONE = TimeZone.getTimeZone("GMT");
public static final String YYYYMMDD_FORMAT = "yyyyMMddHHmmss";
private static final String ZONED_DATETIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZ";
private static final DateFormat ZONED_DATETIME_SIMPLE_FORMATTER = new SimpleDateFormat(ZONED_DATETIME_FORMAT);
private static final DateTimeFormatter[] parseFormats = new DateTimeFormatter[]{
DateTimeFormatter.ISO_OFFSET_DATE_TIME,
DateTimeFormatter.ofPattern(ZONED_DATETIME_FORMAT),
DateTimeFormatter.ISO_INSTANT,
// with milliseconds
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSSX"),
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ"),
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'"),
// legacy and non-sensical format
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'Z")
};
public static Date currentGMTTime() {
// Date object always stores milliseconds offset based on GMT internally
return new Date();
}
public static Date parseTZDateString(String str) throws ParseException {
for (DateTimeFormatter formatter : parseFormats) {
try {
OffsetDateTime dt = OffsetDateTime.parse(str, formatter);
return Date.from(dt.toInstant());
} catch (DateTimeParseException e) {
// do nothing
}
}
throw new ParseException("Unparseable date: \"" + str + "\"", 0);
}
public static Date parseDateString(TimeZone tz, String dateString) {
return parseDateString(tz, dateString, "yyyy-MM-dd HH:mm:ss");
}
public static Date parseDateString(TimeZone tz, String dateString, String formatString) {
DateFormat df = new SimpleDateFormat(formatString);
df.setTimeZone(tz);
try {
return df.parse(dateString);
} catch (ParseException e) {
throw new CloudRuntimeException("why why ", e);
}
}
public static String displayDateInTimezone(TimeZone tz, Date time) {
return getDateDisplayString(tz, time, ZONED_DATETIME_FORMAT);
}
public static String getDateDisplayString(TimeZone tz, Date time) {
return getDateDisplayString(tz, time, "yyyy-MM-dd HH:mm:ss");
}
public static String getDateDisplayString(TimeZone tz, Date time, String formatString) {
if (time == null) {
return null;
}
DateFormat df = new SimpleDateFormat(formatString);
df.setTimeZone(tz);
return df.format(time);
}
public static String getOutputString(Date date) {
if (date == null) {
return "";
}
String formattedString;
synchronized (ZONED_DATETIME_SIMPLE_FORMATTER) {
formattedString = ZONED_DATETIME_SIMPLE_FORMATTER.format(date);
}
return formattedString;
}
public static Date now() {
return new Date(System.currentTimeMillis());
}
public enum IntervalType {
HOURLY, DAILY, WEEKLY, MONTHLY;
boolean equals(String intervalType) {
return super.toString().equalsIgnoreCase(intervalType);
}
public static IntervalType getIntervalType(String intervalTypeStr) {
for (IntervalType intervalType : IntervalType.values()) {
if (intervalType.equals(intervalTypeStr)) {
return intervalType;
}
}
return null;
}
}
public static IntervalType getIntervalType(short type) {
if (type < 0 || type >= IntervalType.values().length) {
return null;
}
return IntervalType.values()[type];
}
/**
* Return next run time
* @param intervalType hourly/daily/weekly/monthly
* @param schedule MM[:HH][:DD] format. DD is day of week for weekly and day of month for monthly
* @param timezone The timezone in which the schedule string is specified
* @param startDate if specified, returns next run time after the specified startDate
* @return
*/
public static Date getNextRunTime(IntervalType type, String schedule, String timezone, Date startDate) {
String[] scheduleParts = schedule.split(":"); //MM:HH:DAY
final Calendar scheduleTime = Calendar.getInstance();
scheduleTime.setTimeZone(TimeZone.getTimeZone(timezone));
if (startDate == null) {
startDate = new Date();
}
scheduleTime.setTime(startDate);
// Throw an ArrayIndexOutOfBoundsException if schedule is badly formatted.
scheduleTime.setLenient(false);
int minutes = 0;
int hour = 0;
int day = 0;
Date execDate = null;
switch (type) {
case HOURLY:
if (scheduleParts.length < 1) {
throw new CloudRuntimeException("Incorrect schedule format: " + schedule + " for interval type:" + type.toString());
}
minutes = Integer.parseInt(scheduleParts[0]);
scheduleTime.set(Calendar.MINUTE, minutes);
scheduleTime.set(Calendar.SECOND, 0);
scheduleTime.set(Calendar.MILLISECOND, 0);
try {
execDate = scheduleTime.getTime();
} catch (IllegalArgumentException ex) {
scheduleTime.setLenient(true);
execDate = scheduleTime.getTime();
scheduleTime.setLenient(false);
}
// XXX: !execDate.after(startDate) is strictly for testing.
// During testing we use a test clock which runs much faster than the real clock
// So startDate and execDate will always be ahead in the future
// and we will never increase the time here
if (execDate.before(new Date()) || !execDate.after(startDate)) {
scheduleTime.add(Calendar.HOUR_OF_DAY, 1);
}
break;
case DAILY:
if (scheduleParts.length < 2) {
throw new CloudRuntimeException("Incorrect schedule format: " + schedule + " for interval type:" + type.toString());
}
minutes = Integer.parseInt(scheduleParts[0]);
hour = Integer.parseInt(scheduleParts[1]);
scheduleTime.set(Calendar.HOUR_OF_DAY, hour);
scheduleTime.set(Calendar.MINUTE, minutes);
scheduleTime.set(Calendar.SECOND, 0);
scheduleTime.set(Calendar.MILLISECOND, 0);
try {
execDate = scheduleTime.getTime();
} catch (IllegalArgumentException ex) {
scheduleTime.setLenient(true);
execDate = scheduleTime.getTime();
scheduleTime.setLenient(false);
}
// XXX: !execDate.after(startDate) is strictly for testing.
// During testing we use a test clock which runs much faster than the real clock
// So startDate and execDate will always be ahead in the future
// and we will never increase the time here
if (execDate.before(new Date()) || !execDate.after(startDate)) {
scheduleTime.add(Calendar.DAY_OF_YEAR, 1);
}
break;
case WEEKLY:
if (scheduleParts.length < 3) {
throw new CloudRuntimeException("Incorrect schedule format: " + schedule + " for interval type:" + type.toString());
}
minutes = Integer.parseInt(scheduleParts[0]);
hour = Integer.parseInt(scheduleParts[1]);
day = Integer.parseInt(scheduleParts[2]);
scheduleTime.set(Calendar.DAY_OF_WEEK, day);
scheduleTime.set(Calendar.HOUR_OF_DAY, hour);
scheduleTime.set(Calendar.MINUTE, minutes);
scheduleTime.set(Calendar.SECOND, 0);
scheduleTime.set(Calendar.MILLISECOND, 0);
try {
execDate = scheduleTime.getTime();
} catch (IllegalArgumentException ex) {
scheduleTime.setLenient(true);
execDate = scheduleTime.getTime();
scheduleTime.setLenient(false);
}
// XXX: !execDate.after(startDate) is strictly for testing.
// During testing we use a test clock which runs much faster than the real clock
// So startDate and execDate will always be ahead in the future
// and we will never increase the time here
if (execDate.before(new Date()) || !execDate.after(startDate)) {
scheduleTime.add(Calendar.DAY_OF_WEEK, 7);
}
;
break;
case MONTHLY:
if (scheduleParts.length < 3) {
throw new CloudRuntimeException("Incorrect schedule format: " + schedule + " for interval type:" + type.toString());
}
minutes = Integer.parseInt(scheduleParts[0]);
hour = Integer.parseInt(scheduleParts[1]);
day = Integer.parseInt(scheduleParts[2]);
if (day > 28) {
throw new CloudRuntimeException("Day cannot be greater than 28 for monthly schedule");
}
scheduleTime.set(Calendar.DAY_OF_MONTH, day);
scheduleTime.set(Calendar.HOUR_OF_DAY, hour);
scheduleTime.set(Calendar.MINUTE, minutes);
scheduleTime.set(Calendar.SECOND, 0);
scheduleTime.set(Calendar.MILLISECOND, 0);
try {
execDate = scheduleTime.getTime();
} catch (IllegalArgumentException ex) {
scheduleTime.setLenient(true);
execDate = scheduleTime.getTime();
scheduleTime.setLenient(false);
}
// XXX: !execDate.after(startDate) is strictly for testing.
// During testing we use a test clock which runs much faster than the real clock
// So startDate and execDate will always be ahead in the future
// and we will never increase the time here
if (execDate.before(new Date()) || !execDate.after(startDate)) {
scheduleTime.add(Calendar.MONTH, 1);
}
break;
default:
throw new CloudRuntimeException("Incorrect interval: " + type.toString());
}
try {
return scheduleTime.getTime();
} catch (IllegalArgumentException ex) {
scheduleTime.setLenient(true);
Date nextScheduledDate = scheduleTime.getTime();
scheduleTime.setLenient(false);
return nextScheduledDate;
}
}
public static long getTimeDifference(Date date1, Date date2){
Calendar dateCalendar1 = Calendar.getInstance();
dateCalendar1.setTime(date1);
Calendar dateCalendar2 = Calendar.getInstance();
dateCalendar2.setTime(date2);
return (dateCalendar1.getTimeInMillis() - dateCalendar2.getTimeInMillis() )/1000;
}
public static CronExpression parseSchedule(String schedule) {
if (schedule != null) {
// CronExpression's granularity is in seconds. Prepending "0 " to change the granularity to minutes.
return CronExpression.parse(String.format("0 %s", schedule));
} else {
return null;
}
}
public static String getHumanReadableSchedule(CronExpression schedule) {
CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(CronType.SPRING);
CronParser parser = new CronParser(cronDefinition);
CronDescriptor descriptor = CronDescriptor.instance();
return descriptor.describe(parser.parse(schedule.toString()));
}
public static ZonedDateTime getZoneDateTime(Date date, ZoneId tzId) {
if (date == null) {
return null;
}
ZonedDateTime zonedDate = ZonedDateTime.ofInstant(date.toInstant(), tzId);
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), TimeZone.getDefault().toZoneId());
zonedDate = zonedDate.withYear(localDateTime.getYear())
.withMonth(localDateTime.getMonthValue())
.withDayOfMonth(localDateTime.getDayOfMonth())
.withHour(localDateTime.getHour())
.withMinute(localDateTime.getMinute())
.withSecond(localDateTime.getSecond());
return zonedDate;
}
public static int getHoursInCurrentMonth(Date date) {
return YearMonth.of(date.getYear(), date.getMonth() + 1).lengthOfMonth() * 24;
}
}