-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtcUtils.java
More file actions
292 lines (252 loc) · 9.6 KB
/
Copy pathUtcUtils.java
File metadata and controls
292 lines (252 loc) · 9.6 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
import org.json.JSONObject;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoUnit;
import java.time.zone.ZoneRulesException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.lang.String.format;
public class UtcUtils {
public UtcUtils() {
}
/**
* returns a string , containing a UTC time, from a zonedDateTime, with offset and default locale using an ISO 8601 formatted input string
* <p>
* 2022-10-15T09:45:00.000Z
* YYYY-MM-DDThh:mm:ss.SSSZ
* <p>
* YYYY = four-digit year
* MM = two-digit month (01=January, etc.)
* DD = two-digit day of month (01 through 31)
* T = indicates that the time value is the time in Greenwich, England, or UTC, time.
* hh = two digits of hour (00 through 23) (am/pm NOT allowed)
* mm = two digits of minute (00 through 59)
* ss = two digits of second (00 through 59)
* SSS = one or more digits representing a decimal fraction of a second
* TZD = time zone designator (Z or +hh:mm or -hh:mm)
*
* @param iso8601String e.g. "2022-10-15T09:45:00.000Z"
* @param zoneid
* @return string utc time, offset (in this case +01:00, and locale) "2022-10-15T10:45+01:00[Europe/Dublin]"
*/
public static String convertInstantToZDT(String iso8601String, ZoneId zoneid) {
try{
Instant instant = Instant.parse(iso8601String);
ZonedDateTime defaultTime = instant.atZone(zoneid);
return String.valueOf(defaultTime);
} catch (DateTimeParseException dtpe) {
return "DateTimeParseException" + dtpe.getMessage();
} catch (ZoneRulesException zre) {
return "ZoneRulesException" + zre.getMessage();
}
}
public static LocalDateTime createLocalDateTime(String dateTimeString) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime localDateTime = LocalDateTime.parse(dateTimeString, formatter);
return localDateTime;
}
/**
* convert LocalDateTime to ZonedDateTime
*
* @param localDateTime
* @return String with format "yyyy-MM-dd'T'HH:mm:ss'Z'"
*/
public static LocalTime convertLDTtoZDT_UTC(LocalDateTime localDateTime) {
ZonedDateTime ldtZoned = localDateTime.atZone(ZoneId.systemDefault());
ZonedDateTime utcZoned = ldtZoned.withZoneSameInstant(ZoneId.of("UTC"));
return utcZoned.toLocalTime();
}
/**
* Using date, return an ISO8601 formatted string
*
* @param date
* @return String with format "yyyy-MM-dd'T'HH:mm:ss'Z'"
*/
public static String getDateISO8601(Date date) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault());
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
return dateFormat.format(date);
}
public static ZoneId getZoneId(String zoneIdString) {
try {
ZoneId zoneid = ZoneId.of(zoneIdString);
return zoneid;
} catch (DateTimeException dte){
System.out.println("DateTimeException" + dte.getMessage());
}
return null;
}
/**
* Using the TimeZone getAvailableId's method, a string array of available zone ids is returned
*
* @return String array of available time zones
*/
public static String[] getTimeZoneIds() {
return TimeZone.getAvailableIDs();
}
/**
* @return String representation of UTC time
*/
public String getInstant_now() {
return Instant.now().toString();
}
/**
* @param date as string
* @return String representation of UTC time
*/
public String getInstant_set(String date) {
try {
Instant instant
= Instant.parse(date);
return instant.toString();
} catch (DateTimeParseException dtpe) {
return "DateTimeParseException: " + date;
}
}
/**
* @param date as string
* @param daysNumber integer, will contain a positive or negative value
* @return String representation of UTC time
*/
public String getInstant_set_plusdays(String date, int daysNumber) {
try {
Instant instant
= Instant.parse(date);
// instant is immutable, therefore make another occurrence of instant with the addition, that will be either a positive or negative value
Instant valueDiff
= instant.plus(daysNumber, ChronoUnit.DAYS);
return valueDiff.toString();
} catch (DateTimeParseException dtpe) {
return "DateTimeParseException: " + date;
}
}
/**
* @return String
*/
public String getInstant_DateTimeFormatter(String dateTime, ZoneId zoneid) {
final String PATTERN_FORMAT = "dd.MM.yyyy HH:mm";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(PATTERN_FORMAT)
.withZone(zoneid);
try {
Instant instant = Instant.parse(dateTime);
return formatter.format(instant);
} catch (DateTimeParseException dtpe) {
return "DateTimeParseException: " + dateTime;
}
}
/**
* pass in a date time string formatted as yyyy-MM-dd HH:mm, zoneid and return a string
*
* @param dateTime string formatted as e.g. 2022-10-15 09:45
* @param zoneid
* @return String formatted as 2022-10-14T22:45:00Z
*/
public String getInstant_Milli(String dateTime, ZoneId zoneid) {
final String PATTERN_FORMAT = "yyyy-MM-dd HH:mm";
SimpleDateFormat formatter = new SimpleDateFormat(PATTERN_FORMAT, Locale.ENGLISH);
formatter.setTimeZone(TimeZone.getTimeZone(zoneid));
Date date = null;
try {
date = formatter.parse(dateTime);
String formattedDateString = formatter.format(date);
Instant instant = Instant.ofEpochMilli(date.getTime());
return instant.toString();
} catch (ParseException e) {
return "ParseException: " + dateTime;
}
}
public String get_ZonedDateTime_default(String datetime, ZoneId zoneId) {
ZonedDateTime zdt =
ZonedDateTime.of(2022, 9, 15, 9, 45, 0, 0, zoneId);
String zstTime = zdt.format(DateTimeFormatter.ISO_INSTANT);
return zstTime;
}
/**
* take a date time string ,
* separate name and offset using regex pattern,
* reformat and return string
*
* @param searchString e.g. 2022-10-15T09:45Z[Africa/Abidjan]
* @return String by name and offset or Zulu [Africa/Abidjan] 09:45Z
*/
public String getRegex(String searchString) {
// regex match +00:00, -00:00 or 00:00Z
Pattern p = Pattern.compile("(([-+]\\d{2}:\\d{2}|\\d{2}:\\d{2}Z))(\\[.*\\])");
Matcher matcher = p.matcher(searchString);
if (matcher.find()) {
String g0 = matcher.group(0);
String g1 = matcher.group(1);
String g2 = matcher.group(2);
String g3 = matcher.group(3);
return format(" %35s %s", g3, g2);
}
return "no regex match: " + searchString;
}
public String getRegex_json(String searchString) {
// regex match +00:00, -00:00 or 00:00Z
Pattern p = Pattern.compile("(([-+]\\d{2}:\\d{2}|\\d{2}:\\d{2}Z))(\\[.*\\])");
Matcher matcher = p.matcher(searchString);
if (matcher.find()) {
String g0 = matcher.group(0);
String g1 = matcher.group(1);
String g2 = matcher.group(2);
String g3 = matcher.group(3);
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", g3);
jsonObject.put("offset", g2);
if (g2.contains("Z")) {
jsonObject.put("Zulu", true);
} else {
jsonObject.put("Zulu", false);
}
return jsonObject.toString();
}
return "no regex match: " + searchString;
}
/**
* take a date time string ,
* separate name and offset using regex pattern,
* reformat and return a map of strings
*
* @param searchString e.g. 2022-10-15T09:45Z[Africa/Abidjan]
* @return Map containing name, offset and zulu as strings:
* "[Africa/Abidjan]"
* "+09:45Z"
* "true"
*
*/
public Map<String, String> getRegex_map(String searchString) {
// regex match +00:00, -00:00 or 00:00Z
Pattern p = Pattern.compile("(([-+]\\d{2}:\\d{2}|\\d{2}:\\d{2}Z))(\\[.*\\])");
Matcher matcher = p.matcher(searchString);
if (matcher.find()) {
String g0 = matcher.group(0);
String g1 = matcher.group(1);
String g2 = matcher.group(2);
String g3 = matcher.group(3);
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", g3);
jsonObject.put("offset", g2);
if (g2.contains("Z")) {
jsonObject.put("Zulu", true);
} else {
jsonObject.put("Zulu", false);
}
Map<String, String> map = new HashMap<>();
map.put("name", g3);
map.put("offset", g2);
if (g2.contains("Z")) {
map.put("Zulu", "true");
} else {
map.put("Zulu", "false");
}
return map;
}
return null;
}
}