get Distance Of Time In Words - Android java.util

Android examples for java.util:Date Time

Description

get Distance Of Time In Words

Demo Code

import android.annotation.SuppressLint;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class Main{

    /**/*www.  j  a  v  a 2  s  .  co  m*/
     * Returns the amount of elapsed time from that date to now in full words
     */
    public static String getDistanceOfTimeInWords(Date date) {
        return getDistanceOfTimeInWords(date, true);
    }
    /**
     * Returns the amount of elapsed time from that date to now in shortened words
     */
    public static String getDistanceOfTimeInWords(Date date,
            boolean beConcise) {
        String ago = "ago";
        String FromNow = "from now";
        String JustNow = "Just now";
        String LessThan = "Less than";
        String About = "About";
        String Over = "Over";
        String Almost = "Almost";
        String Seconds = beConcise ? "secs" : "seconds";
        String Minute = beConcise ? "min" : "minute";
        String Minutes = beConcise ? "mins" : "minutes";
        String Hour = beConcise ? "hr" : "hour";
        String Hours = beConcise ? "hrs" : "hours";
        String Day = "day";
        String Days = "days";
        String Month = "month";
        String Months = "months";
        String Year = "year";
        String Years = "years";

        long since = date.getTime() - (new Date()).getTime();

        String direction = since <= 0 ? ago : FromNow;

        int seconds = (int) (Math.abs(since) / 1000);
        int minutes = Math.round(seconds / SECONDS_PER_MINUTE);
        int hours = Math.round(seconds / SECONDS_PER_HOUR);
        int days = Math.round(seconds / SECONDS_PER_DAY);
        int months = Math.round(seconds / SECONDS_PER_MONTH);
        int years = Math.round(seconds / SECONDS_PER_YEAR);
        int offset = (int) Math.round(Math.floor(years / 4.0) * 1440.0);
        int remainder = (minutes - offset) % 525600;

        int number;
        String measure;
        String modifier = "";

        if (minutes <= 1) {
            measure = Seconds;
            if (seconds <= 4) {
                number = 5;
                modifier = beConcise ? JustNow : LessThan;
            } else if (seconds <= 9) {
                number = 10;
                modifier = beConcise ? JustNow : LessThan;
            } else if (seconds <= 19) {
                number = 20;
                modifier = beConcise ? JustNow : LessThan;
            } else if (seconds <= 39) {
                number = 30;
                modifier = beConcise ? JustNow : About;
            } else if (seconds <= 49) {
                number = 1;
                measure = Minute;
                modifier = beConcise ? JustNow : LessThan;
            } else if (seconds <= 59) {
                number = 1;
                measure = Minute;
                modifier = beConcise ? JustNow : LessThan;
            } else {
                number = 1;
                measure = Minute;
                modifier = beConcise ? JustNow : About;
            }
        } else if (minutes <= 44) {
            number = minutes;
            measure = Minutes;
        } else if (minutes <= 89) {
            number = 1;
            measure = Hour;
            modifier = beConcise ? modifier : About;
            ;
        } else if (minutes <= 1439) {
            number = hours;
            measure = Hours;
            modifier = beConcise ? modifier : About;
            ;
        } else if (minutes <= 2529) {
            number = 1;
            measure = Day;
        } else if (minutes <= 43199) {
            number = days;
            measure = Days;
        } else if (minutes <= 86399) {
            number = 1;
            measure = Month;
            modifier = beConcise ? modifier : About;
        } else if (minutes <= 525599) {
            number = months;
            measure = Months;
        } else {
            number = years;
            measure = number == 1 ? Year : Years;
            if (remainder < 131400) {
                modifier = beConcise ? modifier : About;
                ;
            } else if (remainder < 394200) {
                modifier = Over;
            } else {
                ++number;
                measure = Years;
                modifier = Almost;
            }
        }
        if (modifier.equalsIgnoreCase(JustNow)) {
            return modifier;
        } else {
            if (modifier.length() > 0) {
                modifier += " ";
            }
            return modifier + number + " " + measure + " " + direction;
        }
    }

}

Related Tutorials