forked from getsentry/sentry-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateUtils.java
More file actions
121 lines (109 loc) · 3.89 KB
/
DateUtils.java
File metadata and controls
121 lines (109 loc) · 3.89 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
package io.sentry;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
/** Utilities to deal with dates */
@ApiStatus.Internal
public final class DateUtils {
private static final String UTC = "UTC";
// ISO 8601
private static final String ISO_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
private static final String ISO_FORMAT_WITH_MILLIS = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
// if UTC is not found, it fallback to "GMT" which is UTC equivalent
private static final @NotNull TimeZone UTC_TIMEZONE = TimeZone.getTimeZone(UTC);
private static final @NotNull ThreadLocal<SimpleDateFormat> SDF_ISO_FORMAT_WITH_MILLIS_UTC =
new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
final SimpleDateFormat simpleDateFormat =
new SimpleDateFormat(ISO_FORMAT_WITH_MILLIS, Locale.ROOT);
simpleDateFormat.setTimeZone(UTC_TIMEZONE);
return simpleDateFormat;
}
};
private static final @NotNull ThreadLocal<SimpleDateFormat> SDF_ISO_FORMAT_UTC =
new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(ISO_FORMAT, Locale.ROOT);
simpleDateFormat.setTimeZone(UTC_TIMEZONE);
return simpleDateFormat;
}
};
private DateUtils() {}
/**
* Get the current Date (UTC)
*
* @return the UTC Date
*/
@SuppressWarnings("JdkObsolete")
public static @NotNull Date getCurrentDateTime() {
final Calendar calendar = Calendar.getInstance(UTC_TIMEZONE);
return calendar.getTime();
}
/**
* Get the Date from UTC/ISO 8601 timestamp
*
* @param timestamp UTC/ISO 8601 format eg 2000-12-31T23:59:58Z or 2000-12-31T23:59:58.123Z
* @return the UTC Date
*/
public static @NotNull Date getDateTime(final @NotNull String timestamp)
throws IllegalArgumentException {
try {
return SDF_ISO_FORMAT_WITH_MILLIS_UTC.get().parse(timestamp);
} catch (ParseException e) {
try {
// to keep compatibility with older envelopes
return SDF_ISO_FORMAT_UTC.get().parse(timestamp);
} catch (ParseException ignored) {
// invalid timestamp format
}
throw new IllegalArgumentException("timestamp is not ISO format " + timestamp);
}
}
/**
* Get the Date from millis timestamp
*
* @param timestamp millis eg 1581410911.988 (1581410911 seconds and 988 millis)
* @return the UTC Date
*/
@SuppressWarnings("JdkObsolete")
public static @NotNull Date getDateTimeWithMillisPrecision(final @NotNull String timestamp)
throws IllegalArgumentException {
try {
final String[] times = timestamp.split("\\.", -1);
final long seconds = Long.parseLong(times[0]);
final long millis = times.length > 1 ? Long.parseLong(times[1]) : 0;
return getDateTime((seconds * 1000) + millis);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("timestamp is not millis format " + timestamp);
}
}
/**
* Get the UTC/ISO 8601 timestamp from Date
*
* @param date the UTC Date
* @return the UTC/ISO 8601 timestamp
*/
public static @NotNull String getTimestamp(final @NotNull Date date) {
final DateFormat df = SDF_ISO_FORMAT_WITH_MILLIS_UTC.get();
return df.format(date);
}
/**
* Get the Date from millis timestamp
*
* @param millis the UTC millis from the epoch
* @return the UTC Date
*/
public static @NotNull Date getDateTime(final long millis) {
final Calendar calendar = Calendar.getInstance(UTC_TIMEZONE);
calendar.setTimeInMillis(millis);
return calendar.getTime();
}
}