Android Date Format getLongFriendlyDate(Date dt)

Here you can find the source of getLongFriendlyDate(Date dt)

Description

get Long Friendly Date

Declaration

public static String getLongFriendlyDate(Date dt) 

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());
    }/*from www.  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. getStringByFormat(Date date, String format)
  2. convertDateToString(Date date)
  3. formatDate(Date date)
  4. now(String dateFormat)
  5. now(String dateFormat)
  6. getMainClockString(Date utcDate)
  7. currentDateToString(String format)
  8. dateToString(Date paramDate, String paramString)
  9. changeDateToFormatString(Date fecha, String pattern)