Android Long to Date Convert getShortFriendlyDate(long timeInMillis)

Here you can find the source of getShortFriendlyDate(long timeInMillis)

Description

get Short Friendly Date

Declaration

public static String getShortFriendlyDate(long timeInMillis) 

Method Source Code

//package com.java2s;
import java.util.Calendar;
import java.util.Date;
import android.text.format.DateFormat;

public class Main {
    public static String getShortFriendlyDate(Calendar cal) {
        return getShortFriendlyDate(cal.getTime());
    }/*w  w  w.jav  a2s  . co  m*/

    public static String getShortFriendlyDate(long timeInMillis) {
        return getShortFriendlyDate(new Date(timeInMillis));
    }

    public static String getShortFriendlyDate(Date dt) {
        String dtStr = DateFormat.format("MMM d, yyyy", dt).toString();

        Date today = getToday();
        String todayStr = DateFormat.format("MMM d, yyyy", today)
                .toString();

        if (dtStr.equals(todayStr)) {
            return "Today";
        }

        Date yesterday = getYesterday();
        String yesterdayStr = DateFormat.format("MMM d, yyyy", yesterday)
                .toString();

        if (dtStr.equals(yesterdayStr)) {
            return "Yesterday";
        }

        if (dt.getYear() != today.getYear()) {
            return dtStr;
        } else {
            return DateFormat.format("MMM d", dt).toString();
        }
    }

    public static Date getToday() {
        Date now = new Date();
        Date today = new Date(now.getYear(), now.getMonth(), now.getDate()); //beginning of today
        return today;
    }

    public static Date getYesterday() {
        Date today = getToday();
        Date yesterday = new Date(today.getYear(), today.getMonth(),
                today.getDate() - 1); //beginning of yesterday
        return yesterday;
    }
}

Related

  1. formatDateD(Context paramContext, long paramLong)
  2. formatTimeStampString(Context context, long when, boolean fullFormat)
  3. getDatesStartingFromToWithInterval( long startDate, long endDate, int dayInterval)
  4. getFormatDateString(String format, long date)
  5. getLongFriendlyDate(long timeInMillis)
  6. getTimeStringFrom(long timestamp)
  7. humanFriendlyDate(long time)
  8. longToDate(long timestamp)
  9. millToDate(Long mill)