-
Notifications
You must be signed in to change notification settings - Fork 183
/
DateUtils.java
507 lines (463 loc) · 21.7 KB
/
DateUtils.java
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
package net.danlew.android.joda;
import android.content.Context;
import android.content.res.Resources;
import org.joda.time.DateTime;
import org.joda.time.DateTimeFieldType;
import org.joda.time.DateTimeZone;
import org.joda.time.Days;
import org.joda.time.Duration;
import org.joda.time.Hours;
import org.joda.time.Interval;
import org.joda.time.LocalDate;
import org.joda.time.Minutes;
import org.joda.time.ReadableDuration;
import org.joda.time.ReadableInstant;
import org.joda.time.ReadablePartial;
import org.joda.time.ReadablePeriod;
import org.joda.time.Seconds;
import org.joda.time.Weeks;
import org.joda.time.Years;
/**
* A replacement for android.text.format.DateUtils that uses Joda-Time classes.
*
* It is not a 1:1 implementation of Android's DateUtils. There are a few improvements made:
*
* - Deprecated constants have been removed.
*
* - Constants which are better represented by Joda-Time classes have been removed.
*
* - minResolution has been removed from relative time span methods because they make no sense.
* All it does is remove meaningful information from the string. E.g., it turns
* "in 30 seconds" into "in 0 minutes", or "in 5 hours" into "in 0 days". Having 0 of anything
* doesn't tell the user anything and should not be encouraged.
*
* - "now" has been removed from methods as a parameter. There is (AFAIK) no reason to use any
* time but the current time for now, especially when formatting date/times in relation to now.
*/
public class DateUtils {
// The following FORMAT_* symbols are used for specifying the format of
// dates and times in the formatDateRange method.
public static final int FORMAT_SHOW_TIME = android.text.format.DateUtils.FORMAT_SHOW_TIME;
public static final int FORMAT_SHOW_WEEKDAY = android.text.format.DateUtils.FORMAT_SHOW_WEEKDAY;
public static final int FORMAT_SHOW_YEAR = android.text.format.DateUtils.FORMAT_SHOW_YEAR;
public static final int FORMAT_NO_YEAR = android.text.format.DateUtils.FORMAT_NO_YEAR;
public static final int FORMAT_SHOW_DATE = android.text.format.DateUtils.FORMAT_SHOW_DATE;
public static final int FORMAT_NO_MONTH_DAY = android.text.format.DateUtils.FORMAT_NO_MONTH_DAY;
public static final int FORMAT_NO_NOON = android.text.format.DateUtils.FORMAT_NO_NOON;
public static final int FORMAT_NO_MIDNIGHT = android.text.format.DateUtils.FORMAT_NO_MIDNIGHT;
public static final int FORMAT_ABBREV_TIME = android.text.format.DateUtils.FORMAT_ABBREV_TIME;
public static final int FORMAT_ABBREV_WEEKDAY = android.text.format.DateUtils.FORMAT_ABBREV_WEEKDAY;
public static final int FORMAT_ABBREV_MONTH = android.text.format.DateUtils.FORMAT_ABBREV_MONTH;
public static final int FORMAT_NUMERIC_DATE = android.text.format.DateUtils.FORMAT_NUMERIC_DATE;
public static final int FORMAT_ABBREV_RELATIVE = android.text.format.DateUtils.FORMAT_ABBREV_RELATIVE;
public static final int FORMAT_ABBREV_ALL = android.text.format.DateUtils.FORMAT_ABBREV_ALL;
/**
* We don't want consumers of DateUtils to use this, but we do need it internally to calibrate
* times to UTC for formatting purposes.
*/
private static final int FORMAT_UTC = android.text.format.DateUtils.FORMAT_UTC;
private static final DateTime EPOCH = new DateTime(0, DateTimeZone.UTC);
/**
* Formats a date or a time according to the local conventions.
*
* Since ReadablePartials don't support all fields, we fill in any blanks
* needed for formatting by using the epoch (1970-01-01T00:00:00Z).
*
* See {@link android.text.format.DateUtils#formatDateTime} for full docs.
*
* @param context the context is required only if the time is shown
* @param time a point in time
* @param flags a bit mask of formatting options
* @return a string containing the formatted date/time.
*/
public static String formatDateTime(Context context, ReadablePartial time, int flags) {
return android.text.format.DateUtils.formatDateTime(context, toMillis(time), flags | FORMAT_UTC);
}
/**
* Formats a date or a time according to the local conventions.
*
* See {@link android.text.format.DateUtils#formatDateTime} for full docs.
*
* @param context the context is required only if the time is shown
* @param time a point in time
* @param flags a bit mask of formatting options
* @return a string containing the formatted date/time.
*/
public static String formatDateTime(Context context, ReadableInstant time, int flags) {
return android.text.format.DateUtils.formatDateTime(context, toMillis(time), flags | FORMAT_UTC);
}
/**
* Formats a date or a time range according to the local conventions.
*
* You should ensure that start/end are in the same timezone; formatDateRange()
* doesn't handle start/end in different timezones well.
*
* See {@link android.text.format.DateUtils#formatDateRange} for full docs.
*
* @param context the context is required only if the time is shown
* @param start the start time
* @param end the end time
* @param flags a bit mask of options
* @return a string containing the formatted date/time range
*/
public static String formatDateRange(Context context, ReadablePartial start, ReadablePartial end, int flags) {
return formatDateRange(context, toMillis(start), toMillis(end), flags);
}
/**
* Formats a date or a time range according to the local conventions.
*
* You should ensure that start/end are in the same timezone; formatDateRange()
* doesn't handle start/end in different timezones well.
*
* See {@link android.text.format.DateUtils#formatDateRange} for full docs.
*
* @param context the context is required only if the time is shown
* @param start the start time
* @param end the end time
* @param flags a bit mask of options
* @return a string containing the formatted date/time range
*/
public static String formatDateRange(Context context, ReadableInstant start, ReadableInstant end, int flags) {
return formatDateRange(context, toMillis(start), toMillis(end), flags);
}
private static String formatDateRange(Context context, long startMillis, long endMillis, int flags) {
// Buffer is needed, otherwise end time is off by 1 crucial second; however, don't do this
// if they are already equal (that indicates a point in time rather than a range).
if (startMillis != endMillis) {
endMillis += 1000;
}
return android.text.format.DateUtils.formatDateRange(context, startMillis, endMillis, flags | FORMAT_UTC);
}
private static long toMillis(ReadablePartial time) {
return time.toDateTime(EPOCH).getMillis();
}
private static long toMillis(ReadableInstant time) {
DateTime dateTime = time instanceof DateTime ? (DateTime) time : new DateTime(time);
DateTime utcDateTime = dateTime.withZoneRetainFields(DateTimeZone.UTC);
return utcDateTime.getMillis();
}
/**
* Formats an elapsed time in the form "MM:SS" or "H:MM:SS"
* for display on the call-in-progress screen.
*
* See {@link android.text.format.DateUtils#formatElapsedTime} for full docs.
*
* @param elapsedDuration the elapsed duration
*/
public static String formatElapsedTime(ReadableDuration elapsedDuration) {
return formatElapsedTime(null, elapsedDuration);
}
/**
* Formats an elapsed time in a format like "MM:SS" or "H:MM:SS" (using a form
* suited to the current locale), similar to that used on the call-in-progress
* screen.
*
* See {@link android.text.format.DateUtils#formatElapsedTime} for full docs.
*
* @param recycle {@link StringBuilder} to recycle, or null to use a temporary one.
* @param elapsedDuration the elapsed duration
*/
public static String formatElapsedTime(StringBuilder recycle, ReadableDuration elapsedDuration) {
return android.text.format.DateUtils.formatElapsedTime(recycle,
elapsedDuration.toDuration().toStandardSeconds().getSeconds());
}
/**
* See {@link android.text.format.DateUtils#isToday} for full docs.
*
* @return true if the supplied when is today else false
*/
public static boolean isToday(ReadablePartial time) {
if (!time.isSupported(DateTimeFieldType.dayOfMonth())
|| !time.isSupported(DateTimeFieldType.monthOfYear())
|| !time.isSupported(DateTimeFieldType.year())) {
throw new IllegalArgumentException("isToday() must be passed a ReadablePartial that supports day of " +
"month, month of year and year.");
}
LocalDate localDate = time instanceof LocalDate ? (LocalDate) time : new LocalDate(time);
return LocalDate.now().compareTo(localDate) == 0;
}
/**
* See {@link android.text.format.DateUtils#isToday} for full docs.
*
* @return true if the supplied when is today else false
*/
public static boolean isToday(ReadableInstant time) {
return LocalDate.now().compareTo(new LocalDate(time)) == 0;
}
/**
* Returns a string describing 'time' as a time relative to the current time.
*
* Missing fields from 'time' are filled in with values from the current time.
*
* @see #getRelativeTimeSpanString(Context, ReadableInstant, int)
*/
public static CharSequence getRelativeTimeSpanString(Context context, ReadablePartial time) {
return getRelativeTimeSpanString(context, time.toDateTime(DateTime.now()));
}
/**
* Returns a string describing 'time' as a time relative to the current time.
*
* @see #getRelativeTimeSpanString(Context, ReadableInstant, int)
*/
public static CharSequence getRelativeTimeSpanString(Context context, ReadableInstant time) {
int flags = FORMAT_SHOW_DATE | FORMAT_SHOW_YEAR | FORMAT_ABBREV_MONTH;
return getRelativeTimeSpanString(context, time, flags);
}
/**
* Returns a string describing 'time' as a time relative to the current time.
*
* @see #getRelativeTimeSpanString(Context, ReadableInstant, int)
*/
public static CharSequence getRelativeTimeSpanString(Context context, ReadablePartial time, int flags) {
return getRelativeTimeSpanString(context, time.toDateTime(DateTime.now()), flags);
}
/**
* Returns a string describing 'time' as a time relative to 'now'.
*
* See {@link android.text.format.DateUtils#getRelativeTimeSpanString} for full docs.
*
* @param context the context
* @param time the time to describe
* @param flags a bit mask for formatting options, usually FORMAT_ABBREV_RELATIVE
* @return a string describing 'time' as a time relative to 'now'.
*/
public static CharSequence getRelativeTimeSpanString(Context context, ReadableInstant time, int flags) {
boolean abbrevRelative = (flags & (FORMAT_ABBREV_RELATIVE | FORMAT_ABBREV_ALL)) != 0;
// We set the millis to 0 so we aren't off by a fraction of a second when counting intervals
DateTime now = DateTime.now(time.getZone()).withMillisOfSecond(0);
DateTime timeDt = new DateTime(time).withMillisOfSecond(0);
boolean past = !now.isBefore(timeDt);
Interval interval = past ? new Interval(timeDt, now) : new Interval(now, timeDt);
int resId;
long count;
if (Minutes.minutesIn(interval).isLessThan(Minutes.ONE)) {
count = Seconds.secondsIn(interval).getSeconds();
if (past) {
if (abbrevRelative) {
resId = R.plurals.joda_time_android_abbrev_num_seconds_ago;
}
else {
resId = R.plurals.joda_time_android_num_seconds_ago;
}
}
else {
if (abbrevRelative) {
resId = R.plurals.joda_time_android_abbrev_in_num_seconds;
}
else {
resId = R.plurals.joda_time_android_in_num_seconds;
}
}
}
else if (Hours.hoursIn(interval).isLessThan(Hours.ONE)) {
count = Minutes.minutesIn(interval).getMinutes();
if (past) {
if (abbrevRelative) {
resId = R.plurals.joda_time_android_abbrev_num_minutes_ago;
}
else {
resId = R.plurals.joda_time_android_num_minutes_ago;
}
}
else {
if (abbrevRelative) {
resId = R.plurals.joda_time_android_abbrev_in_num_minutes;
}
else {
resId = R.plurals.joda_time_android_in_num_minutes;
}
}
}
else if (Days.daysIn(interval).isLessThan(Days.ONE)) {
count = Hours.hoursIn(interval).getHours();
if (past) {
if (abbrevRelative) {
resId = R.plurals.joda_time_android_abbrev_num_hours_ago;
}
else {
resId = R.plurals.joda_time_android_num_hours_ago;
}
}
else {
if (abbrevRelative) {
resId = R.plurals.joda_time_android_abbrev_in_num_hours;
}
else {
resId = R.plurals.joda_time_android_in_num_hours;
}
}
}
else if (Weeks.weeksIn(interval).isLessThan(Weeks.ONE)) {
count = Days.daysIn(interval).getDays();
if (past) {
if (abbrevRelative) {
resId = R.plurals.joda_time_android_abbrev_num_days_ago;
}
else {
resId = R.plurals.joda_time_android_num_days_ago;
}
}
else {
if (abbrevRelative) {
resId = R.plurals.joda_time_android_abbrev_in_num_days;
}
else {
resId = R.plurals.joda_time_android_in_num_days;
}
}
}
else {
return formatDateRange(context, time, time, flags);
}
String format = context.getResources().getQuantityString(resId, (int) count);
return String.format(format, count);
}
/**
* Returns a relative time string to display the time expressed by millis.
*
* Missing fields from 'time' are filled in with values from the current time.
*
* See {@link android.text.format.DateUtils#getRelativeTimeSpanString} for full docs.
*
* @param withPreposition If true, the string returned will include the correct
* preposition ("at 9:20am", "on 10/12/2008" or "on May 29").
*/
public static CharSequence getRelativeTimeSpanString(Context ctx, ReadablePartial time, boolean withPreposition) {
return getRelativeTimeSpanString(ctx, time.toDateTime(DateTime.now()), withPreposition);
}
/**
* Returns a relative time string to display the time expressed by millis.
*
* See {@link android.text.format.DateUtils#getRelativeTimeSpanString} for full docs.
*
* @param withPreposition If true, the string returned will include the correct
* preposition ("at 9:20am", "on 10/12/2008" or "on May 29").
*/
public static CharSequence getRelativeTimeSpanString(Context ctx, ReadableInstant time, boolean withPreposition) {
String result;
LocalDate now = LocalDate.now();
LocalDate timeDate = new LocalDate(time);
int prepositionId;
if (Days.daysBetween(now, timeDate).getDays() == 0) {
// Same day
int flags = FORMAT_SHOW_TIME;
result = formatDateRange(ctx, time, time, flags);
prepositionId = R.string.joda_time_android_preposition_for_time;
}
else if (Years.yearsBetween(now, timeDate).getYears() != 0) {
// Different years
int flags = FORMAT_SHOW_DATE | FORMAT_SHOW_YEAR | FORMAT_NUMERIC_DATE;
result = formatDateRange(ctx, time, time, flags);
// This is a date (like "10/31/2008" so use the date preposition)
prepositionId = R.string.joda_time_android_preposition_for_date;
}
else {
// Default
int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
result = formatDateRange(ctx, time, time, flags);
prepositionId = R.string.joda_time_android_preposition_for_date;
}
if (withPreposition) {
result = ctx.getString(prepositionId, result);
}
return result;
}
/**
* Return string describing the time until/elapsed time since 'time' formatted like
* "[relative time/date], [time]".
*
* See {@link android.text.format.DateUtils#getRelativeDateTimeString} for full docs.
*
* @throws IllegalArgumentException if using a ReadablePartial without a time component
* @see #getRelativeDateTimeString(Context, ReadableInstant, ReadablePeriod, int)
*/
public static CharSequence getRelativeDateTimeString(Context context, ReadablePartial time,
ReadablePeriod transitionResolution, int flags) {
if (!time.isSupported(DateTimeFieldType.hourOfDay())
|| !time.isSupported(DateTimeFieldType.minuteOfHour())) {
throw new IllegalArgumentException("getRelativeDateTimeString() must be passed a ReadablePartial that " +
"supports time, otherwise it makes no sense");
}
return getRelativeDateTimeString(context, time.toDateTime(DateTime.now()), transitionResolution, flags);
}
/**
* Return string describing the time until/elapsed time since 'time' formatted like
* "[relative time/date], [time]".
*
* See {@link android.text.format.DateUtils#getRelativeDateTimeString} for full docs.
*
* @param context the context
* @param time some time
* @param transitionResolution the elapsed time (period) at which
* to stop reporting relative measurements. Periods greater
* than this resolution will default to normal date formatting.
* For example, will transition from "6 days ago" to "Dec 12"
* when using Weeks.ONE. If null, defaults to Days.ONE.
* Clamps to min value of Days.ONE, max of Weeks.ONE.
* @param flags flags for getRelativeTimeSpanString() (if duration is less than transitionResolution)
*/
public static CharSequence getRelativeDateTimeString(Context context, ReadableInstant time,
ReadablePeriod transitionResolution, int flags) {
Resources r = context.getResources();
// We set the millis to 0 so we aren't off by a fraction of a second when counting duration
DateTime now = DateTime.now(time.getZone()).withMillisOfSecond(0);
DateTime timeDt = new DateTime(time).withMillisOfSecond(0);
boolean past = !now.isBefore(timeDt);
Duration duration = past ? new Duration(timeDt, now) : new Duration(now, timeDt);
// getRelativeTimeSpanString() doesn't correctly format relative dates
// above a week or exact dates below a day, so clamp
// transitionResolution as needed.
Duration transitionDuration;
Duration minDuration = Days.ONE.toPeriod().toDurationTo(timeDt);
if (transitionResolution == null) {
transitionDuration = minDuration;
}
else {
transitionDuration = past ? transitionResolution.toPeriod().toDurationTo(now) :
transitionResolution.toPeriod().toDurationFrom(now);
Duration maxDuration = Weeks.ONE.toPeriod().toDurationTo(timeDt);
if (transitionDuration.isLongerThan(maxDuration)) {
transitionDuration = maxDuration;
}
else if (transitionDuration.isShorterThan(minDuration)) {
transitionDuration = minDuration;
}
}
CharSequence timeClause = formatDateRange(context, time, time, FORMAT_SHOW_TIME);
String result;
if (!duration.isLongerThan(transitionDuration)) {
CharSequence relativeClause = getRelativeTimeSpanString(context, time, flags);
result = r.getString(R.string.joda_time_android_relative_time, relativeClause, timeClause);
}
else {
CharSequence dateClause = getRelativeTimeSpanString(context, time, false);
result = r.getString(R.string.joda_time_android_date_time, dateClause, timeClause);
}
return result;
}
/**
* Return given duration in a human-friendly format. For example, "4
* minutes" or "1 second". Returns only largest meaningful unit of time,
* from seconds up to hours.
*
* The longest duration it supports is hours.
*
* This method assumes that there are 60 minutes in an hour,
* 60 seconds in a minute and 1000 milliseconds in a second.
* All currently supplied chronologies use this definition.
*/
public static CharSequence formatDuration(Context context, ReadableDuration readableDuration) {
Resources res = context.getResources();
Duration duration = readableDuration.toDuration();
final int hours = (int) duration.getStandardHours();
if (hours != 0) {
return res.getQuantityString(R.plurals.joda_time_android_duration_hours, hours, hours);
}
final int minutes = (int) duration.getStandardMinutes();
if (minutes != 0) {
return res.getQuantityString(R.plurals.joda_time_android_duration_minutes, minutes, minutes);
}
final int seconds = (int) duration.getStandardSeconds();
return res.getQuantityString(R.plurals.joda_time_android_duration_seconds, seconds, seconds);
}
}