Example usage for android.text.format DateUtils getRelativeDateTimeString

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

Introduction

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

Prototype

public static CharSequence getRelativeDateTimeString(Context c, long time, long minResolution,
        long transitionResolution, int flags) 

Source Link

Document

Return string describing the elapsed time since startTime formatted like "[relative time/date], [time]".

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:com.ferjuarez.androidthingsdemo.PhotoEntryAdapter.java

@Override
protected void populateViewHolder(DoorbellEntryViewHolder viewHolder, PhotoEntry model, int position) {
    // Display the timestamp
    if (model != null && model.getTimestamp() != null) {
        CharSequence prettyTime = DateUtils.getRelativeDateTimeString(mApplicationContext, model.getTimestamp(),
                DateUtils.SECOND_IN_MILLIS, DateUtils.WEEK_IN_MILLIS, 0);
        viewHolder.time.setText(prettyTime);

        // Display the image
        if (model.getThumbnail() != null) {
            // Decode image data encoded by the Cloud Vision library
            byte[] imageBytes = Base64.decode(model.getThumbnail(), Base64.NO_WRAP | Base64.URL_SAFE);
            Bitmap bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
            if (bitmap != null) {
                viewHolder.image.setImageBitmap(bitmap);
            } else {
                Drawable placeholder = ContextCompat.getDrawable(mApplicationContext, R.mipmap.ic_launcher);
                viewHolder.image.setImageDrawable(placeholder);
            }/*from   www . ja  v a 2 s  . c om*/
        }
    }

    // Display the metadata
    /*if (model.getAnnotations() != null) {
    ArrayList<String> keywords = new ArrayList<>(model.getAnnotations().keySet());
            
    int limit = Math.min(keywords.size(), 3);
    viewHolder.metadata.setText(TextUtils.join("\n", keywords.subList(0, limit)));
    } else {
    viewHolder.metadata.setText("no annotations yet");
    }*/
}

From source file:com.example.androidthings.doorbell.DoorbellEntryAdapter.java

@Override
protected void populateViewHolder(DoorbellEntryViewHolder viewHolder, DoorbellEntry model, int position) {
    // Display the timestamp
    CharSequence prettyTime = DateUtils.getRelativeDateTimeString(mApplicationContext, model.getTimestamp(),
            DateUtils.SECOND_IN_MILLIS, DateUtils.WEEK_IN_MILLIS, 0);
    viewHolder.time.setText(prettyTime);

    // Display the image
    if (model.getImage() != null) {
        // Decode image data encoded by the Cloud Vision library
        byte[] imageBytes = Base64.decode(model.getImage(), Base64.NO_WRAP | Base64.URL_SAFE);
        Bitmap bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
        if (bitmap != null) {
            viewHolder.image.setImageBitmap(bitmap);
        } else {/*from  w  w w.  j a va 2 s . co  m*/
            Drawable placeholder = ContextCompat.getDrawable(mApplicationContext, R.drawable.ic_image);
            viewHolder.image.setImageDrawable(placeholder);
        }
    }

    // Display the metadata
    if (model.getAnnotations() != null) {
        ArrayList<String> keywords = new ArrayList<>(model.getAnnotations().keySet());

        int limit = Math.min(keywords.size(), 3);
        viewHolder.metadata.setText(TextUtils.join("\n", keywords.subList(0, limit)));
    } else {
        viewHolder.metadata.setText("no annotations yet");
    }
}

From source file:cl.monsoon.s1next.binding.TextViewBindingAdapter.java

@BindingAdapter("relativeDateTime")
public static void setRelativeDateTime(TextView textView, long datetime) {
    textView.setText(DateUtils.getRelativeDateTimeString(textView.getContext(), datetime,
            DateUtils.MINUTE_IN_MILLIS, DateUtils.DAY_IN_MILLIS, 0));
}

From source file:com.battlelancer.seriesguide.dataliberation.DataLiberationFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_data_liberation, container, false);

    mProgressBar = (ProgressBar) v.findViewById(R.id.progressBarDataLiberation);
    mProgressBar.setVisibility(View.GONE);
    mButtonExport = (Button) v.findViewById(R.id.buttonExport);
    mButtonImport = (Button) v.findViewById(R.id.buttonImport);
    mButtonImportAutoBackup = (Button) v.findViewById(R.id.buttonBackupRestoreAutoBackup);
    mCheckBoxFullDump = (CheckBox) v.findViewById(R.id.checkBoxFullDump);
    mCheckBoxImportWarning = (CheckBox) v.findViewById(R.id.checkBoxImportWarning);

    // display backup path
    TextView backuppath = (TextView) v.findViewById(R.id.textViewBackupPath);
    String path = JsonExportTask.getExportPath(false).toString();
    backuppath.setText(getString(R.string.backup_path) + ": " + path);

    // display current db version
    TextView dbVersion = (TextView) v.findViewById(R.id.textViewBackupDatabaseVersion);
    dbVersion.setText(getString(R.string.backup_version) + ": " + SeriesGuideDatabase.DATABASE_VERSION);

    // display last auto-backup date
    TextView lastAutoBackup = (TextView) v.findViewById(R.id.textViewBackupLastAutoBackup);
    long lastAutoBackupTime = AdvancedSettings.getLastAutoBackupTime(getActivity());
    lastAutoBackup.setText(getString(R.string.last_auto_backup,
            DataLiberationTools.isAutoBackupAvailable()
                    ? DateUtils.getRelativeDateTimeString(getActivity(), lastAutoBackupTime,
                            DateUtils.SECOND_IN_MILLIS, DateUtils.DAY_IN_MILLIS, 0)
                    : "n/a"));

    return v;/*w w w  .  ja va2 s . c  om*/
}

From source file:com.chalmers.feedlr.adapter.FeedAdapter.java

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

    // Holds the views, so that a recycled view does not have to find its
    // XML view/*from  w  ww  .  j a  va2 s .  c  om*/
    ViewHolder viewHolder = (ViewHolder) v.getTag();

    // Remove the recycled profile picture
    viewHolder.profilePicture.setVisibility(View.INVISIBLE);

    // Get user id
    int colNumUserId = c.getColumnIndex(DatabaseHelper.ITEM_COLUMN_USER_ID);
    Cursor cursor = db.getUser(c.getInt(colNumUserId) + "");
    cursor.moveToFirst();

    // Set source image
    int colNumSource = cursor.getColumnIndex(DatabaseHelper.USER_COLUMN_SOURCE);
    if (cursor.getString(colNumSource).equals("facebook")) {
        viewHolder.source.setBackgroundResource(R.drawable.source_facebook);
    } else {
        viewHolder.source.setBackgroundResource(R.drawable.source_twitter);
    }

    // Display profile picture
    int colNumURL = cursor.getColumnIndex(DatabaseHelper.USER_COLUMN_IMGURL);
    String imgURL = cursor.getString(colNumURL);
    viewHolder.profilePicture.setTag(numberOfViews++);
    new DownloadImageTask(viewHolder.profilePicture).execute(imgURL);

    // Display username
    int colNumUsername = cursor.getColumnIndex(DatabaseHelper.USER_COLUMN_USERNAME);
    if (cursor.getString(colNumUsername).length() > 18) {
        viewHolder.author.setTextSize(16);
    }

    viewHolder.author.setText(cursor.getString(colNumUsername));

    // Display timestamp
    int colNumTimestamp = c.getColumnIndex(DatabaseHelper.ITEM_COLUMN_TIMESTAMP);
    Date timestampDate = new Date(c.getLong(colNumTimestamp));
    String parsedTimestamp = DateUtils.getRelativeDateTimeString(context, timestampDate.getTime(),
            DateUtils.SECOND_IN_MILLIS, DateUtils.WEEK_IN_MILLIS, 0).toString();
    viewHolder.timestamp.setText(stripTimestamp(parsedTimestamp));
}

From source file:com.bubblegum.traceratops.app.ui.adapters.plugins.AbsAdapterPlugin.java

private String systemTimeInMillisToSystemDateFormat(Context context, long millis) {
    return DateUtils.getRelativeDateTimeString(context, millis, DateUtils.SECOND_IN_MILLIS,
            2 * DateUtils.DAY_IN_MILLIS, DateUtils.FORMAT_ABBREV_RELATIVE).toString();
}

From source file:com.synox.android.utils.DisplayUtils.java

@SuppressWarnings("deprecation")
public static CharSequence getRelativeDateTimeString(Context c, long time, long minResolution,
        long transitionResolution, int flags) {

    CharSequence dateString = "";

    // in Future/*  w w  w . ja v a2 s . c  o m*/
    if (time > System.currentTimeMillis()) {
        return DisplayUtils.unixTimeToHumanReadable(time);
    }
    // < 60 seconds -> seconds ago
    else if ((System.currentTimeMillis() - time) < 60 * 1000) {
        return c.getString(R.string.file_list_seconds_ago);
    } else {
        // Workaround 2.x bug (see https://github.com/owncloud/android/issues/716)
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB
                && (System.currentTimeMillis() - time) > 24 * 60 * 60 * 1000) {
            Date date = new Date(time);
            date.setHours(0);
            date.setMinutes(0);
            date.setSeconds(0);
            dateString = DateUtils.getRelativeDateTimeString(c, date.getTime(), minResolution,
                    transitionResolution, flags);
        } else {
            dateString = DateUtils.getRelativeDateTimeString(c, time, minResolution, transitionResolution,
                    flags);
        }
    }

    return dateString.toString().split(",")[0];
}

From source file:com.cerema.cloud2.utils.DisplayUtils.java

@SuppressWarnings("deprecation")
public static CharSequence getRelativeDateTimeString(Context c, long time, long minResolution,
        long transitionResolution, int flags) {

    CharSequence dateString = "";

    // in Future//from www  . ja  va 2 s. com
    if (time > System.currentTimeMillis()) {
        return DisplayUtils.unixTimeToHumanReadable(time);
    }
    // < 60 seconds -> seconds ago
    else if ((System.currentTimeMillis() - time) < 60 * 1000) {
        return c.getString(R.string.file_list_seconds_ago);
    } else {
        dateString = DateUtils.getRelativeDateTimeString(c, time, minResolution, transitionResolution, flags);
    }

    String[] parts = dateString.toString().split(",");
    if (parts.length == 2) {
        if (parts[1].contains(":") && !parts[0].contains(":")) {
            return parts[0];
        } else if (parts[0].contains(":") && !parts[1].contains(":")) {
            return parts[1];
        }
    }
    //dateString contains unexpected format. fallback: use relative date time string from android api as is.
    return dateString.toString();
}

From source file:com.vuze.android.remote.fragment.TorrentInfoFragment.java

private void fillTimeline(FragmentActivity a, Map<?, ?> mapTorrent) {
    String s;/*w ww  .  jav  a2 s . co m*/
    long addedOn = MapUtils.getMapLong(mapTorrent, TransmissionVars.FIELD_TORRENT_DATE_ADDED, 0);
    s = addedOn <= 0 ? ""
            : DateUtils.getRelativeDateTimeString(getActivity(), addedOn * 1000, DateUtils.MINUTE_IN_MILLIS,
                    DateUtils.WEEK_IN_MILLIS * 2, 0).toString();
    fillRow(a, R.id.torrentInfo_row_addedOn, R.id.torrentInfo_val_addedOn, s);

    long activeOn = MapUtils.getMapLong(mapTorrent, TransmissionVars.FIELD_TORRENT_DATE_ACTIVITY, 0);
    s = activeOn <= 0 ? ""
            : DateUtils.getRelativeDateTimeString(getActivity(), activeOn * 1000, DateUtils.MINUTE_IN_MILLIS,
                    DateUtils.WEEK_IN_MILLIS * 2, 0).toString();
    fillRow(a, R.id.torrentInfo_row_lastActiveOn, R.id.torrentInfo_val_lastActiveOn, s);

    long doneOn = MapUtils.getMapLong(mapTorrent, TransmissionVars.FIELD_TORRENT_DATE_DONE, 0);
    s = doneOn <= 0 ? ""
            : DateUtils.getRelativeDateTimeString(getActivity(), doneOn * 1000, DateUtils.MINUTE_IN_MILLIS,
                    DateUtils.WEEK_IN_MILLIS * 2, 0).toString();
    fillRow(a, R.id.torrentInfo_row_completedOn, R.id.torrentInfo_val_completedOn, s);

    long startedOn = MapUtils.getMapLong(mapTorrent, TransmissionVars.FIELD_TORRENT_DATE_STARTED, 0);
    s = startedOn <= 0 ? ""
            : DateUtils.getRelativeDateTimeString(getActivity(), startedOn * 1000, DateUtils.MINUTE_IN_MILLIS,
                    DateUtils.WEEK_IN_MILLIS * 2, 0).toString();
    fillRow(a, R.id.torrentInfo_row_startedOn, R.id.torrentInfo_val_startedOn, s);

    long secondsDownloading = MapUtils.getMapLong(mapTorrent,
            TransmissionVars.FIELD_TORRENT_SECONDS_DOWNLOADING, 0);
    s = secondsDownloading <= 0 ? "" : DisplayFormatters.prettyFormat(secondsDownloading);
    fillRow(a, R.id.torrentInfo_row_downloadingFor, R.id.torrentInfo_val_downloadingFor, s);

    long secondsUploading = MapUtils.getMapLong(mapTorrent, TransmissionVars.FIELD_TORRENT_SECONDS_SEEDING, 0);
    s = secondsUploading <= 0 ? "" : DisplayFormatters.prettyFormat(secondsUploading);
    fillRow(a, R.id.torrentInfo_row_seedingFor, R.id.torrentInfo_val_seedingFor, s);

    long etaSecs = MapUtils.getMapLong(mapTorrent, TransmissionVars.FIELD_TORRENT_ETA, -1);
    s = etaSecs > 0 && etaSecs * 1000 < DateUtils.WEEK_IN_MILLIS ? DisplayFormatters.prettyFormat(etaSecs) : "";
    fillRow(a, R.id.torrentInfo_row_eta, R.id.torrentInfo_val_eta, s);

}