Android Date Relative Format relativeDate(String dateString)

Here you can find the source of relativeDate(String dateString)

Description

relative Date

Declaration

public static String relativeDate(String dateString) 

Method Source Code

//package com.java2s;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Main {
    public static String relativeDate(String dateString) {
        String dayMonthYear = dateString.substring(0,
                dateString.indexOf(" "));
        int month = Integer.parseInt(dayMonthYear.substring(0, 2));
        int day = Integer.parseInt(dayMonthYear.substring(3, 5));
        int year = Integer.parseInt(dayMonthYear.substring(6, 8));

        Calendar cal = Calendar.getInstance();
        int currentDay = cal.get(Calendar.DAY_OF_MONTH);
        int currentMonth = cal.get(Calendar.MONTH) + 1;
        int currentYear = cal.get(Calendar.YEAR) - 2000;

        String processedDate = dayMonthYear;

        if (day == currentDay && month == currentMonth
                && year == currentYear) {
            processedDate = dateString.substring(
                    dateString.indexOf(" ") + 1).toLowerCase();

            if (processedDate.startsWith("0")) {
                processedDate = processedDate.substring(1);
            }/*  ww w . j a va  2  s  . c  o m*/
        } else if (year == currentYear) {
            Date date = new Date(year + 100, month - 1, day);
            SimpleDateFormat dateFormat = new SimpleDateFormat("MMM d");
            processedDate = dateFormat.format(date);
        }

        return processedDate;
    }
}

Related

  1. formatRelativeTime(String fullRelativeDate)
  2. getRelativeTimeofTweet(String tweetCreatedAt)
  3. getRelativeDate(Date date)
  4. humanFriendlyDate(Date date)
  5. dateIsBetweenTwoDates(String begin, String end)
  6. getHumanReadableDateDiff(Date pastDate, boolean unknownIfNull)