Java Time Since Format timeSincePretty(long time)

Here you can find the source of timeSincePretty(long time)

Description

time Since Pretty

License

Apache License

Declaration

public static String timeSincePretty(long time) 

Method Source Code

//package com.java2s;
/*//  ww  w  .ja v a2  s  . co m
 * Copyright 2014 Alexander Chauncey
 *
 * 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 {
    public static String timeSincePretty(long time) {
        return "in " + prettyTime(time, true);
    }

    public static String prettyTime(long time, boolean since) {
        long diff = (since ? System.currentTimeMillis() - time : time - System.currentTimeMillis()) / 1000;
        double day_diff = Math.floor(diff / 86400);

        if (day_diff == 0 && diff < 60)
            return "a few seconds";
        if (diff < 120)
            return "1 minute";
        if (diff < 3600)
            return (int) Math.floor(diff / 60) + " minutes";
        if (diff < 7200)
            return "1 hour";
        if (diff < 86400)
            return (int) Math.floor(diff / 3600) + " hours";
        if (day_diff == 1)
            return "1 day";
        if (day_diff < 7)
            return (int) day_diff + " days";
        if (day_diff == 7)
            return "1 week";
        return (int) Math.ceil(day_diff / 7) + " weeks";
    }
}

Related

  1. timeSince()
  2. timeSince(long lastTimeInMillis)
  3. timeSince(long t)
  4. timeSince(long timestart)
  5. timeSinceNanos(long prevTime)