Android Calendar Format getShortFriendlyDate(Calendar cal)

Here you can find the source of getShortFriendlyDate(Calendar cal)

Description

get Short Friendly Date

Declaration

public static String getShortFriendlyDate(Calendar cal) 

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  ww  .ja v a 2  s  .  c  o 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. formatCalendar(Calendar calendar)
  2. formatCalendar(Calendar calendar, String pattern)
  3. formatDateTime(Calendar dateTime)
  4. getLongFriendlyDate(Calendar cal)
  5. getSelectedDate(Calendar calendar, String pattern)