Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.text.format.DateUtils;
import android.text.format.Time;

public class Main {
    /**
     * Get formatted time.
     *
     * @param publishedTime The published time in millis.
     *
     * @return The formatted time.
     */
    static public String getFormattedTime(long publishedTime) {
        // This is copied from RecentCallsListActivity.java

        long now = System.currentTimeMillis();

        // Set the date/time field by mixing relative and absolute times.
        int flags = DateUtils.FORMAT_ABBREV_ALL;

        if (!DateUtils.isToday(publishedTime)) {
            // DateUtils.getRelativeTimeSpanString doesn't consider the nature
            // days comparing with DateUtils.getRelativeDayString. Override the
            // real date to implement the requirement.

            Time time = new Time();
            time.set(now);
            long gmtOff = time.gmtoff;
            int days = Time.getJulianDay(publishedTime, gmtOff) - Time.getJulianDay(now, gmtOff);

            // Set the delta from now to get the correct display
            publishedTime = now + days * DateUtils.DAY_IN_MILLIS;
        } else if (publishedTime > now && (publishedTime - now) < DateUtils.HOUR_IN_MILLIS) {
            // Avoid e.g. "1 minute left" when publish time is "07:00" and
            // current time is "06:58"
            publishedTime += DateUtils.MINUTE_IN_MILLIS;
        }

        return (DateUtils.getRelativeTimeSpanString(publishedTime, now, DateUtils.MINUTE_IN_MILLIS, flags))
                .toString();
    }
}