Java Time Since Format timeSince(long lastTimeInMillis)

Here you can find the source of timeSince(long lastTimeInMillis)

Description

time Since

License

Apache License

Declaration

public static String timeSince(long lastTimeInMillis) 

Method Source Code

//package com.java2s;
/*/*from  ww w.j  a  va 2 s. c o m*/
 * Copyright 2013, The Sporting Exchange Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    private static final long DAY_LENGTH = 86400000L;
    private static final long HOUR_LENGTH = 3600000L;
    private static final long MINUTE_LENGTH = 60000L;
    private static final long SECOND_LENGTH = 1000L;

    public static String timeSince(long lastTimeInMillis) {
        if (lastTimeInMillis == 0) {
            return "Never";
        }
        long timeDiff = System.currentTimeMillis() - lastTimeInMillis;
        String ret = "";
        String sep = "";
        if (timeDiff < SECOND_LENGTH) {
            ret = "Less than 1 second";
        } else {
            if (timeDiff >= DAY_LENGTH) {
                long days = timeDiff / DAY_LENGTH;
                ret += sep + days + " day";
                if (days > 1) {
                    ret += "s";
                }
                sep = ", ";
                timeDiff -= (days * DAY_LENGTH);
            }
            if (timeDiff >= HOUR_LENGTH) {
                long hours = timeDiff / HOUR_LENGTH;
                ret += sep + hours + " hour";
                if (hours > 1) {
                    ret += "s";
                }
                sep = ", ";
                timeDiff -= (hours * HOUR_LENGTH);
            }
            if (timeDiff >= MINUTE_LENGTH) {
                long mins = timeDiff / MINUTE_LENGTH;
                ret += sep + mins + " minute";
                if (mins > 1) {
                    ret += "s";
                }
                sep = ", ";
                timeDiff -= (mins * MINUTE_LENGTH);
            }
            if (timeDiff >= SECOND_LENGTH) {
                long secs = timeDiff / SECOND_LENGTH;
                ret += sep + secs + " second";
                if (secs > 1) {
                    ret += "s";
                }
                timeDiff -= (secs * SECOND_LENGTH);
            }
        }

        ret += " ago";
        return ret;
    }
}

Related

  1. differenceNumberOfDays(long lodTime, long currentTime)
  2. timeSince()
  3. timeSince(long t)
  4. timeSince(long timestart)
  5. timeSinceNanos(long prevTime)
  6. timeSincePretty(long time)