Android Long to Date Convert getElapsedDurationSince(long seconds)

Here you can find the source of getElapsedDurationSince(long seconds)

Description

get Elapsed Duration Since

License

Open Source License

Declaration

public static String getElapsedDurationSince(long seconds) 

Method Source Code

//package com.java2s;
/*/*from  w w w  .  j a  va 2s  .c  o  m*/
 Copyright (C) 2012 Prasanna Thirumalai
    
 This file is part of StackX.

 StackX is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 StackX is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with StackX.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    private static final String JUST_NOW = "just now";
    private static final String AGO = "ago";
    public static final String SECOND = "s";
    public static final String MINUTE = "m";
    public static final String HOUR = "h";
    public static final String DAY = "d";
    public static final String MONTH = "M";
    public static final String YEAR = "y";
    public static final int SECONDS_IN_MIN = 60;
    public static final int SECONDS_IN_HOUR = 3600;
    public static final int SECONDS_IN_DAY = 3600 * 24;
    public static final int DAYS_IN_MONTH = 30;
    public static final int MONTHS_IN_YEAR = 12;

    public static String getElapsedDurationSince(long seconds) {
        String unit = SECOND;
        String residueUnit = null;

        long currentTimeInSeconds = System.currentTimeMillis() / 1000;
        long elapsedTimeInSeconds = currentTimeInSeconds - seconds;

        int count = 0;
        int residue = 0;

        if (elapsedTimeInSeconds > SECONDS_IN_MIN
                && elapsedTimeInSeconds < SECONDS_IN_HOUR) {
            count = (int) (elapsedTimeInSeconds / SECONDS_IN_MIN);
            unit = MINUTE;
        } else if (elapsedTimeInSeconds > SECONDS_IN_HOUR
                && elapsedTimeInSeconds < SECONDS_IN_DAY) {
            count = (int) (elapsedTimeInSeconds / SECONDS_IN_HOUR);
            unit = HOUR;
        } else if (elapsedTimeInSeconds > SECONDS_IN_DAY) {
            count = (int) (elapsedTimeInSeconds / SECONDS_IN_DAY);
            if (count > DAYS_IN_MONTH) {

                residue = count % DAYS_IN_MONTH;
                count /= DAYS_IN_MONTH;

                if (count > MONTHS_IN_YEAR) {
                    residue = count % MONTHS_IN_YEAR;
                    count /= MONTHS_IN_YEAR;
                    unit = YEAR;
                    residueUnit = MONTH;
                } else {
                    unit = MONTH;
                    residueUnit = DAY;
                }
            } else {
                unit = DAY;
            }
        }

        if (count == 0)
            return JUST_NOW;

        String duration = new String(count + unit);

        if (residue > 0)
            duration += " " + residue + residueUnit;

        duration += " " + AGO;
        return duration;
    }
}

Related

  1. getDateWeekLabel(long date)
  2. getDay(long dateTimeMillis)
  3. getDayOfWeek(long timeMillis)
  4. getDefaultDateTimeString(long date, Locale locale)
  5. getDefaultDatetime(long milliseconds)
  6. getFormattedTime(String dateTimeFormat, long timestamp)
  7. getFriendlyDate(Context context, long timestamp)
  8. getHour(long dateTimeMillis)
  9. getHour(long timeMillis)