Android Date Format getShortFriendlyDate(Date dt)

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

Description

get Short Friendly Date

Declaration

public static String getShortFriendlyDate(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 getShortFriendlyDate(Calendar cal) {
        return getShortFriendlyDate(cal.getTime());
    }// ww  w.j a  va  2  s. c  om

    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. formatDate(Date date)
  2. formatDate(Date date)
  3. formatDate(Date date, String pattern)
  4. formatDate(Date date, String pattern, Locale locale)
  5. format(Date d, String dateFormat)
  6. getFormatDateString(String format)
  7. getFormatDateString(String format, java.util.Date date)
  8. getFormatDateTime(String formatString, Date d)
  9. formatDateToString(Date timestamp)