Java Millisecond Convert millisToLongDHMS(long duration, String pubDate)

Here you can find the source of millisToLongDHMS(long duration, String pubDate)

Description

millis To Long DHMS

License

Open Source License

Parameter

Parameter Description
duration duration between time now and the past date
pubDate The date that you want to calculate time ago of..

Return

a Date ex. 2 minutes ago, 5 hours ago, today, or date string format dd/MM/yyyy HH:mm

Declaration

public static String millisToLongDHMS(long duration, String pubDate) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public final static long ONE_SECOND = 1000;
    public final static long ONE_MINUTE = ONE_SECOND * 60;
    public final static long ONE_HOUR = ONE_MINUTE * 60;
    public final static long ONE_DAY = ONE_HOUR * 24;

    /**/*  ww  w . ja  va  2s. c o  m*/
     * @param duration duration between time now and the past date
     * @param pubDate The date that you want to calculate time ago of..
     * @return a Date ex. 2 minutes ago, 5 hours ago, today, or date string format dd/MM/yyyy HH:mm
     **/
    public static String millisToLongDHMS(long duration, String pubDate) {
        StringBuffer res = new StringBuffer();
        long temp = 0;
        // Check if duration more than 1000 Mills
        if (duration >= ONE_SECOND) {
            // if temp bigger than zero that means we have to diplay the pubDate, otherwise duration < ONE_DAY 
            temp = duration / ONE_DAY;
            if (temp > 0) {
                duration -= temp * ONE_DAY;
                res.append(pubDate);
                return res.toString();
            }

            // if temp > 0 that means at least 1 hour passed
            temp = duration / ONE_HOUR;
            if (temp > 0) {
                duration -= temp * ONE_HOUR;
                res.append("today, ").append(temp).append(" hour").append(temp > 1 ? "s ago" : " ago");
                return res.toString();
            }

            // if temp > 0 that means at least 1 Minute passed and not more than 1 hour since we reached here
            temp = duration / ONE_MINUTE;
            if (temp > 0) {
                duration -= temp * ONE_MINUTE;
                res.append(temp).append(" minute").append(temp > 1 ? "s ago" : " ago");
                return res.toString();
            }

            // if temp > 0 that means at least 1 second passed and not more than 1 minute since we reached here.
            temp = duration / ONE_SECOND;
            if (temp > 0) {
                res.append(temp).append(" second").append(temp > 1 ? "s ago" : " ago");
            }
            return res.toString();
        } else {
            return "0 second";
        }
    }
}

Related

  1. millisToDurationString(long millis)
  2. millisToHMS(long millis)
  3. millisToHMSShort(long millis)
  4. millisToLongDHMS(long duration)
  5. millisToLongDHMS(long duration)
  6. millisToLongM(long duration)
  7. millisToNanos(final long millis)
  8. millisToNanos(int i)
  9. millisToNanos(long millis)