Android Calendar Format getLongFriendlyDate(Calendar cal)

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

Description

get Long Friendly Date

Declaration

public static String getLongFriendlyDate(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 getLongFriendlyDate(Calendar cal) {
        return getLongFriendlyDate(cal.getTime());
    }/*ww  w  .  ja v  a2s.c o 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. getCurrentDateString(String dateFormat)
  2. getAsString(Calendar date)
  3. formatCalendar(Calendar calendar)
  4. formatCalendar(Calendar calendar, String pattern)
  5. formatDateTime(Calendar dateTime)
  6. getSelectedDate(Calendar calendar, String pattern)
  7. getShortFriendlyDate(Calendar cal)