Android Date Format toRelativeDateString(Date value)

Here you can find the source of toRelativeDateString(Date value)

Description

to Relative Date String

License

Apache License

Declaration

@SuppressWarnings("deprecation")
    public static String toRelativeDateString(Date value) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class Main {
    @SuppressWarnings("deprecation")
    public static String toRelativeDateString(Date value) {
        if (value != null) {
            String result = "";

            Date dateNow = new Date();

            if ((dateNow.getYear() == value.getYear())
                    && (dateNow.getMonth() == value.getMonth())) {
                int daysApart = dateNow.getDate() - value.getDate();

                if (daysApart >= 28) {
                    result = "4 weeks ago";
                } else if (daysApart >= 21) {
                    result = "3 weeks ago";
                } else if (daysApart >= 14) {
                    result = "2 weeks ago";
                } else if (daysApart >= 7) {
                    result = "a week ago";
                } else if (daysApart >= 2) {
                    result = Integer.toString(daysApart) + " days ago";
                } else if (daysApart >= 1) {
                    result = "1 day ago";
                } else {
                    int hoursApart = dateNow.getHours() - value.getHours();
                    if (hoursApart > 1) {
                        result = Integer.toString(hoursApart)
                                + " hours ago";
                    } else {
                        int minsApart = dateNow.getMinutes()
                                - value.getMinutes();

                        if (minsApart > 10) {
                            result = Integer.toString(minsApart)
                                    + " minutes ago";
                        } else {
                            result = "a moment ago";
                        }/*  ww  w.j a  v a2  s  .  co m*/
                    }
                }
            } else {
                result = toDateTimeString(value);
            }

            return result;
        } else {
            return "";
        }
    }

    public static String toString(Date value, String format) {
        if (value != null) {
            SimpleDateFormat dateFormat = new SimpleDateFormat(format);
            dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));

            return dateFormat.format(value);
        } else {
            return "";
        }
    }

    public static String toDateTimeString(Date value) {
        if (value != null) {
            SimpleDateFormat dateFormat = new SimpleDateFormat(
                    "yyyy-MM-dd HH:mm:ss");
            dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));

            return dateFormat.format(value);
        } else {
            return "";
        }
    }
}

Related

  1. getUniversalDateStamp(Date date)
  2. getUtcTimeStringFromDate(Date date)
  3. timestampToISO8601(Date aDate)
  4. toDateString(Date value)
  5. toDateTimeString(Date value)
  6. toString(Date date, String format)
  7. toString(Date value, String format)
  8. toStringFR(Date date)
  9. toStringUS(Date date)