DateSorterpublic class DateSorter extends Object Sorts dates into the following groups:
Today
Yesterday
five 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 | Date | mDate | Calendar | mCal |
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.roll(Calendar.DAY_OF_YEAR, -1);
mBins[1] = c.getTimeInMillis(); // Yesterday
c.roll(Calendar.DAY_OF_YEAR, -(NUM_DAYS_AGO - 1));
mBins[2] = c.getTimeInMillis(); // Five days ago
c.roll(Calendar.DAY_OF_YEAR, NUM_DAYS_AGO); // move back to today
c.roll(Calendar.MONTH, -1);
mBins[3] = c.getTimeInMillis(); // One month ago
c.roll(Calendar.MONTH, -1);
mBins[4] = c.getTimeInMillis(); // Over one month ago
// build labels
mLabels[0] = context.getText(com.android.internal.R.string.today).toString();
mLabels[1] = context.getText(com.android.internal.R.string.yesterday).toString();
int resId = com.android.internal.R.plurals.num_days_ago;
String format = resources.getQuantityString(resId, NUM_DAYS_AGO);
mLabels[2] = String.format(format, NUM_DAYS_AGO);
mLabels[3] = context.getText(com.android.internal.R.string.oneMonthDurationPast).toString();
mLabels[4] = context.getText(com.android.internal.R.string.beforeOneMonthDurationPast)
.toString();
|
Methods Summary |
---|
private java.util.Calendar | 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);
return c;
| public long | getBoundary(int index)
return mBins[index];
| public int | getIndex(long time)
// Lame linear search
for (int i = 0; i < DAY_COUNT; i++) {
if (time > mBins[i]) return i;
}
return DAY_COUNT - 1;
| public java.lang.String | getLabel(int index)
return mLabels[index];
|
|