-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path110.java
More file actions
161 lines (141 loc) · 5.6 KB
/
Copy path110.java
File metadata and controls
161 lines (141 loc) · 5.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
/*******************************************************************************
* Copyright (c) 2017 Intuit
*
* 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.
*******************************************************************************/
package com.intuit.ipp.util;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* Util class to have methods to get the Date instance
*
*/
public final class DateUtils {
/**
* Constructor to have private modifier as it has only static methods
*/
private DateUtils() {
}
/**
* variable DATE_YYYYMMDDTHHMMSS_SSSZ
*/
public static final String DATE_yyyyMMddTHHmmssSSSZ = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
/**
* variable DATE_yyyMMddTHHmmssZ (Date format without milliseconds)
*/
public static final String DATE_yyyMMddTHHmmssZ = "yyyy-MM-dd'T'HH:mm:ssZ";
/**
* variable DATE_yyyyMMddTHHmmssSSSZone
*/
public static final String DATE_yyyyMMddTHHmmssSSSZone = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
/**
* variable DATE_yyyyMMddTHHmmssSSSZone (Date format with milliseconds)
*/
public static final String DATE_yyyMMddTHHmmssZone = "yyyy-MM-dd'T'HH:mm:ss'Z'";
/**
* Method to get the date utils instance
*
* @return DateUtils
*/
public static DateUtils getInstance() {
return new DateUtils();
}
/**
* Method to get the Calendar instance for the given Date format
*
* @param date the date
* @return the Date instance
* @throws java.text.ParseException
* @throws ParseException
*/
/**
* Note: The below method uses date formats that are supported by Java SE 1.6 since we still
* need to support it. Moving forward when we port to Java SE 1.7 please update the code and
* use more generic date formats to make the code more robust.
* Java SE 1.6: http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
* Java SE 1.7: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
*
*/
public static Date getDateFromString(String date) throws ParseException {
DateFormat formatter = new SimpleDateFormat(DATE_yyyyMMddTHHmmssSSSZ);
if (date.charAt(date.length() - 3) == ':' && !date.endsWith("Z")) {
date = date.substring(0, date.lastIndexOf(':')) + date.substring(date.lastIndexOf(':') + 1, date.length());
if (!date.contains(".")) {
formatter = new SimpleDateFormat(DATE_yyyMMddTHHmmssZ);
}
} else if (date.charAt(date.length() - 3) != ':' && !date.endsWith("Z")) {
if (!date.contains(".")) {
formatter = new SimpleDateFormat(DATE_yyyMMddTHHmmssZ);
}
} else if (date.endsWith("Z") && date.contains(".")) {
formatter = new SimpleDateFormat(DATE_yyyyMMddTHHmmssSSSZone);
} else if (date.endsWith("Z") && !date.contains(".")) {
formatter = new SimpleDateFormat(DATE_yyyMMddTHHmmssZone);
}
return formatter.parse(date);
}
/**
* Method to get the current date time Calendar instance
*
* @return the Date instance
* @throws ParseException
*/
public static Date getCurrentDateTime() throws ParseException {
Calendar currentDate = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat(DATE_yyyyMMddTHHmmssSSSZ);
String dateNow = formatter.format(currentDate.getTime());
return getDateFromString(dateNow);
}
/**
* Method to get the Date instance for the given days to be added to the current date
*
* @param noOfDays
* @return the Date instance
* @throws ParseException
*/
public static Date getDateWithNextDays(int noOfDays) throws ParseException {
Calendar currentDate = Calendar.getInstance();
currentDate.add(Calendar.DATE, noOfDays);
SimpleDateFormat formatter = new SimpleDateFormat(DATE_yyyyMMddTHHmmssSSSZ);
String dateNow = formatter.format(currentDate.getTime());
return getDateFromString(dateNow);
}
/**
* Method to get the Date instance for the given days to be subtracted to the current date
*
* @param noOfDays
* @return the Date instance
* @throws ParseException
*/
public static Date getDateWithPrevDays(int noOfDays) throws ParseException {
Calendar currentDate = Calendar.getInstance();
currentDate.add(Calendar.DATE, -noOfDays);
SimpleDateFormat formatter = new SimpleDateFormat(DATE_yyyyMMddTHHmmssSSSZ);
String dateNow = formatter.format(currentDate.getTime());
return getDateFromString(dateNow);
}
/**
* Method to convert the given Date to String format
*
* @param date the java.util.Date
* @return the date in string
* @throws ParseException
*/
public static String getStringFromDateTime(Date date) throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat(DATE_yyyyMMddTHHmmssSSSZone);
return formatter.format(date);
}
}