Java TimeUnit Usage elapsedTimeSince(Date d)

Here you can find the source of elapsedTimeSince(Date d)

Description

elapsed Time Since

License

Open Source License

Declaration

public static String elapsedTimeSince(Date d) 

Method Source Code

//package com.java2s;

import java.util.Date;
import java.util.concurrent.TimeUnit;

public class Main {
    public static final long kSecond = 1000;
    public static final long kMinute = 60 * kSecond;
    public static final long kHour = 60 * kMinute;
    public static final long kDay = 24 * kHour;
    public static final long kWeek = 7 * kDay;
    public static final long kMonth = 30 * kDay;
    public static final long kYear = 365 * kDay;

    public static String elapsedTimeSince(Date d) {
        // return elapsed time with precision that's scaled back as the time grows distant
        long unit = 1;
        final long elapsedMs = System.currentTimeMillis() - d.getTime();

        // over 5 seconds, report in seconds
        if (elapsedMs > 1000 * 5) {
            unit = 1000;//www . j a v a 2s .  c  o m
        }

        // over 5 minutes, report in minutes
        if (elapsedMs > 1000 * 60 * 5) {
            unit = 1000 * 60;
        }

        // over 5 hours, report in hours
        if (elapsedMs > 1000 * 60 * 60 * 5) {
            unit = 1000 * 60 * 60;
        }

        // over 5 days, report in days
        if (elapsedMs > 1000 * 60 * 60 * 24 * 5) {
            unit = 1000 * 60 * 60 * 24;
        }

        // over 5 weeks, report in weeks
        if (elapsedMs > 1000 * 60 * 60 * 24 * 7 * 5) {
            unit = 1000 * 60 * 60 * 24 * 7;
        }

        // over 5 months, report in months
        if (elapsedMs > 1000 * 60 * 60 * 24 * 30 * 5) {
            unit = 1000 * 60 * 60 * 24 * 30;
        }

        // over 2 years, report in years
        if (elapsedMs > 1000 * 60 * 60 * 24 * 365 * 2) {
            unit = 1000 * 60 * 60 * 24 * 365;
        }

        return elapsedTimeSince(d, unit);
    }

    public static String elapsedTimeSince(Date d, long smallestUnitInMillis) {
        if (d == null) {
            return "";
        }

        final Date now = new Date();
        final long elapsedMs = now.getTime() - d.getTime();
        if (elapsedMs < 0) {
            return timeValue(elapsedMs * -1, TimeUnit.MILLISECONDS,
                    smallestUnitInMillis) + " in the future";
        } else {
            return timeValue(elapsedMs, TimeUnit.MILLISECONDS,
                    smallestUnitInMillis) + " ago";
        }
    }

    public static String timeValue(long units, TimeUnit tu,
            long smallestUnit) {
        final long timeInMs = TimeUnit.MILLISECONDS.convert(units, tu);

        String result = "" + timeInMs + " ms";
        if (timeInMs > kYear) {
            final long years = timeInMs / kYear;
            final long remaining = timeInMs - (years * kYear);
            result = "" + years + " yrs";
            if (remaining > smallestUnit) {
                result += ", ";
                result += timeValue(remaining, TimeUnit.MILLISECONDS,
                        smallestUnit);
            }
        } else if (timeInMs > kMonth) {
            final long months = timeInMs / kMonth;
            final long remaining = timeInMs - (months * kMonth);
            result = "" + months + " months";
            if (remaining > smallestUnit) {
                result += ", ";
                result += timeValue(remaining, TimeUnit.MILLISECONDS,
                        smallestUnit);
            }
        } else if (timeInMs > kWeek) {
            final long weeks = timeInMs / kWeek;
            final long remaining = timeInMs - (weeks * kWeek);
            result = "" + weeks + " wks";
            if (remaining > smallestUnit) {
                result += ", ";
                result += timeValue(remaining, TimeUnit.MILLISECONDS,
                        smallestUnit);
            }
        } else if (timeInMs > kDay) {
            final long days = timeInMs / kDay;
            final long remaining = timeInMs - (days * kDay);
            result = "" + days + " days";
            if (remaining > smallestUnit) {
                result += ", ";
                result += timeValue(remaining, TimeUnit.MILLISECONDS,
                        smallestUnit);
            }
        } else if (timeInMs > kHour) {
            final long hrs = timeInMs / kHour;
            final long remaining = timeInMs - (hrs * kHour);
            result = "" + hrs + (hrs == 1 ? " hr" : " hrs");
            if (remaining > smallestUnit) {
                result += ", ";
                result += timeValue(remaining, TimeUnit.MILLISECONDS,
                        smallestUnit);
            }
        } else if (timeInMs > kMinute) {
            final long mins = timeInMs / kMinute;
            final long remaining = timeInMs - (mins * kMinute);
            result = "" + mins + " m";
            if (remaining > smallestUnit) {
                result += ", ";
                result += timeValue(remaining, TimeUnit.MILLISECONDS,
                        smallestUnit);
            }
        } else if (timeInMs > kSecond) {
            final long seconds = timeInMs / kSecond;
            final long remaining = timeInMs - (seconds * kSecond);
            result = "" + seconds + " s";
            if (remaining > smallestUnit) {
                result += ", ";
                result += timeValue(remaining, TimeUnit.MILLISECONDS,
                        smallestUnit);
            }
        } else {
            result = "" + timeInMs + " ms";
        }
        return result;
    }
}

Related

  1. durationIsValid(long seconds, int nanos)
  2. durationToString(long duration)
  3. durationToString(long millis)
  4. elapsedMicroSec(long startNanoTime)
  5. elapsedTime(long start, long end)
  6. format(long elapsed, boolean hours)
  7. formatDuration(long duration)
  8. formatDuration(long millis)
  9. formatDuration(long millis)