DateSorterpublic class DateSorter extends Object Sorts dates into the following groups:
Today
Yesterday
seven days ago
one month ago
older than a month ago |
Fields Summary |
---|
private static final String | LOGTAG | public static final int | DAY_COUNTmust be >= 3 | private long[] | mBins | private String[] | mLabels | private static final int | NUM_DAYS_AGO |
Constructors Summary |
---|
public DateSorter(android.content.Context context)
Resources resources = context.getResources();
Calendar c = Calendar.getInstance();
beginningOfDay(c);
// Create the bins
mBins[0] = c.getTimeInMillis(); // Today
c.add(Calendar.DAY_OF_YEAR, -1);
mBins[1] = c.getTimeInMillis(); // Yesterday
c.add(Calendar.DAY_OF_YEAR, -(NUM_DAYS_AGO - 1));
mBins[2] = c.getTimeInMillis(); // Five days ago
c.add(Calendar.DAY_OF_YEAR, NUM_DAYS_AGO); // move back to today
c.add(Calendar.MONTH, -1);
mBins[3] = c.getTimeInMillis(); // One month ago
// build labels
Locale locale = resources.getConfiguration().locale;
if (locale == null) {
locale = Locale.getDefault();
}
LocaleData localeData = LocaleData.get(locale);
mLabels[0] = localeData.today;
mLabels[1] = localeData.yesterday;
int resId = com.android.internal.R.plurals.last_num_days;
String format = resources.getQuantityString(resId, NUM_DAYS_AGO);
mLabels[2] = String.format(format, NUM_DAYS_AGO);
mLabels[3] = context.getString(com.android.internal.R.string.last_month);
mLabels[4] = context.getString(com.android.internal.R.string.older);
|
Methods Summary |
---|
private void | beginningOfDay(java.util.Calendar c)Calcuate 12:00am by zeroing out hour, minute, second, millisecond
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
| public long | getBoundary(int index)
int lastDay = DAY_COUNT - 1;
// Error case
if (index < 0 || index > lastDay) index = 0;
// Since this provides a lower boundary on dates that will be included
// in the given bin, provide the smallest value
if (index == lastDay) return Long.MIN_VALUE;
return mBins[index];
| public int | getIndex(long time)
int lastDay = DAY_COUNT - 1;
for (int i = 0; i < lastDay; i++) {
if (time > mBins[i]) return i;
}
return lastDay;
| public java.lang.String | getLabel(int index)
if (index < 0 || index >= DAY_COUNT) return "";
return mLabels[index];
|
|