Android Date to String Convert javaDateToTimeSpan(final Date date)

Here you can find the source of javaDateToTimeSpan(final Date date)

Description

java Date To Time Span

License

Open Source License

Declaration

public static String javaDateToTimeSpan(final Date date) 

Method Source Code

//License from project: Open Source License 

import android.text.format.DateUtils;
import org.wordpress.caredear.R;
import org.wordpress.android.WordPress;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class Main{
    public static String javaDateToTimeSpan(final Date date) {
        if (date == null)
            return "";

        long passedTime = date.getTime();
        long currentTime = System.currentTimeMillis();

        // return "now" if less than a minute has elapsed
        long secondsSince = (currentTime - passedTime) / 1000;
        if (secondsSince < 60)
            return WordPress.getContext().getString(
                    R.string.reader_timespan_now);

        // less than an hour (ex: 12m)
        long minutesSince = secondsSince / 60;
        if (minutesSince < 60)
            return Long.toString(minutesSince) + "m";

        // less than a day (ex: 17h)
        long hoursSince = minutesSince / 60;
        if (hoursSince < 24)
            return Long.toString(hoursSince) + "h";

        // less than a week (ex: 5d)
        long daysSince = hoursSince / 24;
        if (daysSince < 7)
            return Long.toString(daysSince) + "d";

        // less than a year old, so return day/month without year (ex: Jan 30)
        if (daysSince < 365)
            return DateUtils.formatDateTime(WordPress.getContext(),
                    passedTime, DateUtils.FORMAT_NO_YEAR
                            | DateUtils.FORMAT_ABBREV_ALL);

        // date is older, so include year (ex: Jan 30, 2013)
        return DateUtils.formatDateTime(WordPress.getContext(), passedTime,
                DateUtils.FORMAT_ABBREV_ALL);
    }//from w w w .  j  a v  a  2 s.c  o  m
}

Related

  1. getLongFormat(Date date)
  2. getShortDateTimeString(Date date)
  3. getShortFormat(Date date)
  4. getStringFromDate(Date date)
  5. javaDateToIso8601(Date date)
  6. toGMTDate(Date localDate)
  7. toString(Date date)
  8. getStrFromDate(Date date)
  9. dateToString(Date date)