@Test public void testFormatFromSeconds() { final int min = DAY_IN_HOURS * 60 * MINUTE_IN_SECONDS; String timeString = TimeFormatterUtil.format(getApplicationContext(), min); assertThat(timeString, is("1 d")); }
public static String format(final Context context, final long milliseconds) { if (milliseconds < 0) { return ""; } if (milliseconds == 0) { return context.getString(R.string.msdkui_minutes, 0); } final int seconds = (int) (milliseconds / SECOND_IN_MILLISECONDS); final int days = seconds / (HOUR_IN_SECONDS * DAY_IN_HOURS); final int hours = seconds / HOUR_IN_SECONDS % DAY_IN_HOURS; if (days > 0) { if (hours > 0) { return context.getString(R.string.msdkui_days_hours, days, hours); } else { return context.getString(R.string.msdkui_days, days); } } final int minutes = (seconds / MINUTE_IN_SECONDS) % MINUTE_IN_SECONDS; if (hours > 0) { if (minutes > 0) { return context.getString(R.string.msdkui_hours_minutes, hours, minutes); } else { return context.getString(R.string.msdkui_hours, hours); } } if (minutes > 0) { return context.getString(R.string.msdkui_minutes, minutes); } return context.getString(R.string.msdkui_seconds, (seconds / 10) * 10); }
TimeFormatterUtil { public static String format(final Context context, final long milliseconds) { if (milliseconds < 0) { return ""; } if (milliseconds == 0) { return context.getString(R.string.msdkui_minutes, 0); } final int seconds = (int) (milliseconds / SECOND_IN_MILLISECONDS); final int days = seconds / (HOUR_IN_SECONDS * DAY_IN_HOURS); final int hours = seconds / HOUR_IN_SECONDS % DAY_IN_HOURS; if (days > 0) { if (hours > 0) { return context.getString(R.string.msdkui_days_hours, days, hours); } else { return context.getString(R.string.msdkui_days, days); } } final int minutes = (seconds / MINUTE_IN_SECONDS) % MINUTE_IN_SECONDS; if (hours > 0) { if (minutes > 0) { return context.getString(R.string.msdkui_hours_minutes, hours, minutes); } else { return context.getString(R.string.msdkui_hours, hours); } } if (minutes > 0) { return context.getString(R.string.msdkui_minutes, minutes); } return context.getString(R.string.msdkui_seconds, (seconds / 10) * 10); } }
TimeFormatterUtil { public static String format(final Context context, final long milliseconds) { if (milliseconds < 0) { return ""; } if (milliseconds == 0) { return context.getString(R.string.msdkui_minutes, 0); } final int seconds = (int) (milliseconds / SECOND_IN_MILLISECONDS); final int days = seconds / (HOUR_IN_SECONDS * DAY_IN_HOURS); final int hours = seconds / HOUR_IN_SECONDS % DAY_IN_HOURS; if (days > 0) { if (hours > 0) { return context.getString(R.string.msdkui_days_hours, days, hours); } else { return context.getString(R.string.msdkui_days, days); } } final int minutes = (seconds / MINUTE_IN_SECONDS) % MINUTE_IN_SECONDS; if (hours > 0) { if (minutes > 0) { return context.getString(R.string.msdkui_hours_minutes, hours, minutes); } else { return context.getString(R.string.msdkui_hours, hours); } } if (minutes > 0) { return context.getString(R.string.msdkui_minutes, minutes); } return context.getString(R.string.msdkui_seconds, (seconds / 10) * 10); } private  TimeFormatterUtil(); }
TimeFormatterUtil { public static String format(final Context context, final long milliseconds) { if (milliseconds < 0) { return ""; } if (milliseconds == 0) { return context.getString(R.string.msdkui_minutes, 0); } final int seconds = (int) (milliseconds / SECOND_IN_MILLISECONDS); final int days = seconds / (HOUR_IN_SECONDS * DAY_IN_HOURS); final int hours = seconds / HOUR_IN_SECONDS % DAY_IN_HOURS; if (days > 0) { if (hours > 0) { return context.getString(R.string.msdkui_days_hours, days, hours); } else { return context.getString(R.string.msdkui_days, days); } } final int minutes = (seconds / MINUTE_IN_SECONDS) % MINUTE_IN_SECONDS; if (hours > 0) { if (minutes > 0) { return context.getString(R.string.msdkui_hours_minutes, hours, minutes); } else { return context.getString(R.string.msdkui_hours, hours); } } if (minutes > 0) { return context.getString(R.string.msdkui_minutes, minutes); } return context.getString(R.string.msdkui_seconds, (seconds / 10) * 10); } private  TimeFormatterUtil(); static String format(final Context context, final long milliseconds); static String format(final Context context, final int seconds); }
TimeFormatterUtil { public static String format(final Context context, final long milliseconds) { if (milliseconds < 0) { return ""; } if (milliseconds == 0) { return context.getString(R.string.msdkui_minutes, 0); } final int seconds = (int) (milliseconds / SECOND_IN_MILLISECONDS); final int days = seconds / (HOUR_IN_SECONDS * DAY_IN_HOURS); final int hours = seconds / HOUR_IN_SECONDS % DAY_IN_HOURS; if (days > 0) { if (hours > 0) { return context.getString(R.string.msdkui_days_hours, days, hours); } else { return context.getString(R.string.msdkui_days, days); } } final int minutes = (seconds / MINUTE_IN_SECONDS) % MINUTE_IN_SECONDS; if (hours > 0) { if (minutes > 0) { return context.getString(R.string.msdkui_hours_minutes, hours, minutes); } else { return context.getString(R.string.msdkui_hours, hours); } } if (minutes > 0) { return context.getString(R.string.msdkui_minutes, minutes); } return context.getString(R.string.msdkui_seconds, (seconds / 10) * 10); } private  TimeFormatterUtil(); static String format(final Context context, final long milliseconds); static String format(final Context context, final int seconds);  }