Java Timestamp Create timestampDifference(long ts1, long ts2)

Here you can find the source of timestampDifference(long ts1, long ts2)

Description

timestamp Difference

License

Open Source License

Declaration

public static String timestampDifference(long ts1, long ts2) 

Method Source Code

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

public class Main {
    public static String timestampDifference(long ts1, long ts2) {
        long difference = (ts1 - ts2) / 1000; // I timestamp sono in ms e non in sec
        if (difference <= 0) {
            return "0 second";
        }//  ww  w .j a v a  2 s.  c o  m

        String strIntervals[][] = new String[][] { new String[] { "year", "years" },
                new String[] { "month", "months" }, new String[] { "week", "weeks" },
                new String[] { "day", "days" }, new String[] { "hour", "hours" },
                new String[] { "minute", "minutes" }, new String[] { "second", "seconds" }, };

        Long lngIntervals[] = new Long[] { (long) 31556926, // year
                (long) 2628000, // month
                (long) 604800, // week
                (long) 86400, (long) 3600, (long) 60, (long) 1 };

        int granularity = 2;
        String retval = "";
        double time;
        for (int i = 0; i < strIntervals.length; i++) {
            if (difference >= lngIntervals[i]) {
                time = Math.floor((double) (difference / lngIntervals[i]));
                difference %= lngIntervals[i];
                retval += (retval.length() > 0 ? " " : "") + (int) time + " ";
                retval += ((time > 1) ? strIntervals[i][1] : strIntervals[i][0]);
                granularity--;

                if (difference <= 0) {
                    break;
                }

                if (granularity == 1) {
                    retval += " and";
                } else if (granularity == 0) {
                    break;
                }
            }
        }
        return retval;
    }
}

Related

  1. getTimeStrByTimestamp(long timestamp, String tz)
  2. timestamp()
  3. timeStamp()
  4. timestamp2string()
  5. timestamp4(int unixTime)
  6. timestampMicros()
  7. timestampNonce(final int length)
  8. timestampNow()
  9. timestampStartOfDay(long time)