@@ -66,6 +66,7 @@ private DateTimeUtil() {
66
66
* Gets the {@link Calendar} as UTC milliseconds from the epoch.
67
67
*
68
68
* @param calendar the calendar to be converted to millis
69
+ *
69
70
* @return the date as UTC milliseconds from the epoch
70
71
*/
71
72
public static double getUtcMillisFromEpoch (Calendar calendar ) {
@@ -79,6 +80,7 @@ public static double getUtcMillisFromEpoch(Calendar calendar) {
79
80
* Gets the date as {@link Calendar}.
80
81
*
81
82
* @param date the date to be returned as {@link Calendar}
83
+ *
82
84
* @return the date as {@link Calendar}
83
85
*/
84
86
public static Calendar getCalendar (Date date ) {
@@ -87,27 +89,72 @@ public static Calendar getCalendar(Date date) {
87
89
return calendar ;
88
90
}
89
91
92
+ /**
93
+ * Gets a default {@link GregorianCalendar}.
94
+ *
95
+ * @return a default {@link GregorianCalendar} using the current time in the default
96
+ * time zone with the default locale
97
+ */
90
98
public static Calendar getCurrentTimeCalendar () {
91
99
return new GregorianCalendar ();
92
100
}
93
101
102
+
103
+ /**
104
+ * Gets current time consistently.
105
+ *
106
+ * @return the time at which it was allocated, measured to the nearest millisecond
107
+ */
94
108
public static Date getCurrentTimeDate () {
95
109
return new Date ();
96
110
}
97
111
112
+ /**
113
+ * Adds the specified amount of days to the given calendar field.
114
+ *
115
+ * @param calendar the calendar field where to add
116
+ * @param days the amount of days to be added
117
+ *
118
+ * @return the time at which it was allocated, measured to the nearest millisecond
119
+ */
98
120
public static Calendar addDaysToCalendar (Calendar calendar , int days ) {
99
121
calendar .add (Calendar .DAY_OF_YEAR , days );
100
122
return calendar ;
101
123
}
102
124
125
+ /**
126
+ * Defines if date is in past.
127
+ *
128
+ * @param date the date to be compared with current date
129
+ *
130
+ * @return <code>true</code> if given date is in past, <code>false</code> instead
131
+ */
103
132
public static boolean isInPast (Date date ) {
104
133
return date .before (getCurrentTimeDate ());
105
134
}
106
135
136
+ /**
137
+ * Gets the number of milliseconds since January 1, 1970, 00:00:00 GMT
138
+ * represented by specified date.
139
+ *
140
+ * @param date the specified date to get time
141
+ *
142
+ * @return the number of milliseconds since January 1, 1970, 00:00:00 GMT
143
+ * represented by the specified date
144
+ */
107
145
public static long getRelativeTime (Date date ) {
108
146
return date .getTime ();
109
147
}
110
148
149
+ /**
150
+ * Adds the specified amount of days to the given date.
151
+ *
152
+ * @param date the specified date to add
153
+ * @param days the amount of days to be added
154
+ *
155
+ * @return a {@link Date} object representing the calendar's time value (millisecond
156
+ * offset from the Epoch)
157
+ */
111
158
public static Date addDaysToDate (Date date , int days ) {
112
159
Calendar cal = new GregorianCalendar ();
113
160
cal .setTime (date );
@@ -116,15 +163,24 @@ public static Date addDaysToDate(Date date, int days) {
116
163
}
117
164
118
165
/**
119
- * Parses passing date with default {@code yyyy-MM-dd} pattern.
166
+ * Parses passing date with default yyyy-MM-dd pattern.
120
167
*
121
168
* @param date is date to be parse
169
+ *
122
170
* @return parse date
123
171
*/
124
172
public static Date parseWithDefaultPattern (String date ) {
125
173
return parse (date , DEFAULT_PATTERN );
126
174
}
127
175
176
+ /**
177
+ * Parses passing date with specified format.
178
+ *
179
+ * @param date the date to be parsed
180
+ * @param format the format of parsing the date
181
+ *
182
+ * @return parsed date
183
+ */
128
184
public static Date parse (String date , String format ) {
129
185
try {
130
186
return initParserSDF (format ).parse (date );
@@ -134,22 +190,52 @@ public static Date parse(String date, String format) {
134
190
}
135
191
136
192
/**
137
- * Format passing date with default {@code yyyy-MM-dd} pattern.
193
+ * Format passing date with default yyyy-MM-dd pattern.
194
+ *
195
+ * @param date the date to be formatted
138
196
*
139
- * @param date is date to be format
140
- * @return format date
197
+ * @return formatted date
141
198
*/
142
199
public static String formatWithDefaultPattern (Date date ) {
143
200
return format (date , DEFAULT_PATTERN );
144
201
}
145
202
203
+ /**
204
+ * Format passing date with specified pattern.
205
+ *
206
+ * @param date date to be formatted
207
+ * @param pattern pattern for format
208
+ *
209
+ * @return formatted date
210
+ */
146
211
public static String format (Date date , String pattern ) {
147
212
return initParserSDF (pattern ).format (date );
148
213
}
149
214
215
+ /**
216
+ * Gets the offset of time zone from UTC
217
+ *
218
+ * @return the offset of time zone from UTC
219
+ *
220
+ * @deprecated Unused and will be removed in the next major release.
221
+ * Use {@link DateTimeUtil#getCurrentTimeZoneOffset(Date)} instead.
222
+ */
223
+ @ Deprecated
150
224
public static long getCurrentTimeZoneOffset () {
225
+ return getCurrentTimeZoneOffset (getCurrentTimeDate ());
226
+ }
227
+
228
+ /**
229
+ * Gets the offset of time zone from UTC at the specified date.
230
+ *
231
+ * @param date the date represented in milliseconds since January 1, 1970 00:00:00 GMT
232
+ *
233
+ * @return the offset of time zone from UTC at the specified date adjusted with the amount
234
+ * of daylight saving.
235
+ */
236
+ public static long getCurrentTimeZoneOffset (Date date ) {
151
237
TimeZone tz = TimeZone .getDefault ();
152
- return tz .getOffset (getCurrentTimeDate () .getTime ());
238
+ return tz .getOffset (date .getTime ());
153
239
}
154
240
155
241
private static DateFormat initParserSDF (String pattern ) {
0 commit comments