-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathDateUtil.java
More file actions
258 lines (229 loc) · 9.76 KB
/
DateUtil.java
File metadata and controls
258 lines (229 loc) · 9.76 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
// 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
// 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.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import com.cloud.utils.exception.CloudRuntimeException;
public class DateUtil {
public static final TimeZone GMT_TIMEZONE = TimeZone.getTimeZone("GMT");
public static final String YYYYMMDD_FORMAT = "yyyyMMddHHmmss";
private static final DateFormat _outputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
public static Date currentGMTTime() {
// Date object always stores miliseconds offset based on GMT internally
return new Date();
}
// yyyy-MM-ddTHH:mm:ssZxxxx
public static Date parseTZDateString(String str) throws ParseException {
DateFormat dfParse = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'Z");
return dfParse.parse(str);
}
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, "yyyy-MM-dd HH:mm:ss z");
}
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) {
DateFormat df = new SimpleDateFormat(formatString);
df.setTimeZone(tz);
return df.format(time);
}
public static String getOutputString(Date date) {
if (date == null) {
return "";
}
String formattedString = null;
synchronized(_outputFormat) {
formattedString = _outputFormat.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);
execDate = scheduleTime.getTime();
// 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);
execDate = scheduleTime.getTime();
// 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);
execDate = scheduleTime.getTime();
// 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);
execDate = scheduleTime.getTime();
// 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());
}
return scheduleTime.getTime();
}
// test only
public static void main(String[] args) {
TimeZone localTimezone = Calendar.getInstance().getTimeZone();
TimeZone gmtTimezone = TimeZone.getTimeZone("GMT");
TimeZone estTimezone = TimeZone.getTimeZone("EST");
Date time = new Date();
System.out.println("local time :" + getDateDisplayString(localTimezone, time));
System.out.println("GMT time :" + getDateDisplayString(gmtTimezone, time));
System.out.println("EST time :" + getDateDisplayString(estTimezone, time));
//Test next run time. Expects interval and schedule as arguments
if(args.length == 2) {
System.out.println("Next run time: "+ getNextRunTime(IntervalType.getIntervalType(args[0]), args[1], "GMT", time).toString());
}
time = new Date();
DateFormat dfDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'Z");
String str = dfDate.format(time);
System.out.println("Formated TZ time string : "+ str);
try {
Date dtParsed = DateUtil.parseTZDateString(str);
System.out.println("Parsed TZ time string : "+ dtParsed.toString());
} catch (ParseException e) {
}
}
}