@@ -22,7 +22,7 @@ public class DateTimeUtils extends Utils {
22
22
private static SimpleDateFormat simpleISO8601DateWithMillisFormatter = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss.SSSX" , Locale .getDefault ());
23
23
24
24
25
- // PARSE
25
+ // DECODE & ENCODE
26
26
27
27
/**
28
28
* Returns Date object parsed from date string or current date in case of error.
@@ -64,6 +64,42 @@ public static Date decodeDateWithMillisFromIsoString(String dateStr)
64
64
}
65
65
}
66
66
67
+ /**
68
+ * Returns ISO 8601 string formatted from time
69
+ *
70
+ * @param millis
71
+ *
72
+ * @return ISO 8601 String or current date ISO String in case of error
73
+ */
74
+ public static String encodeMillisToIsoString (long millis )
75
+ {
76
+ if (millis < 0 ) {
77
+ return simpleISO8601DateFormatter .format (System .currentTimeMillis ());
78
+ }
79
+
80
+ try {
81
+ return simpleISO8601DateFormatter .format (millis );
82
+ } catch (RuntimeException rte ) {
83
+ return simpleISO8601DateFormatter .format (System .currentTimeMillis ());
84
+ }
85
+ }
86
+
87
+ /**
88
+ * Returns ISO 8601 string formatted from time
89
+ *
90
+ * @param date
91
+ *
92
+ * @return ISO 8601 String or current date ISO String in case of error
93
+ */
94
+ public static String encodeMillisToIsoString (Date date )
95
+ {
96
+ if (date == null ) {
97
+ return simpleISO8601DateFormatter .format (System .currentTimeMillis ());
98
+ }
99
+
100
+ return encodeMillisToIsoString (date .getTime ());
101
+ }
102
+
67
103
// DATE AND TIME
68
104
69
105
/**
@@ -74,7 +110,7 @@ public static Date decodeDateWithMillisFromIsoString(String dateStr)
74
110
*
75
111
* @return formatted date and time or empty string in case of failure
76
112
*/
77
- public static String getFormattedDateAndTimeWithSystemLocale (Context context , Date date )
113
+ public static String getFormattedDateAndTime (Context context , Date date )
78
114
{
79
115
if (checkNull (context ) || checkNull (date )) {
80
116
return "" ;
@@ -83,6 +119,40 @@ public static String getFormattedDateAndTimeWithSystemLocale(Context context, Da
83
119
return DateUtils .formatDateTime (context , date .getTime (), DateUtils .FORMAT_SHOW_YEAR | DateUtils .FORMAT_NUMERIC_DATE | DateUtils .FORMAT_SHOW_TIME | DateUtils .FORMAT_SHOW_DATE | DateUtils .FORMAT_24HOUR );
84
120
}
85
121
122
+ /**
123
+ * Returns formatted date and time according to system locale
124
+ *
125
+ * @param context
126
+ * @param millis
127
+ *
128
+ * @return formatted date and time or empty string in case of failure
129
+ */
130
+ public static String getFormattedDateAndTime (Context context , long millis )
131
+ {
132
+ if (checkNull (context ) || millis < 0 ) {
133
+ return "" ;
134
+ }
135
+
136
+ return DateUtils .formatDateTime (context , millis , DateUtils .FORMAT_SHOW_YEAR | DateUtils .FORMAT_NUMERIC_DATE | DateUtils .FORMAT_SHOW_TIME | DateUtils .FORMAT_SHOW_DATE | DateUtils .FORMAT_24HOUR );
137
+ }
138
+
139
+ /**
140
+ * Returns formatted date and time with date in words and according to system locale
141
+ *
142
+ * @param context
143
+ * @param millis
144
+ *
145
+ * @return formatted date and time or empty string in case of failure
146
+ */
147
+ public static String getFormattedDateAndTimeInWords (Context context , long millis )
148
+ {
149
+ if (checkNull (context ) || millis < 0 ) {
150
+ return "" ;
151
+ }
152
+
153
+ return DateUtils .formatDateTime (context , millis , DateUtils .FORMAT_SHOW_YEAR | DateUtils .FORMAT_SHOW_TIME | DateUtils .FORMAT_SHOW_DATE | DateUtils .FORMAT_24HOUR );
154
+ }
155
+
86
156
/**
87
157
* Returns formatted date and time with date in words and according to system locale
88
158
*
@@ -91,7 +161,7 @@ public static String getFormattedDateAndTimeWithSystemLocale(Context context, Da
91
161
*
92
162
* @return formatted date and time or empty string in case of failure
93
163
*/
94
- public static String getFormattedDateAndTimeInWordsWithSystemLocale (Context context , Date date )
164
+ public static String getFormattedDateAndTimeInWords (Context context , Date date )
95
165
{
96
166
if (checkNull (context ) || checkNull (date )) {
97
167
return "" ;
@@ -108,7 +178,7 @@ public static String getFormattedDateAndTimeInWordsWithSystemLocale(Context cont
108
178
*
109
179
* @return formatted time or empty string in case of failure
110
180
*/
111
- public static String getFormattedJustTimeWithSystemLocale (Context context , Date date )
181
+ public static String getFormattedJustTime (Context context , Date date )
112
182
{
113
183
if (checkNull (context ) || checkNull (date )) {
114
184
return "" ;
@@ -117,6 +187,23 @@ public static String getFormattedJustTimeWithSystemLocale(Context context, Date
117
187
return DateUtils .formatDateTime (context , date .getTime (), DateUtils .FORMAT_SHOW_TIME );
118
188
}
119
189
190
+ /**
191
+ * Returns formatted time according to system locale
192
+ *
193
+ * @param context
194
+ * @param millis
195
+ *
196
+ * @return formatted time or empty string in case of failure
197
+ */
198
+ public static String getFormattedJustTime (Context context , long millis )
199
+ {
200
+ if (checkNull (context ) || millis < 0 ) {
201
+ return "" ;
202
+ }
203
+
204
+ return DateUtils .formatDateTime (context , millis , DateUtils .FORMAT_SHOW_TIME );
205
+ }
206
+
120
207
/**
121
208
* Returns formatted date according to system locale
122
209
*
@@ -125,7 +212,7 @@ public static String getFormattedJustTimeWithSystemLocale(Context context, Date
125
212
*
126
213
* @return formatted date or empty string in case of failure
127
214
*/
128
- public static String getFormattedJustDateWithSystemLocale (Context context , Date date )
215
+ public static String getFormattedJustDate (Context context , Date date )
129
216
{
130
217
if (checkNull (context ) || checkNull (date )) {
131
218
return "" ;
@@ -134,6 +221,57 @@ public static String getFormattedJustDateWithSystemLocale(Context context, Date
134
221
return DateUtils .formatDateTime (context , date .getTime (), DateUtils .FORMAT_SHOW_YEAR | DateUtils .FORMAT_NUMERIC_DATE );
135
222
}
136
223
224
+ /**
225
+ * Returns formatted date according to system locale
226
+ *
227
+ * @param context
228
+ * @param millis
229
+ *
230
+ * @return formatted date or empty string in case of failure
231
+ */
232
+ public static String getFormattedJustDate (Context context , long millis )
233
+ {
234
+ if (checkNull (context ) || millis < 0 ) {
235
+ return "" ;
236
+ }
237
+
238
+ return DateUtils .formatDateTime (context , millis , DateUtils .FORMAT_SHOW_YEAR | DateUtils .FORMAT_NUMERIC_DATE );
239
+ }
240
+
241
+ /**
242
+ * Returns formatted date (just day and month) according to system locale
243
+ *
244
+ * @param context
245
+ * @param date
246
+ *
247
+ * @return formatted date or empty string in case of failure
248
+ */
249
+ public static String getFormattedJustDateDayAndMonth (Context context , Date date )
250
+ {
251
+ if (checkNull (context ) || checkNull (date )) {
252
+ return "" ;
253
+ }
254
+
255
+ return DateUtils .formatDateTime (context , date .getTime (), DateUtils .FORMAT_SHOW_DATE );
256
+ }
257
+
258
+ /**
259
+ * Returns formatted date (just day and month) according to system locale
260
+ *
261
+ * @param context
262
+ * @param millis
263
+ *
264
+ * @return formatted date or empty string in case of failure
265
+ */
266
+ public static String getFormattedJustDateDayAndMonth (Context context , long millis )
267
+ {
268
+ if (checkNull (context ) || millis < 0 ) {
269
+ return "" ;
270
+ }
271
+
272
+ return DateUtils .formatDateTime (context , millis , DateUtils .FORMAT_SHOW_DATE );
273
+ }
274
+
137
275
/**
138
276
* Returns formatted date in words according to system locale
139
277
*
@@ -142,7 +280,7 @@ public static String getFormattedJustDateWithSystemLocale(Context context, Date
142
280
*
143
281
* @return formatted date or empty string in case of failure
144
282
*/
145
- public static String getFormattedJustDateInWordsWithSystemLocale (Context context , Date date )
283
+ public static String getFormattedJustDateInWords (Context context , Date date )
146
284
{
147
285
if (checkNull (context ) || checkNull (date )) {
148
286
return "" ;
@@ -151,6 +289,23 @@ public static String getFormattedJustDateInWordsWithSystemLocale(Context context
151
289
return DateUtils .formatDateTime (context , date .getTime (), DateUtils .FORMAT_SHOW_YEAR );
152
290
}
153
291
292
+ /**
293
+ * Returns formatted date in words according to system locale
294
+ *
295
+ * @param context
296
+ * @param millis
297
+ *
298
+ * @return formatted date or empty string in case of failure
299
+ */
300
+ public static String getFormattedJustDateInWords (Context context , long millis )
301
+ {
302
+ if (checkNull (context ) || millis < 0 ) {
303
+ return "" ;
304
+ }
305
+
306
+ return DateUtils .formatDateTime (context , millis , DateUtils .FORMAT_SHOW_YEAR );
307
+ }
308
+
154
309
/**
155
310
* Returns formatted date in words according to system locale
156
311
*
@@ -160,7 +315,7 @@ public static String getFormattedJustDateInWordsWithSystemLocale(Context context
160
315
*
161
316
* @return formatted date and time interval or empty string in case of failure
162
317
*/
163
- public static String getFormattedDateAndTimeIntervalWithSystemLocale (Context context , Date dateStart , Date dateEnd )
318
+ public static String getFormattedDateAndTimeInterval (Context context , Date dateStart , Date dateEnd )
164
319
{
165
320
if (checkNull (context ) || checkNull (dateStart ) || checkNull (dateEnd )) {
166
321
return "" ;
@@ -184,20 +339,34 @@ public static String getFormattedDateAndTimeIntervalWithSystemLocale(Context con
184
339
185
340
String resultDate = "" ;
186
341
if (startDateInMillis == endDateInMillis ) {
187
- resultDate += getFormattedJustDateWithSystemLocale (context , dateStart );
342
+ resultDate += getFormattedJustDate (context , dateStart );
188
343
resultDate += " " ;
189
- resultDate += getFormattedJustTimeWithSystemLocale (context , dateStart );
344
+ resultDate += getFormattedJustTime (context , dateStart );
190
345
resultDate += " - " ;
191
- resultDate += getFormattedJustTimeWithSystemLocale (context , dateEnd );
346
+ resultDate += getFormattedJustTime (context , dateEnd );
192
347
} else {
193
- resultDate += getFormattedDateAndTimeWithSystemLocale (context , dateStart );
348
+ resultDate += getFormattedDateAndTime (context , dateStart );
194
349
resultDate += " - " ;
195
- resultDate += getFormattedDateAndTimeWithSystemLocale (context , dateEnd );
350
+ resultDate += getFormattedDateAndTime (context , dateEnd );
196
351
}
197
352
198
353
return resultDate ;
199
354
}
200
355
356
+ /**
357
+ * Returns formatted date in words according to system locale
358
+ *
359
+ * @param context
360
+ * @param millisStart
361
+ * @param millisEnd
362
+ *
363
+ * @return formatted date and time interval or empty string in case of failure
364
+ */
365
+ public static String getFormattedDateAndTimeInterval (Context context , long millisStart , long millisEnd )
366
+ {
367
+ return getFormattedDateAndTimeInterval (context , millisStart , millisEnd );
368
+ }
369
+
201
370
/**
202
371
* Returns formatted time duration
203
372
*
@@ -212,7 +381,7 @@ public static String getFormattedTimeDuration(Context context, long milliseconds
212
381
return "" ;
213
382
}
214
383
215
- StringBuilder builder = new StringBuilder ("" );
384
+ StringBuilder builder = new StringBuilder ();
216
385
final long hr = TimeUnit .MILLISECONDS .toHours (milliseconds );
217
386
final long min = TimeUnit .MILLISECONDS .toMinutes (milliseconds ) % 60 ;
218
387
final long sec = TimeUnit .MILLISECONDS .toSeconds (milliseconds ) % 60 ;
@@ -236,4 +405,21 @@ public static String getFormattedTimeDuration(Context context, long milliseconds
236
405
237
406
return builder .toString ();
238
407
}
408
+
409
+ /**
410
+ * Returns formatted time duration
411
+ *
412
+ * @param context
413
+ * @param time
414
+ *
415
+ * @return formatted time duration or empty string in case of failure
416
+ */
417
+ public static String getFormattedTimeDuration (Context context , Date time , boolean withMilliseconds )
418
+ {
419
+ if (checkNull (context ) || checkNull (time )) {
420
+ return "" ;
421
+ }
422
+
423
+ return getFormattedTimeDuration (context , time .getTime (), withMilliseconds );
424
+ }
239
425
}
0 commit comments