aboutsummaryrefslogtreecommitdiffstats
path: root/app-c/appc/appc-common/src/main/java/org/openecomp/appc/util/Time.java
blob: 147ae2ca7dc6826d6b5fce82812cf4bca5ba976f (plain)
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
/*-
 * ============LICENSE_START=======================================================
 * openECOMP : APP-C
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights
 * 						reserved.
 * ================================================================================
 * Licensed 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.
 * ============LICENSE_END=========================================================
 */



package org.openecomp.appc.util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
import java.util.SimpleTimeZone;
import java.util.TimeZone;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * This class is a general purpose helper class to augment standard Java time support.
 *
 */

public final class Time {

    /**
     * Logger to log operations
     */
    private static final Logger LOG = LoggerFactory.getLogger(Time.class);

    /**
     * A formatter to be used to format values
     */
    private static SimpleDateFormat dateformatter = null;

    /**
     * The UTC timezone (for UTC or GMT time)
     */
    @SuppressWarnings("nls")
    private static final TimeZone utcTZ = TimeZone.getTimeZone("UTC");

    /**
     * The cached reference to the datatype factory
     */
    private static DatatypeFactory xmlDatatypeFactory = null;

    /**
     * Private default constructor prevents instantiation
     */
    private Time() {
        //
    }

    /**
     * Increments a date by the indicated months, days, hours, minutes, and seconds, and returns the updated date.
     *
     * @param date
     *            The date to be manipulated
     * @param months
     *            The number of months to be added to the date
     * @param days
     *            The number of days to be added to the date
     * @param hours
     *            The number of hours to be added to the date
     * @param minutes
     *            The number of minutes to be added to the date
     * @param seconds
     *            The number of seconds to be added to the date
     * @return The updated date.
     */
    public static Date addTime(final Date date, final int months, final int days, final int hours, final int minutes,
        final int seconds) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, months);
        cal.add(Calendar.DATE, days);
        cal.add(Calendar.HOUR_OF_DAY, hours);
        cal.add(Calendar.MINUTE, minutes);
        cal.add(Calendar.SECOND, seconds);
        return cal.getTime();
    }

    /**
     * Clears the time components of a calendar to zero, leaving the date components unchanged.
     *
     * @param cal
     *            the calendar to be updated
     * @return The updated calendar object
     */
    public static Calendar dateOnly(final Calendar cal) {
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal;
    }

    /**
     * This method returns the local time that corresponds to the end of the current day
     *
     * @return The time that corresponds to the end of the current day, expressed as local time
     */
    public static Date endOfDayLocal() {
        return endOfDayLocal(new Date());
    }

    /**
     * This method returns the last moment of the day for the supplied local time. This is defined as the millisecond
     * before midnight of the current date represented by the local time.
     *
     * @param localTime
     *            The local time for which the last moment of the day is desired.
     * @return The millisecond prior to midnight, local time.
     */
    public static Date endOfDayLocal(final Date localTime) {
        // @sonar:off
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(localTime);
        calendar.set(Calendar.HOUR, 11);
        calendar.set(Calendar.AM_PM, Calendar.PM);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        // @sonar:on

        return calendar.getTime();
    }

    /**
     * The end of the current day and in the current time zone expressed as a UTC time.
     *
     * @return The UTC time that corresponds to the end of the current day
     */
    public static Date endOfDayUTC() {
        return endOfDayUTC(new Date());
    }

    /**
     * Returns the UTC time that corresponds to the end of the day for the local time specified, using the current
     * (default) time zone.
     *
     * @param localTime
     *            The local time for which we are requesting the UTC time that corresponds to the end of the day
     * @return The UTC time that corresponds to the end of the local day specified by the local time.
     */
    public static Date endOfDayUTC(final Date localTime) {
        return endOfDayUTC(localTime, TimeZone.getDefault());
    }

    /**
     * Returns the time expressed in UTC time of the end of the day specified in local time and within the local time
     * zone.
     *
     * @param localTime
     *            The local time for which we will compute the end of the local day, and then convert to UTC time.
     * @param localTimeZone
     *            The time zone that the local time is within.
     * @return The UTC date that corresponds to the end of the day local time and in the local time zone.
     */
    public static Date endOfDayUTC(final Date localTime, final TimeZone localTimeZone) {
        Date endOfDay = endOfDayLocal(localTime);
        return utcDate(endOfDay, localTimeZone);
    }

    /**
     * returns current Date in 'UTC' Timezone
     *
     * @return The current date, expressed in the UTC timezone.
     */
    @SuppressWarnings("nls")
    public static Date getCurrentUTCDate() {

        // This code incorrectly changes the default timezone for the entire JVM in order to compute the UTC
        // date for the current time.

        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
        calendar.setTimeInMillis(utcTime());
        return calendar.getTime();
    }

    /**
     * This method loads and caches the reference to the XML data type factory object.
     *
     * @return The XML Data Type Factory object
     */
    public static DatatypeFactory getDatatypeFactory() {
        if (xmlDatatypeFactory == null) {
            try {
                xmlDatatypeFactory = DatatypeFactory.newInstance();
            } catch (DatatypeConfigurationException e) {
                e.printStackTrace(System.err);
            }
        }
        return xmlDatatypeFactory;
    }

    /**
     * Gives the date-time String based on given Locale and Timezone
     *
     * @param date
     *            The date to be formatted
     * @param locale
     *            The locale that we want to format the value for
     * @param timezone
     *            The time zone that the date is within
     * @return The formatted value
     */
    public static String getDateByLocaleAndTimeZone(final Date date, final Locale locale, final TimeZone timezone) {
        String strDate = null;
        DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, locale);
        df.setTimeZone(timezone);
        synchronized (df) {
            strDate = df.format(date);
        }
        return strDate;
    }

    /**
     * Returns singleton UTC date formatter.
     *
     * @return
     */
    @SuppressWarnings("nls")
    private static SimpleDateFormat getDateFormatter() {
        if (dateformatter == null) {
            dateformatter = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
            dateformatter.setTimeZone(new SimpleTimeZone(SimpleTimeZone.UTC_TIME, "UTC"));
        }
        return dateformatter;
    }

    /**
     * This method returns the local time that corresponds to a given UTC time in the current time zone.
     *
     * @param utcTime
     *            The UTC time for which we desire the equivalent local time in the current time zone.
     * @return The local time that is equivalent to the given UTC time for the current time zone
     */
    public static long localTime(final long utcTime) {
        return localTime(utcTime, TimeZone.getDefault());
    }

    /**
     * This method can be used to get the local time that corresponds to a specific UTC time.
     * <p>
     * This method has a problem since the offset can only be determined by having a local time. So, we take the UTC
     * time and add the raw offset to it to come up with an approximation of the local time. This gives us a local time
     * that we can use to determine what the offset should be, which is what we actually add to the UTC time to get the
     * local time.
     * </p>
     *
     * @param utcTime
     *            The UTC time for which we want to obtain the equivalent local time
     * @param localTZ
     *            The time zone that we want the local time to be within
     * @return The local time for the specified time zone and the given UTC time
     */
    public static long localTime(final long utcTime, final TimeZone localTZ) {
        int offset = localTZ.getOffset(utcTime + localTZ.getRawOffset());
        long result = utcTime + offset;

        return result;
    }

    /**
     * Sets the date components of a calendar to the specified values, leaving the time components unchanged.
     *
     * @param cal
     *            The calendar to be updated
     * @param year
     *            The year to be set
     * @param month
     *            The month to be set
     * @param day
     *            The day to be set
     * @return The updated calendar object
     */
    public static Calendar setDate(final Calendar cal, final int year, final int month, final int day) {
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month);
        cal.set(Calendar.DAY_OF_MONTH, day);
        return cal;
    }

    /**
     * Returns the start of the day expressed in local time for the current local time.
     *
     * @return The start of the day
     */
    public static Date startOfDayLocal() {
        return startOfDayLocal(new Date());
    }

    /**
     * This method returns the date that corresponds to the start of the day local time. The date returned represents
     * midnight of the previous day represented in local time. If the UTC time is desired, use the methods
     * {@link #startOfDayUTC(Date, TimeZone)}, {@link #startOfDayUTC(Date)}, or {@link #startOfDayUTC()}
     *
     * @param localTime
     *            The local date that we wish to compute the start of day for.
     * @return The date that corresponds to the start of the local day
     */
    public static Date startOfDayLocal(final Date localTime) {
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(localTime);
        calendar.set(Calendar.HOUR, 0);
        calendar.set(Calendar.AM_PM, Calendar.AM);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        return calendar.getTime();
    }

    /**
     * This method returns the UTC date that corresponds to the start of the local day based on the current time and the
     * default time zone (the time zone we are running in).
     *
     * @return The start of the local day expressed as a UTC time.
     */
    public static Date startOfDayUTC() {
        return startOfDayUTC(new Date());
    }

    /**
     * This method returns the UTC date that corresponds to the start of the local day specified in the current time
     * zone.
     *
     * @param localTime
     *            The local time to be used to compute the start of the day
     * @return The start of the local day expressed as a UTC time.
     */
    public static Date startOfDayUTC(final Date localTime) {
        return startOfDayUTC(localTime, TimeZone.getDefault());
    }

    /**
     * This method returns the UTC date that corresponds to the start of the local day specified in the local timezone.
     *
     * @param localTime
     *            The local time to be used to compute start of day
     * @param localTimeZone
     *            The time zone that the local time was recorded within
     * @return The corresponding UTC date
     */
    public static Date startOfDayUTC(final Date localTime, final TimeZone localTimeZone) {
        Date startOfDay = startOfDayLocal(localTime);
        return utcDate(startOfDay, localTimeZone);
    }

    /**
     * This method creates and returns an XML timestamp expressed as the current UTC value for the system. The caller
     * does not specify the time value or time zone using this method. This ensures that the timestamp value is always
     * expressed as UTC time.
     *
     * @return The XMLGregorianCalendar that can be used to record the timestamp
     */

    public static XMLGregorianCalendar timestamp() {
        getDatatypeFactory();
        XMLGregorianCalendar ts = xmlDatatypeFactory.newXMLGregorianCalendar();
        GregorianCalendar utc = new GregorianCalendar();
        utc.setTime(utcDate());
        ts.setTimezone(0);
        ts.setYear(utc.get(Calendar.YEAR));
        // Calendar Months are from 0-11 need to +1
        ts.setMonth(utc.get(Calendar.MONTH) + 1);
        ts.setDay(utc.get(Calendar.DAY_OF_MONTH));
        ts.setHour(utc.get(Calendar.HOUR_OF_DAY));
        ts.setMinute(utc.get(Calendar.MINUTE));
        ts.setSecond(utc.get(Calendar.SECOND));
        ts.setMillisecond(utc.get(Calendar.MILLISECOND));
        return ts;
    }

    /**
     * Converts XMLGregorianCalendar to java.util.Date in Java
     *
     * @param calendar
     *            the calendar object to be converted
     * @return The equivalent Date object
     */
    public static Date toDate(final XMLGregorianCalendar calendar) {
        if (calendar == null) {
            return null;
        }
        return calendar.toGregorianCalendar().getTime();
    }

    /**
     * Converts java Date to XMLGregorianCalendar.
     *
     * @param date
     *            The date to convert
     * @return The XMLGregorianCalendar for the specified date
     */
    @SuppressWarnings("nls")
    public static XMLGregorianCalendar toXMLCalendar(final Date date) {
        GregorianCalendar cal = (GregorianCalendar) Calendar.getInstance();
        cal.setTime(date);

        XMLGregorianCalendar xmlCal = null;
        try {
            xmlCal = DatatypeFactory.newInstance().newXMLGregorianCalendar(cal);
        } catch (DatatypeConfigurationException e) {
            LOG.error("toXMLCalendar", e);
        }
        return xmlCal;
    }

    /**
     * Truncates the provided date so that only the date, hours, and minutes portions are significant. This method
     * returns the date with the seconds and milliseconds forced to zero.
     *
     * @param date
     *            The date to truncate
     * @return The date with only the year, month, day, hours, and minutes significant.
     */
    public static Date truncDate(final Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * The UTC date that corresponds to the current date in the local time zone.
     *
     * @return The UTC date for now in the current time zone.
     */
    public static Date utcDate() {
        return new Date();
    }

    /**
     * The UTC date for the specified date in the current (default) time zone.
     *
     * @param date
     *            The local date for which the UTC date is desired.
     * @return The UTC date that corresponds to the date in the current time zone.
     */
    public static Date utcDate(final Date date) {
        TimeZone tz = TimeZone.getDefault();
        return utcDate(date, tz);
    }

    /**
     * Returns the UTC date for the specified date in the specified time zone.
     *
     * @param date
     *            The date for which the UTC date is desired in the specified zone
     * @param tz
     *            The time zone that corresponds to the date to be converted to UTC
     * @return The UTC date that corresponds to the local date in the local time zone.
     */
    public static Date utcDate(final Date date, final TimeZone tz) {
        return new Date(utcTime(date.getTime(), tz));
    }

    /**
     * Format incoming date as string in GMT or UTC.
     *
     * @param dt
     *            The date to be formatted
     * @return The date formatted for UTC timezone
     */
    public static String utcFormat(final Date dt) {
        String strDate = null;
        DateFormat df = getDateFormatter();
        synchronized (df) {
            strDate = df.format(dt);
        }
        return strDate;
    }

    /**
     * Parse previously formated Date object back to a Date object.
     *
     * @param dateStr
     *            The representation of a UTC date as a string
     * @return The date object containing the parsed representation, or null if the representation cannot be parsed
     */
    @SuppressWarnings("nls")
    public static Date utcParse(final String dateStr) {
        String[] adtl = {
            "yyyy-MM-dd"
        };
        return utcParse(dateStr, adtl);
    }

    /**
     * Parse previously formated Date object back to a Date object.
     *
     * @param dateStr
     *            The representation of a UTC date as a string
     * @param adtlFormatStrings
     *            A list of strings that represent additional date format representations to try and parse.
     * @return The date object containing the parsed representation, or null if the representation cannot be parsed
     */
    @SuppressWarnings("nls")
    public static Date utcParse(final String dateStr, String... adtlFormatStrings) {
        if (dateStr != null) {
            // Build the list of formatters starting with the default defined in the class
            List<DateFormat> formats = new ArrayList<>();
            formats.add(getDateFormatter());

            if (adtlFormatStrings != null) {
                for (String s : adtlFormatStrings) {
                    formats.add(new SimpleDateFormat(s));
                }
            }

            // Return the first matching date formatter's result
            for (DateFormat df : formats) {
                df.setTimeZone(utcTZ);
                try {
                    return df.parse(dateStr);
                } catch (ParseException e) {
                    LOG.debug(String.format("IGNORE - Date string [%s] does not fit pattern [%s]", dateStr,
                        df.toString()));
                }
            }
        }
        return null;
    }

    /**
     * This method returns the current time for the UTC timezone
     *
     * @return The time in the UTC time zone that corresponds to the current local time.
     */
    public static long utcTime() {
        return new Date().getTime();
    }

    /**
     * Get the UTC time that corresponds to the given time in the default time zone (current time zone for the system).
     *
     * @param localTime
     *            The time in the current time zone for which the UTC time is desired.
     * @return The UTC time
     */
    public static long utcTime(final long localTime) {
        TimeZone tz = TimeZone.getDefault();
        return utcTime(localTime, tz);
    }

    /**
     * Get the UTC time that corresponds to the given time in the specified timezone.
     * <p>
     * Note that the java <code>getOffset()</code> method works a little counter-intuitive. It returns the offset that
     * would be added to the current UTC time to get the LOCAL time represented by the local time zone. That means to
     * get the UTC time, we need to SUBTRACT this offset from the local time.
     * </p>
     *
     * @param localTime
     *            The time in the specified time zone for which the UTC time is desired.
     * @param localTZ
     *            The time zone which the local time is in.
     * @return The UTC time for the specified local time in the specified local time zone.
     */
    public static long utcTime(final long localTime, final TimeZone localTZ) {
        int offset = localTZ.getOffset(localTime);
        return localTime - offset;

    }

    /**
     * Creates a timestamp value from a time
     *
     * @param utcTime
     *            The UTC time to convert to a timestamp
     * @return The timestamp
     */
    public static Timestamp utcTimestamp(final long utcTime) {
        TimeZone tz = TimeZone.getDefault();
        return new Timestamp(utcTime(utcTime, tz));
    }
}