get Remaining Time From Date As Html String - Android android.text

Android examples for android.text:Html

Description

get Remaining Time From Date As Html String

Demo Code

import java.util.Date;

public class Main{

    public static String getRemainingTimeFromDateAsHtmlString(
            long releaseDateLong, boolean image) {
        String resultString = "";
        //        Calendar calendar = Calendar.getInstance();
        //        calendar.setTimeInMillis(releaseDateLong);

        long YEAR_MILLIS = 1000L * 60 * 60 * 24 * 365;
        long MONTH_MILLIS = 1000L * 60 * 60 * 24 * 30;
        long WEEK_MILLIS = 1000L * 60 * 60 * 24 * 7;
        long DAY_MILLIS = 1000L * 60 * 60 * 24;
        long HOUR_MILLIS = 1000L * 60 * 60;
        long MINUTE_MILLIS = 1000L * 60;
        long SECONDS_MILLIS = 1000;

        String verb = image ? "View" : "Play";

        Date now = new Date();
        long delta = releaseDateLong - now.getTime();

        if (delta < MINUTE_MILLIS) {
            int seconds = (int) (delta / SECONDS_MILLIS);
            resultString = String.format("%s in <em>%d</em> seconds",
                    verb, seconds);/* ww w . j  a  v  a2  s.  co m*/
        } else if (delta < HOUR_MILLIS) {
            int minutes = (int) (delta / MINUTE_MILLIS);
            int seconds = (int) ((delta % MINUTE_MILLIS) / SECONDS_MILLIS);
            if (seconds > 0) {
                resultString = String
                        .format("%s in <em>%d</em> minutes <em>%d</em> seconds",
                                verb, minutes, seconds);
            } else {
                resultString = String.format("%s in <em>%d</em> minutes",
                        verb, minutes);
            }
        } else if (delta < DAY_MILLIS) {
            int hours = (int) (delta / HOUR_MILLIS);
            int minutes = (int) ((delta % HOUR_MILLIS) / MINUTE_MILLIS);
            if (minutes > 0) {
                resultString = String.format(
                        "%s in <em>%d</em> hours <em>%d</em> minutes",
                        verb, hours, minutes);
            } else {
                resultString = String.format("%s in <em>%d</em> hours",
                        verb, hours);
            }
        } else if (delta < WEEK_MILLIS) {
            int days = (int) (delta / DAY_MILLIS);
            int hours = (int) ((delta % DAY_MILLIS) / HOUR_MILLIS);
            if (hours > 0) {
                resultString = String.format(
                        "%s in <em>%d</em> days <em>%d</em> hours",
                        verb, days, hours);
            } else {
                resultString = String.format("%s in <em>%d</em> days",
                        verb, days);
            }
        } else if (delta < MONTH_MILLIS) {
            int weeks = (int) (delta / WEEK_MILLIS);
            int days = (int) ((delta % WEEK_MILLIS) / DAY_MILLIS);
            if (days > 0) {
                resultString = String.format(
                        "%s in <em>%d</em> weeks <em>%d</em> days",
                        verb, weeks, days);
            } else {
                resultString = String.format("%s in <em>%d</em> weeks",
                        verb, weeks);
            }
        } else if (delta < YEAR_MILLIS) {
            int months = (int) (delta / MONTH_MILLIS);
            int weeks = (int) ((delta % MONTH_MILLIS) / WEEK_MILLIS);
            if (weeks > 0) {
                resultString = String.format(
                        "%s in <em>%d</em> months <em>%d</em> weeks",
                        verb, months, weeks);
            } else {
                resultString = String.format("%s in <em>%d</em> months",
                        verb, months);
            }
        } else {
            android.text.format.DateUtils.getRelativeTimeSpanString(
                    releaseDateLong, System.currentTimeMillis(),
                    android.text.format.DateUtils.FORMAT_SHOW_YEAR,
                    android.text.format.DateUtils.FORMAT_ABBREV_RELATIVE);
        }

        return resultString;
    }

}

Related Tutorials