How do I create a localized relative timeline like Android alarm clock?
I want to create a similar string as the Android alarm does when setting the alarm.
Example:
- "The alarm is set for 6 days, 10 hours and 9 minutes." - "The alarm is set at 10 o'clock and after 9 minutes."
- "Alarm set within 9 minutes."
I can use:
DateUtils.getRelativeTimeSpanString(alarm.triggerTime,
System.currentTimeMillis(),
DateUtils.MINUTE_IN_MILLIS,
flags).toString();
The result is a message like "in 19 hours . I can't seem to create anything more than " in 19 hours and 5 minutes or "in 5 days, 10 hours and 30 minutes .
I want to figure out how to achieve this, if possible, using Android localized helper classes like DateUtils
. I would really like to be able to stitch together strings that could cause problems in other languages.
source to share
If you are using Eclipse IDE
, try installing GrepCode_Plugin
from this tutorial GrepCode_Plugin
Once you're done using search in GrepCode Search
using android.text.format.DateUtils
. It will list all the files for you. Try downloading the latest version.
Get a method from this file and modify it to suit your needs.
For your reference, I will put the method listed in android 4.1.1_r1
public static CharSequence getRelativeTimeSpanString(long time, long now, long minResolution,
int flags) {
Resources r = Resources.getSystem();
boolean abbrevRelative = (flags & (FORMAT_ABBREV_RELATIVE | FORMAT_ABBREV_ALL)) != 0;
boolean past = (now >= time);
long duration = Math.abs(now - time);
int resId;
long count;
if (duration < MINUTE_IN_MILLIS && minResolution < MINUTE_IN_MILLIS) {
count = duration / SECOND_IN_MILLIS;
if (past) {
if (abbrevRelative) {
resId = com.android.internal.R.plurals.abbrev_num_seconds_ago;
} else {
resId = com.android.internal.R.plurals.num_seconds_ago;
}
} else {
if (abbrevRelative) {
resId = com.android.internal.R.plurals.abbrev_in_num_seconds;
} else {
resId = com.android.internal.R.plurals.in_num_seconds;
}
}
} else if (duration < HOUR_IN_MILLIS && minResolution < HOUR_IN_MILLIS) {
count = duration / MINUTE_IN_MILLIS;
if (past) {
if (abbrevRelative) {
resId = com.android.internal.R.plurals.abbrev_num_minutes_ago;
} else {
resId = com.android.internal.R.plurals.num_minutes_ago;
}
} else {
if (abbrevRelative) {
resId = com.android.internal.R.plurals.abbrev_in_num_minutes;
} else {
resId = com.android.internal.R.plurals.in_num_minutes;
}
}
} else if (duration < DAY_IN_MILLIS && minResolution < DAY_IN_MILLIS) {
count = duration / HOUR_IN_MILLIS;
if (past) {
if (abbrevRelative) {
resId = com.android.internal.R.plurals.abbrev_num_hours_ago;
} else {
resId = com.android.internal.R.plurals.num_hours_ago;
}
} else {
if (abbrevRelative) {
resId = com.android.internal.R.plurals.abbrev_in_num_hours;
} else {
resId = com.android.internal.R.plurals.in_num_hours;
}
}
} else if (duration < WEEK_IN_MILLIS && minResolution < WEEK_IN_MILLIS) {
count = getNumberOfDaysPassed(time, now);
if (past) {
if (abbrevRelative) {
resId = com.android.internal.R.plurals.abbrev_num_days_ago;
} else {
resId = com.android.internal.R.plurals.num_days_ago;
}
} else {
if (abbrevRelative) {
resId = com.android.internal.R.plurals.abbrev_in_num_days;
} else {
resId = com.android.internal.R.plurals.in_num_days;
}
}
} else {
// We know that we won't be showing the time, so it is safe to pass
// in a null context.
return formatDateRange(null, time, time, flags);
}
String format = r.getQuantityString(resId, (int) count);
return String.format(format, count);
}