Android Utililty Methods Long to Date Convert

List of utility methods to do Long to Date Convert

Description

The list of methods to do Long to Date Convert are organized into topic(s).

Method

longdaysSinceTimestamp(long timestamp)
Determine days difference since timestamp to current time if timestamp is equal to Long.MAX_VALUE then return Long.MAX_VALUE
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
long nowTime = cal.getTimeInMillis();
long dayTime = 24 * 60 * 60 * 1000;
long days;
...
StringencodeDate(long date)
Encode a long date to string value in Z format (see RFC 3339)
Time t = new Time(UTC.getID());
t.set(date);
return t.format3339(false);
Stringformat(String aFormat, long aDate)
format
SimpleDateFormat format = new SimpleDateFormat(aFormat);
return format.format(new Date(aDate));
CharSequenceformatDuration(Context context, long millis)
Return given duration in a human-friendly format.
final Resources res = context.getResources();
if (millis >= DateUtils.HOUR_IN_MILLIS) {
    final int hours = (int) (millis / DateUtils.HOUR_IN_MILLIS);
    final int minutes = (int) (millis % DateUtils.HOUR_IN_MILLIS / DateUtils.MINUTE_IN_MILLIS);
    if (minutes == 0) {
        return res.getQuantityString(R.plurals.duration_hours,
                hours, hours);
    return res.getQuantityString(R.plurals.duration_hours, hours,
            hours)
            + " "
            + res.getQuantityString(R.plurals.duration_minutes,
                    minutes, minutes);
} else if (millis >= DateUtils.MINUTE_IN_MILLIS) {
    final int minutes = (int) ((millis + 30000) / DateUtils.MINUTE_IN_MILLIS);
    return res.getQuantityString(R.plurals.duration_minutes,
            minutes, minutes);
} else {
    final int seconds = (int) ((millis + 500) / DateUtils.SECOND_IN_MILLIS);
    return res.getQuantityString(R.plurals.duration_seconds,
            seconds, seconds);
CharSequenceformatDurationShort(Context context, long millis)
Return given duration in a human-friendly 'short' format.
final Resources res = context.getResources();
if (millis >= DateUtils.HOUR_IN_MILLIS) {
    final int hours = (int) (millis / DateUtils.HOUR_IN_MILLIS);
    final int minutes = (int) (millis % DateUtils.HOUR_IN_MILLIS / DateUtils.MINUTE_IN_MILLIS);
    if (minutes == 0) {
        return res.getString(R.string.duration_short_hours, hours);
    return res.getString(R.string.duration_short_hours, hours)
...
StringformatPublishDaysAgo(long timestamp)
format Publish Days Ago
long days = daysSinceTimestamp(timestamp);
String dayString;
if (days == Long.MAX_VALUE) {
    dayString = "Never Published";
} else {
    if (days < 0) {
        if (days == -1) {
            dayString = new SimpleDateFormat("'Tomorrow at' HH:mm",
...
StringformatTime(final long time)
format Time
return DateFormat.format(timeFormatString, time).toString();
StringformatToCountdown(long millis)
format To Countdown
millis = (long) Math.floor(millis / 1000);
String sec = "" + (millis % 60);
millis -= millis % 60;
millis /= 60;
String mins = "" + (millis % 60);
millis -= millis % 60;
millis /= 60;
long hrs = millis;
...
StringfullFromUtc(long milliseconds)
Convert to "yyyy-HH-dd HH:mm:ss" format from millisconds (epoch time)
Date date = dateFromUtc(milliseconds);
return toFullDate(date);
StringgenerateFileName(long sysTime)
Generate file name from system time in the format "yyyyMMdd-HHmmss-SSS",
return new SimpleDateFormat("yyyyMMdd-HHmmss-SSS", Locale.US)
        .format(new Date(sysTime));