Android Long to Date Convert getLongFriendlyDate(long timeInMillis)

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

Description

get Long Friendly Date

Declaration

public static String getLongFriendlyDate(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 getLongFriendlyDate(Calendar cal) {
        return getLongFriendlyDate(cal.getTime());
    }//w  w w . j  a v a  2  s  . co m

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

    public static String getLongFriendlyDate(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 " + DateFormat.format("h:mm aa", dt);
        }

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

        if (dtStr.equals(yesterdayStr)) {
            return "Yesterday " + DateFormat.format("h:mm aa", dt);
        }

        if (dt.getYear() != today.getYear()) {
            return DateFormat.format("MMM d, yyyy h:mm aa", dt).toString();
        } else {
            return DateFormat.format("MMM d h:mm aa", 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. format(String pattern, long timeInMillis)
  2. formatDateD(Context paramContext, long paramLong)
  3. formatTimeStampString(Context context, long when, boolean fullFormat)
  4. getDatesStartingFromToWithInterval( long startDate, long endDate, int dayInterval)
  5. getFormatDateString(String format, long date)
  6. getShortFriendlyDate(long timeInMillis)
  7. getTimeStringFrom(long timestamp)
  8. humanFriendlyDate(long time)
  9. longToDate(long timestamp)