toTimeUnit
last analyzed

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
c 0
b 0
f 0
1
package org.apereo.cas.util;
2
3
import org.joda.time.DateTime;
4
import org.joda.time.ReadableInstant;
5
6
import java.sql.Timestamp;
7
import java.time.Instant;
8
import java.time.LocalDate;
9
import java.time.LocalDateTime;
10
import java.time.LocalTime;
11
import java.time.ZoneId;
12
import java.time.ZoneOffset;
13
import java.time.ZonedDateTime;
14
import java.time.chrono.ChronoZonedDateTime;
15
import java.time.format.DateTimeFormatter;
16
import java.time.temporal.ChronoUnit;
17
import java.time.temporal.TemporalAccessor;
18
import java.util.Calendar;
19
import java.util.Date;
20
import java.util.concurrent.TimeUnit;
21
22
/**
23
 * @author Timur Duehr [email protected]
24
 * @since 5.0.0
25
 */
26
public final class DateTimeUtils {
27
28
    private DateTimeUtils() {
29
    }
30
31
    /**
32
     * Parse the given value as a local datetime.
33
     *
34
     * @param value the value
35
     * @return the date/time instance
36
     */
37
    public static LocalDateTime localDateTimeOf(final String value) {
38
        LocalDateTime result;
39
        try {
40
            result = LocalDateTime.parse(value, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
41
        } catch (final Exception e) {
42
            result = null;
43
        }
44
45
        try {
46
            result = LocalDateTime.parse(value, DateTimeFormatter.ISO_ZONED_DATE_TIME);
47
        } catch (final Exception e) {
48
            result = null;
49
        }
50
51
        if (result == null) {
52
            try {
53
                result = LocalDateTime.parse(value);
54
            } catch (final Exception e) {
55
                result = null;
56
            }
57
        }
58
        
59
        if (result == null) {
60
            try {
61
                result = LocalDateTime.parse(value.toUpperCase(), DateTimeFormatter.ofPattern("MM/dd/yyyy hh:mm a"));
62
            } catch (final Exception e) {
63
                result = null;
64
            }
65
        }
66
67
        if (result == null) {
68
            try {
69
                result = LocalDateTime.parse(value.toUpperCase(), DateTimeFormatter.ofPattern("MM/dd/yyyy h:mm a"));
70
            } catch (final Exception e) {
71
                result = null;
72
            }
73
        }
74
75
        if (result == null) {
76
            try {
77
                result = LocalDateTime.parse(value, DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm"));
78
            } catch (final Exception e) {
79
                result = null;
80
            }
81
        }
82
83
        if (result == null) {
84
            try {
85
                final LocalDate ld = LocalDate.parse(value, DateTimeFormatter.ofPattern("MM/dd/yyyy"));
86
                result = LocalDateTime.of(ld, LocalTime.now());
87
            } catch (final Exception e) {
88
                result = null;
89
            }
90
        }
91
        
92
        if (result == null) {
93
            try {
94
                final LocalDate ld = LocalDate.parse(value);
95
                result = LocalDateTime.of(ld, LocalTime.now());
96
            } catch (final Exception e) {
97
                result = null;
98
            }
99
        }
100
101
        return result;
102
    }
103
104
    /**
105
     * Local date time of local date time.
106
     *
107
     * @param time the time
108
     * @return the local date time
109
     */
110
    public static LocalDateTime localDateTimeOf(final long time) {
111
        return LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneOffset.systemDefault());
112
    }
113
114
    /**
115
     * Local date time of local date time.
116
     *
117
     * @param time the time
118
     * @return the local date time
119
     */
120
    public static LocalDateTime localDateTimeOf(final Date time) {
121
        return localDateTimeOf(time.getTime());
122
    }
123
124
    /**
125
     * Parse the given value as a zoned datetime.
126
     *
127
     * @param value the value
128
     * @return the date/time instance
129
     */
130
    public static ZonedDateTime zonedDateTimeOf(final String value) {
131
        try {
132
            return ZonedDateTime.parse(value);
133
        } catch (final Exception e) {
134
            return null;
135
        }
136
    }
137
138
    /**
139
     * Utility for creating a ZonedDateTime object from a ZonedDateTime.
140
     *
141
     * @param time ZonedDateTime to be copied
142
     * @return ZonedDateTime representing time
143
     */
144
145
    public static ZonedDateTime zonedDateTimeOf(final TemporalAccessor time) {
146
        return ZonedDateTime.from(time);
147
    }
148
149
    /**
150
     * Gets ZonedDateTime for ReadableInstant.
151
     *
152
     * @param time Time object to be converted.
153
     * @return ZonedDateTime representing time
154
     */
155
    public static ZonedDateTime zonedDateTimeOf(final ReadableInstant time) {
156
        return zonedDateTimeOf(time.getMillis());
157
    }
158
159
    /**
160
     * Utility for creating a ZonedDateTime object from a millisecond timestamp.
161
     *
162
     * @param time Milliseconds since Epoch UTC
163
     * @return ZonedDateTime representing time
164
     */
165
    public static ZonedDateTime zonedDateTimeOf(final long time) {
166
        return zonedDateTimeOf(time, ZoneOffset.UTC);
167
    }
168
169
    /**
170
     * Utility for creating a ZonedDateTime object from a millisecond timestamp.
171
     *
172
     * @param time   Miliseconds since Epoch
173
     * @param zoneId Time zone
174
     * @return ZonedDateTime representing time
175
     */
176
    public static ZonedDateTime zonedDateTimeOf(final long time, final ZoneId zoneId) {
177
        return ZonedDateTime.ofInstant(Instant.ofEpochMilli(time), zoneId);
178
    }
179
180
    /**
181
     * Gets ZonedDateTime for Date.
182
     *
183
     * @param time Time object to be converted.
184
     * @return ZonedDateTime representing time
185
     */
186
    public static ZonedDateTime zonedDateTimeOf(final Date time) {
187
        return zonedDateTimeOf(time.getTime());
188
    }
189
190
    /**
191
     * Gets ZonedDateTime for Calendar.
192
     *
193
     * @param time Time object to be converted.
194
     * @return ZonedDateTime representing time
195
     */
196
    public static ZonedDateTime zonedDateTimeOf(final Calendar time) {
197
        return ZonedDateTime.ofInstant(time.toInstant(), time.getTimeZone().toZoneId());
198
    }
199
200
    /**
201
     * Gets DateTime for Instant.
202
     *
203
     * @param time Time object to be converted.
204
     * @return DateTime representing time
205
     */
206
    public static DateTime dateTimeOf(final Instant time) {
207
        return new DateTime(time.toEpochMilli());
208
    }
209
210
    /**
211
     * Gets DateTime for ZonedDateTime.
212
     *
213
     * @param time Time object to be converted.
214
     * @return DateTime representing time
215
     */
216
    public static DateTime dateTimeOf(final ChronoZonedDateTime time) {
217
        return dateTimeOf(time.toInstant());
218
    }
219
220
    /**
221
     * Gets Date for ZonedDateTime.
222
     *
223
     * @param time Time object to be converted.
224
     * @return Date representing time
225
     */
226
    public static Date dateOf(final ChronoZonedDateTime time) {
227
        return dateOf(time.toInstant());
228
    }
229
230
231
    /**
232
     * Gets Date for Instant.
233
     *
234
     * @param time Time object to be converted.
235
     * @return Date representing time
236
     */
237
    public static Date dateOf(final Instant time) {
238
        return Date.from(time);
239
    }
240
241
    /**
242
     * Gets Timestamp for ZonedDateTime.
243
     *
244
     * @param time Time object to be converted.
245
     * @return Timestamp representing time
246
     */
247
    public static Timestamp timestampOf(final ChronoZonedDateTime time) {
248
        return timestampOf(time.toInstant());
249
    }
250
251
    /**
252
     * Gets Timestamp for Instant.
253
     *
254
     * @param time Time object to be converted.
255
     * @return Timestamp representing time
256
     */
257
    private static Timestamp timestampOf(final Instant time) {
258
        return Timestamp.from(time);
259
    }
260
261
    /**
262
     * To time unit time unit.
263
     *
264
     * @param tu the tu
265
     * @return the time unit
266
     */
267
    public static TimeUnit toTimeUnit(final ChronoUnit tu) {
268
        if (tu == null) {
269
            return null;
270
        }
271
        switch (tu) {
272
            case DAYS:
273
                return TimeUnit.DAYS;
274
            case HOURS:
275
                return TimeUnit.HOURS;
276
            case MINUTES:
277
                return TimeUnit.MINUTES;
278
            case SECONDS:
279
                return TimeUnit.SECONDS;
280
            case MICROS:
281
                return TimeUnit.MICROSECONDS;
282
            case MILLIS:
283
                return TimeUnit.MILLISECONDS;
284
            case NANOS:
285
                return TimeUnit.NANOSECONDS;
286
            default:
287
                throw new UnsupportedOperationException("Temporal unit is not supported");
288
        }
289
    }
290
291
    /**
292
     * To chrono unit.
293
     *
294
     * @param tu the tu
295
     * @return the chrono unit
296
     */
297
    public static ChronoUnit toChronoUnit(final TimeUnit tu) {
298
        if (tu == null) {
299
            return null;
300
        }
301
        switch (tu) {
302
            case DAYS:
303
                return ChronoUnit.DAYS;
304
            case HOURS:
305
                return ChronoUnit.HOURS;
306
            case MINUTES:
307
                return ChronoUnit.MINUTES;
308
            case SECONDS:
309
                return ChronoUnit.SECONDS;
310
            case MICROSECONDS:
311
                return ChronoUnit.MICROS;
312
            case MILLISECONDS:
313
                return ChronoUnit.MILLIS;
314
            case NANOSECONDS:
315
                return ChronoUnit.NANOS;
316
            default:
317
                return null;
318
        }
319
    }
320
}
321