Android Long to Date Convert getFriendlyDate(Context context, long timestamp)

Here you can find the source of getFriendlyDate(Context context, long timestamp)

Description

Returns date from given @param timestamp in friendly format.

License

Open Source License

Parameter

Parameter Description
timestamp the dates timestamp

Return

friendly formatted date

Declaration

public static String getFriendlyDate(Context context, long timestamp) 

Method Source Code

/*/*from   ww  w. j a  va  2  s.c o m*/
 * Ubuntu One Files - access Ubuntu One cloud storage on Android platform.
 * 
 * Copyright (C) 2011 Canonical Ltd.
 * Author: Michal Karnicki <michal.karnicki@canonical.com>
 *   
 * This file is part of Ubuntu One Files.
 *  
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *  
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *  
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see http://www.gnu.org/licenses 
 */

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import android.content.Context;
import android.content.res.Resources;
import com.ubuntuone.android.files.R;

public class Main{
    public static final Long MILLIS_IN_MINUTE = 60 * MILLIS_IN_SECOND;
    public static final Long MILLIS_IN_HOUR = 2 * MILLIS_IN_HALF_HOUR;
    public static final Long MILLIS_IN_DAY = 2 * MILLIS_IN_HALF_DAY;
    public static final Long MILLIS_IN_MONTH = 30 * MILLIS_IN_DAY;
    private static SimpleDateFormat sDateFormatter;
    private static TimeZone sTimeZone;
    private static Resources sResources;
    /**
     * Returns date from given @param timestamp in friendly format.
     * 
     * @param timestamp the dates timestamp
     * @return friendly formatted date
     */
    public static String getFriendlyDate(Context context, long timestamp) {
        if (timestamp < 0)
            return "";

        if (sResources == null) {
            sResources = context.getResources();
        }

        final long now = System.currentTimeMillis();
        final long offset = sTimeZone.getOffset(now);
        // utcNow == now - offset
        final long diff = now - offset - timestamp;

        if (diff < MILLIS_IN_MINUTE)
            return "just now";
        if (diff < MILLIS_IN_HOUR) {
            int minutes = (int) (diff / (double) MILLIS_IN_MINUTE);
            final String fmt = sResources.getQuantityString(
                    R.plurals.last_modified_minutes, minutes, minutes);
            return String.format(fmt, minutes);
        }
        if (diff < MILLIS_IN_DAY) {
            int hours = (int) (diff / (double) MILLIS_IN_HOUR);
            final String fmt = sResources.getQuantityString(
                    R.plurals.last_modified_hours, hours, hours);
            return String.format(fmt, hours);
        }
        if (diff < MILLIS_IN_MONTH) {
            int days = (int) (diff / (double) MILLIS_IN_DAY);
            final String fmt = sResources.getQuantityString(
                    R.plurals.last_modified_days, days, days);
            return String.format(fmt, days);
        }
        return sDateFormatter.format(new Date(timestamp));
    }
}

Related

  1. getDayOfWeek(long timeMillis)
  2. getDefaultDateTimeString(long date, Locale locale)
  3. getDefaultDatetime(long milliseconds)
  4. getElapsedDurationSince(long seconds)
  5. getFormattedTime(String dateTimeFormat, long timestamp)
  6. getHour(long dateTimeMillis)
  7. getHour(long timeMillis)
  8. getHoursAndMinutes(long dateTimeMillis)
  9. getLocalTimeFromUTCMillis(long utcMillis)