Example usage for android.text.format DateUtils getRelativeTimeSpanString

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

Introduction

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

Prototype

public static CharSequence getRelativeTimeSpanString(long time, long now, long minResolution, int flags) 

Source Link

Document

Returns a string describing 'time' as a time relative to 'now'.

Usage

From source file:Main.java

/**
 * Twitter human friendly date./* w  w  w.j a  v  a  2 s . co  m*/
 *
 * @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

/**
 * Get formatted time.//from   ww  w.  j a  v  a2 s  .c o  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:com.hemou.android.util.StrUtils.java

/**
 * Get relative time for date/*from w  ww  .  ja va 2  s  .  c o m*/
 * 
 * @param date
 * @return relative time
 */
public static CharSequence getRelativeTime(final Date date) {
    if (date == null)
        return "--";
    long now = System.currentTimeMillis();
    if (Math.abs(now - date.getTime()) > 60000)
        return DateUtils.getRelativeTimeSpanString(date.getTime(), now, MINUTE_IN_MILLIS,
                FORMAT_SHOW_DATE | FORMAT_SHOW_YEAR | FORMAT_NUMERIC_DATE);
    else
        return "";
}

From source file:com.calgen.udacity.lego.ui.Utils.java

/**
 * @param mCursor needed to fetch data//from  ww w  .j a  v  a2 s.  c  o m
 * @param context needed to fetch string resources
 * @return modified string as [date] by [author]
 */
public static String getModifiedByline(Cursor mCursor, Context context) {
    return String.format(context.getString(R.string.by_line),
            DateUtils
                    .getRelativeTimeSpanString(mCursor.getLong(ArticleLoader.Query.PUBLISHED_DATE),
                            System.currentTimeMillis(), DateUtils.HOUR_IN_MILLIS, DateUtils.FORMAT_ABBREV_ALL)
                    .toString(),
            mCursor.getString(ArticleLoader.Query.AUTHOR));
}

From source file:org.pixmob.droidlink.ui.EventCursorAdapter.java

@Override
public void bindView(View v, Context context, Cursor cursor) {
    final int state = cursor.getInt(cursor.getColumnIndexOrThrow(STATE));
    final String name = cursor.getString(cursor.getColumnIndexOrThrow(NAME));
    String number = cursor.getString(cursor.getColumnIndexOrThrow(NUMBER));
    final long date = cursor.getLong(cursor.getColumnIndexOrThrow(CREATED));

    if (number != null) {
        number = PhoneNumberUtils.formatNumber(number);
    }/* w  ww . ja va  2  s  .c  o m*/

    final int type = cursor.getInt(cursor.getColumnIndexOrThrow(TYPE));
    final Integer typeResourceId = EVENT_ICONS.get(type);

    final CharSequence eventDate = DateUtils.getRelativeTimeSpanString(date, System.currentTimeMillis(),
            DateUtils.MINUTE_IN_MILLIS, DateUtils.FORMAT_ABBREV_RELATIVE);

    final String message = cursor.getString(cursor.getColumnIndexOrThrow(MESSAGE));

    final String eventName;
    final String eventNumber;
    if (number == null) {
        eventName = context.getString(R.string.unknown_number);
        eventNumber = EMPTY;
    } else if (name == null) {
        eventName = number;
        eventNumber = EMPTY;
    } else {
        eventName = name;
        eventNumber = number;
    }

    TextView tv = (TextView) v.findViewById(R.id.event_name);
    tv.setText(eventName);

    tv = (TextView) v.findViewById(R.id.event_number);
    tv.setText(eventNumber);

    tv = (TextView) v.findViewById(R.id.event_date);
    tv.setText(eventDate);

    tv = (TextView) v.findViewById(R.id.event_message);
    tv.setText(message);

    ImageView iv = (ImageView) v.findViewById(R.id.event_icon);
    if (typeResourceId != null) {
        iv.setImageResource(typeResourceId);
        iv.setVisibility(View.VISIBLE);
    } else {
        iv.setVisibility(View.INVISIBLE);
    }

    iv = (ImageView) v.findViewById(R.id.event_state);
    if (state == EventsContract.PENDING_DELETE_STATE) {
        iv.setImageResource(R.drawable.pending_delete);
        iv.setVisibility(View.VISIBLE);
    } else if (state == EventsContract.PENDING_UPLOAD_STATE) {
        iv.setImageResource(R.drawable.pending_upload);
        iv.setVisibility(View.VISIBLE);
    } else {
        iv.setVisibility(View.GONE);
    }

    v.setTag(TAG_ID, cursor.getString(cursor.getColumnIndex(_ID)));
}

From source file:ca.mudar.snoozy.ui.widget.HistoryCursorAdapter.java

@Override
public void bindView(View view, Context context, Cursor cursor) {
    super.bindView(view, context, cursor);

    /*//from   w w w . j a  va 2 s. c  o m
     Get current cursor values
      */
    final boolean isPowerOn = (1 == cursor.getInt(HistoryQuery.IS_POWER_ON));
    final long timestamp = cursor.getLong(HistoryQuery.TIME_STAMP);
    final boolean isFirst = (1 == cursor.getInt(HistoryQuery.IS_FIRST));
    final boolean isLast = (1 == cursor.getInt(HistoryQuery.IS_LAST));
    final int batteryLevel = cursor.getInt(HistoryQuery.BATTERY_LEVEL);

    /*
     Prepare the data
      */
    final String sPowerStatus = mResources.getString(
            isPowerOn ? R.string.history_item_power_connected : R.string.history_item_power_disconnected);
    final String sBatteryLevel = String.format(mResources.getString(R.string.history_item_battery_level),
            batteryLevel);
    final int resPowerStatusColor = mResources
            .getColor(isPowerOn ? R.color.card_row_highlight_color : R.color.card_row_color);

    String sTimestamp;
    String sDay;

    if (DateUtils.isToday(timestamp)) {
        sDay = mResources.getString(R.string.history_item_day_today);
        sTimestamp = (String) DateUtils.formatSameDayTime(timestamp, mMillis, 0, DateFormat.SHORT);
        //            sTimestamp = (String) DateUtils.getRelativeTimeSpanString(timestamp,now, 0, 0);
    } else {
        sDay = (String) DateUtils.getRelativeTimeSpanString(timestamp, mMillis, DateUtils.DAY_IN_MILLIS,
                DateUtils.FORMAT_ABBREV_RELATIVE);
        //            sDay = sDay.substring(0,1).toUpperCase() + sDay.substring(1);
        sTimestamp = mTimeFormat.format(new Date(timestamp));
    }
    sDay = sDay.substring(0, 1).toUpperCase() + sDay.substring(1);

    /*
     Set UI values
     */
    ((TextView) view.findViewById(R.id.history_is_power_on)).setText(sPowerStatus);
    ((TextView) view.findViewById(R.id.history_is_power_on)).setTextColor(resPowerStatusColor);
    ((TextView) view.findViewById(R.id.history_timestamp)).setText(sTimestamp);
    ((TextView) view.findViewById(R.id.history_battery_level)).setText(sBatteryLevel);

    if (isFirst && isLast) {
        ((TextView) view.findViewById(R.id.history_day)).setText(sDay);

        view.findViewById(R.id.history_header).setVisibility(View.VISIBLE);

        view.setBackgroundResource(R.drawable.bg_cards_top_bottom);
    } else if (isLast) {
        ((TextView) view.findViewById(R.id.history_day)).setText(sDay);

        view.findViewById(R.id.history_header).setVisibility(View.VISIBLE);

        view.setBackgroundResource(R.drawable.bg_cards_top);
    } else if (isFirst) {
        view.findViewById(R.id.history_header).setVisibility(View.GONE);

        view.setBackgroundResource(R.drawable.bg_cards_bottom);
    } else {
        view.findViewById(R.id.history_header).setVisibility(View.GONE);

        view.setBackgroundResource(R.drawable.bg_cards_middle);
    }
}

From source file:com.csipsimple.ui.messages.MessageAdapter.java

@Override
public void bindView(View view, Context context, Cursor cursor) {
    final MessageListItemViews tagView = (MessageListItemViews) view.getTag();

    SipMessage msg = new SipMessage(cursor);

    String number = msg.getRemoteNumber();
    long date = msg.getDate();
    String subject = msg.getBodyContent();
    String errorTxt = msg.getErrorContent();
    String mimeType = msg.getMimeType();
    int type = msg.getType();

    String timestamp = "";
    if (System.currentTimeMillis() - date > 1000 * 60 * 60 * 24) {
        // If it was recieved one day ago or more display relative
        // timestamp - SMS like behavior
        int flags = DateUtils.FORMAT_ABBREV_RELATIVE;
        timestamp = (String) DateUtils.getRelativeTimeSpanString(date, System.currentTimeMillis(),
                DateUtils.MINUTE_IN_MILLIS, flags);
    } else {//from w  w  w  .  j  a  va  2 s. c  om
        // If it has been recieved recently show time of reception - IM
        // like behavior
        timestamp = dateFormatter.format(new Date(date));
    }

    tagView.dateView.setText(timestamp);

    // Delivery state
    if (type == SipMessage.MESSAGE_TYPE_QUEUED) {
        tagView.deliveredIndicator.setVisibility(View.VISIBLE);
        tagView.deliveredIndicator.setImageResource(R.drawable.ic_email_pending);
        tagView.deliveredIndicator.setContentDescription(mContext.getString(R.string.status_pending));
    } else if (type == SipMessage.MESSAGE_TYPE_FAILED) {
        tagView.deliveredIndicator.setVisibility(View.VISIBLE);
        tagView.deliveredIndicator.setImageResource(R.drawable.ic_sms_mms_not_delivered);
        tagView.deliveredIndicator
                .setContentDescription(mContext.getString(R.string.undelivered_msg_dialog_title));
    } else {
        tagView.deliveredIndicator.setVisibility(View.GONE);
        tagView.deliveredIndicator.setContentDescription("");
    }

    if (TextUtils.isEmpty(errorTxt)) {
        tagView.errorView.setVisibility(View.GONE);
    } else {
        tagView.errorView.setVisibility(View.VISIBLE);
        tagView.errorView.setText(errorTxt);
    }

    // Subject
    tagView.contentView.setText(formatMessage(number, subject, mimeType));

    if (msg.isOutgoing()) {
        setPhotoSide(tagView, ArrowPosition.LEFT);

        // Photo
        tagView.quickContactView.assignContactUri(personalInfo.contactContentUri);
        ContactsAsyncHelper.updateImageViewWithContactPhotoAsync(mContext,
                tagView.quickContactView.getImageView(), personalInfo, R.drawable.ic_contact_picture_holo_dark);

    } else {
        setPhotoSide(tagView, ArrowPosition.RIGHT);

        // Contact
        CallerInfo info = CallerInfo.getCallerInfoFromSipUri(mContext, msg.getFullFrom());

        // Photo
        tagView.quickContactView.assignContactUri(info.contactContentUri);
        ContactsAsyncHelper.updateImageViewWithContactPhotoAsync(mContext,
                tagView.quickContactView.getImageView(), info, R.drawable.ic_contact_picture_holo_dark);
    }

}

From source file:com.abcvoipsip.ui.messages.MessageAdapter.java

@Override
public void bindView(View view, Context context, Cursor cursor) {
    final MessageListItemViews tagView = (MessageListItemViews) view.getTag();

    SipMessage msg = new SipMessage(cursor);

    String number = msg.getRemoteNumber();
    long date = msg.getDate();
    String subject = msg.getBodyContent();
    String errorTxt = msg.getErrorContent();
    String mimeType = msg.getMimeType();
    int type = msg.getType();

    String timestamp = "";
    if (System.currentTimeMillis() - date > 1000 * 60 * 60 * 24) {
        // If it was recieved one day ago or more display relative
        // timestamp - SMS like behavior
        int flags = DateUtils.FORMAT_ABBREV_RELATIVE;
        timestamp = (String) DateUtils.getRelativeTimeSpanString(date, System.currentTimeMillis(),
                DateUtils.MINUTE_IN_MILLIS, flags);
    } else {/*  w w w .ja  va 2s . co  m*/
        // If it has been recieved recently show time of reception - IM
        // like behavior
        timestamp = dateFormatter.format(new Date(date));
    }

    tagView.dateView.setText(timestamp);

    // Delivery state
    if (type == SipMessage.MESSAGE_TYPE_QUEUED) {
        tagView.deliveredIndicator.setVisibility(View.VISIBLE);
        tagView.deliveredIndicator.setImageResource(R.drawable.ic_email_pending);
        tagView.deliveredIndicator.setContentDescription(mContext.getString(R.string.status_pending));
    } else if (type == SipMessage.MESSAGE_TYPE_FAILED) {
        tagView.deliveredIndicator.setVisibility(View.VISIBLE);
        tagView.deliveredIndicator.setImageResource(R.drawable.ic_sms_mms_not_delivered);
        tagView.deliveredIndicator
                .setContentDescription(mContext.getString(R.string.undelivered_msg_dialog_title));
    } else {
        tagView.deliveredIndicator.setVisibility(View.GONE);
        tagView.deliveredIndicator.setContentDescription("");
    }

    if (TextUtils.isEmpty(errorTxt)) {
        tagView.errorView.setVisibility(View.GONE);
    } else {
        tagView.errorView.setVisibility(View.VISIBLE);
        tagView.errorView.setText(errorTxt);
    }

    // Subject
    tagView.contentView.setText(formatMessage(number, subject, mimeType));

    if (msg.isOutgoing()) {
        setPhotoSide(tagView, ArrowPosition.LEFT);

        // Photo
        tagView.quickContactView.assignContactUri(personalInfo.contactContentUri);
        ContactsAsyncHelper.updateImageViewWithContactPhotoAsync(mContext,
                tagView.quickContactView.getImageView(), personalInfo,
                SipHome.USE_LIGHT_THEME ? R.drawable.ic_contact_picture_holo_light
                        : R.drawable.ic_contact_picture_holo_dark);

    } else {
        setPhotoSide(tagView, ArrowPosition.RIGHT);

        // Contact
        CallerInfo info = CallerInfo.getCallerInfoFromSipUri(mContext, msg.getFullFrom());

        // Photo
        tagView.quickContactView.assignContactUri(info.contactContentUri);
        ContactsAsyncHelper.updateImageViewWithContactPhotoAsync(mContext,
                tagView.quickContactView.getImageView(), info,
                SipHome.USE_LIGHT_THEME ? R.drawable.ic_contact_picture_holo_light
                        : R.drawable.ic_contact_picture_holo_dark);
    }

}

From source file:com.newcell.calltext.ui.messages.MessageAdapter.java

@Override
public void bindView(View view, Context context, Cursor cursor) {
    final MessageListItemViews tagView = (MessageListItemViews) view.getTag();

    SipMessage msg = new SipMessage(cursor);

    String number = msg.getRemoteNumber();
    long date = msg.getDate();
    String subject = msg.getBodyContent();
    String errorTxt = msg.getErrorContent();
    String mimeType = msg.getMimeType();
    int type = msg.getType();

    String timestamp = "";
    if (System.currentTimeMillis() - date > 1000 * 60 * 60 * 24) {
        // If it was received one day ago or more display relative
        // timestamp - SMS like behavior
        int flags = DateUtils.FORMAT_ABBREV_RELATIVE;
        timestamp = (String) DateUtils.getRelativeTimeSpanString(date, System.currentTimeMillis(),
                DateUtils.MINUTE_IN_MILLIS, flags);
    } else {//from   ww w . ja  v  a  2s . com
        // If it has been received recently show time of reception - IM
        // like behavior
        timestamp = dateFormatter.format(new Date(date));
    }

    tagView.dateView.setText(timestamp);

    // Delivery state
    if (type == SipMessage.MESSAGE_TYPE_QUEUED) {
        tagView.deliveredIndicator.setVisibility(View.VISIBLE);
        tagView.deliveredIndicator.setImageResource(R.drawable.ic_email_pending);
        tagView.deliveredIndicator.setContentDescription(mContext.getString(R.string.status_pending));
    } else if (type == SipMessage.MESSAGE_TYPE_FAILED) {
        tagView.deliveredIndicator.setVisibility(View.VISIBLE);
        tagView.deliveredIndicator.setImageResource(R.drawable.ic_sms_mms_not_delivered);
        tagView.deliveredIndicator
                .setContentDescription(mContext.getString(R.string.undelivered_msg_dialog_title));
    } else {
        tagView.deliveredIndicator.setVisibility(View.GONE);
        tagView.deliveredIndicator.setContentDescription("");
    }

    if (TextUtils.isEmpty(errorTxt)) {
        tagView.errorView.setVisibility(View.GONE);
    } else {
        tagView.errorView.setVisibility(View.VISIBLE);
        tagView.errorView.setText(errorTxt);
    }

    // Subject
    tagView.contentView.setText(formatMessage(number, subject, mimeType));

    if (msg.isOutgoing()) {
        setPhotoSide(tagView, ArrowPosition.LEFT);

        // Photo
        tagView.quickContactView.assignContactUri(personalInfo.contactContentUri);
        ContactsAsyncHelper.updateImageViewWithContactPhotoAsync(mContext,
                tagView.quickContactView.getImageView(), personalInfo, R.drawable.ic_contact_picture_holo_dark);

    } else {
        setPhotoSide(tagView, ArrowPosition.RIGHT);

        // Contact
        CallerInfo info = CallerInfo.getCallerInfoFromSipUri(mContext, msg.getFullFrom());

        // Photo
        tagView.quickContactView.assignContactUri(info.contactContentUri);
        ContactsAsyncHelper.updateImageViewWithContactPhotoAsync(mContext,
                tagView.quickContactView.getImageView(), info, R.drawable.ic_contact_picture_holo_dark);
    }

}

From source file:com.todoroo.astrid.notes.CommentsController.java

/** Helper method to set the contents and visibility of each field */
private void bindView(View view, NoteOrUpdate item) {
    // name/*  w  w w.  j  a  v a  2s .  com*/
    final TextView nameView = (TextView) view.findViewById(R.id.title);
    {
        nameView.setText(item.title);
        Linkify.addLinks(nameView, Linkify.ALL);
    }

    // date
    final TextView date = (TextView) view.findViewById(R.id.date);
    {
        CharSequence dateString = DateUtils.getRelativeTimeSpanString(item.createdAt, DateUtilities.now(),
                DateUtils.MINUTE_IN_MILLIS, DateUtils.FORMAT_ABBREV_RELATIVE);
        date.setText(dateString);
    }

    // picture
    final ImageView commentPictureView = (ImageView) view.findViewById(R.id.comment_picture);
    setupImagePopupForCommentView(view, commentPictureView, item.commentBitmap, activity);
}