Example usage for android.text.format DateUtils MINUTE_IN_MILLIS

List of usage examples for android.text.format DateUtils MINUTE_IN_MILLIS

Introduction

In this page you can find the example usage for android.text.format DateUtils MINUTE_IN_MILLIS.

Prototype

long MINUTE_IN_MILLIS

To view the source code for android.text.format DateUtils MINUTE_IN_MILLIS.

Click Source Link

Usage

From source file:Main.java

public static String formatRelativeDate(Context dateContext, Date date) {
    return DateUtils.getRelativeDateTimeString(dateContext, date.getTime(), DateUtils.MINUTE_IN_MILLIS,
            DateUtils.DAY_IN_MILLIS * 2, 0).toString();
}

From source file:Main.java

/**
 * Twitter human friendly date.//from  ww w. ja va2s . c  om
 *
 * @return the string
 */
public static CharSequence humanFriendlyDate(Long created) {
    return DateUtils.getRelativeTimeSpanString(created, System.currentTimeMillis(), DateUtils.MINUTE_IN_MILLIS,
            DateUtils.FORMAT_ABBREV_RELATIVE);
}

From source file:Main.java

public static String getTimeDiff(long otherTime) {
    long currentTime = Calendar.getInstance().getTimeInMillis();
    String timeDiff = DateUtils.getRelativeTimeSpanString(otherTime, currentTime, DateUtils.MINUTE_IN_MILLIS)
            .toString();//from w w  w . java  2s .  com
    if (timeDiff.equals("0 minutes ago")) {
        return "<1 minute ago";
    }
    return timeDiff;
}

From source file:Main.java

public static String getRelativeTimeDiff(Date date) {
    String timeDiff = DateUtils/*from   ww w .ja  v  a2  s  .co  m*/
            .getRelativeTimeSpanString(date.getTime(), System.currentTimeMillis(), DateUtils.MINUTE_IN_MILLIS)
            .toString();

    if (timeDiff.contains("0 ")) {
        timeDiff = "in few seconds ago";
    }

    return timeDiff;
}

From source file:Main.java

public static void appendGmtOffset(StringBuilder sb, final int gmtOffset) {
    sb.append("GMT");

    if (gmtOffset < 0) {
        sb.append('-');
    } else {//from w w w  . j ava  2  s .  c o  m
        sb.append('+');
    }

    final int p = Math.abs(gmtOffset);
    sb.append(p / DateUtils.HOUR_IN_MILLIS); // Hour

    final int min = (p / (int) DateUtils.MINUTE_IN_MILLIS) % 60;
    if (min != 0) { // Show minutes if non-zero
        sb.append(':');
        if (min < 10) {
            sb.append('0');
        }
        sb.append(min);
    }
}

From source file:Main.java

/**
 * Get formatted time.//from   ww  w  .jav a2s. co  m
 *
 * @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();
}

From source file:Main.java

public static int hoursBetween(Date d1, Date d2) {
    return (int) ((d2.getTime() - d1.getTime()) / DateUtils.MINUTE_IN_MILLIS);
}

From source file:com.twitter.sdk.android.tweetui.TweetDateUtils.java

/**
 * This method is not thread safe. It has been modified from the original to not rely on global
 * time state. If a timestamp is in the future we return it as an absolute date string. Within
 * the same second we return 0s/* w  w w .ja  v  a 2  s .  com*/
 *
 * @param res resource
 * @param currentTimeMillis timestamp for offset
 * @param timestamp timestamp
 * @return the relative time string
 */
static String getRelativeTimeString(Resources res, long currentTimeMillis, long timestamp) {
    final long diff = currentTimeMillis - timestamp;
    if (diff >= 0) {
        if (diff < DateUtils.MINUTE_IN_MILLIS) { // Less than a minute ago
            final int secs = (int) (diff / 1000);
            return res.getQuantityString(R.plurals.tw__time_secs, secs, secs);
        } else if (diff < DateUtils.HOUR_IN_MILLIS) { // Less than an hour ago
            final int mins = (int) (diff / DateUtils.MINUTE_IN_MILLIS);
            return res.getQuantityString(R.plurals.tw__time_mins, mins, mins);
        } else if (diff < DateUtils.DAY_IN_MILLIS) { // Less than a day ago
            final int hours = (int) (diff / DateUtils.HOUR_IN_MILLIS);
            return res.getQuantityString(R.plurals.tw__time_hours, hours, hours);
        } else {
            final Calendar now = Calendar.getInstance();
            now.setTimeInMillis(currentTimeMillis);
            final Calendar c = Calendar.getInstance();
            c.setTimeInMillis(timestamp);
            final Date d = new Date(timestamp);

            if (now.get(Calendar.YEAR) == c.get(Calendar.YEAR)) {
                // Same year
                return RELATIVE_DATE_FORMAT.formatShortDateString(res, d);
            } else {
                // Outside of our year
                return RELATIVE_DATE_FORMAT.formatLongDateString(res, d);
            }
        }
    }
    return RELATIVE_DATE_FORMAT.formatLongDateString(res, new Date(timestamp));
}

From source file:ca.hoogit.garagepi.Utils.Helpers.java

/**
 * Create a relative from now time string
 *
 * @param timestamp Time since epoch/*from   w w w.  j a  v  a 2  s  . co  m*/
 * @return Formatted string ie. "2 Minutes from now"
 */
public static String epochToFromNow(long timestamp) {
    return DateUtils
            .getRelativeTimeSpanString(timestamp, System.currentTimeMillis(), DateUtils.MINUTE_IN_MILLIS)
            .toString();
}

From source file:com.liferay.alerts.model.Alert.java

public String getFormattedTimestamp() {
    String formattedTimestamp;//from   w  w  w . j av  a 2 s  .c o  m
    long elapsedTime = System.currentTimeMillis() - _timestamp;

    if (elapsedTime < DateUtils.MINUTE_IN_MILLIS) {
        formattedTimestamp = String.format("%ds", TimeUnit.MILLISECONDS.toSeconds(elapsedTime));
    } else if (elapsedTime < DateUtils.HOUR_IN_MILLIS) {
        formattedTimestamp = String.format("%dm", TimeUnit.MILLISECONDS.toMinutes(elapsedTime));
    } else if (elapsedTime < DateUtils.DAY_IN_MILLIS) {
        formattedTimestamp = String.format("%dh", TimeUnit.MILLISECONDS.toHours(elapsedTime));
    } else {
        formattedTimestamp = String.format("%dd", TimeUnit.MILLISECONDS.toDays(elapsedTime));
    }

    return formattedTimestamp;
}