Java Long Number to Time getTimeTagged(long time, boolean round)

Here you can find the source of getTimeTagged(long time, boolean round)

Description

Return a readable String of the amount of time since a provided time.

License

Apache License

Parameter

Parameter Description
time The provided time.
round Round to an int.

Return

The readable String.

Declaration

public static String getTimeTagged(long time, boolean round) 

Method Source Code


//package com.java2s;
/*/*from ww  w.  j  ava2  s. c  o  m*/
 * Copyright 2014 Alex Bennett & 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.
 */

import java.text.DecimalFormat;

import java.util.concurrent.TimeUnit;

public class Main {
    /**
     * Return a readable String of the amount of time since a provided time.
     *
     * @param time  The provided time.
     * @param round Round to an int.
     * @return The readable String.
     */
    public static String getTimeTagged(long time, boolean round) {
        DecimalFormat format = round ? new DecimalFormat("#") : new DecimalFormat("#.##");
        if (getHours(time) >= 1)
            return format.format(getHours(time)) + "h";
        else if (Double.valueOf(format.format(getMinutes(time))) >= 1)
            return format.format(getMinutes(time)) + "m";
        else
            return format.format(getSeconds(time)) + "s";
    }

    /**
     * Return the hours since the provided time.
     *
     * @param time The provided time.
     * @return Hours.
     */
    public static double getHours(long time) {
        return (double) TimeUnit.MILLISECONDS.toHours(time - System.currentTimeMillis());
    }

    /**
     * Return the minutes since the provided time.
     *
     * @param time The provided time.
     * @return Minutes.
     */
    public static double getMinutes(long time) {
        return (double) TimeUnit.MILLISECONDS.toMinutes(time - System.currentTimeMillis());
    }

    /**
     * Return the seconds since the provided time.
     *
     * @param time The provided time.
     * @return Seconds.
     */
    public static double getSeconds(long time) {
        return (double) TimeUnit.MILLISECONDS.toSeconds(time - System.currentTimeMillis());
    }
}

Related

  1. getTimestampDiff(Long old)
  2. getTimeStr(long t)
  3. getTimeStr(long timeLong)
  4. getTimeString(long nanoTime)
  5. getTimeString(long time)
  6. getTimeText(Long oldTime)
  7. getUploadPath(String fileName, long time)
  8. logDuration(Logger logger, Level level, long startTime, String message)
  9. long2Str(long time)