Java Long Number to Time getTimeFromLong(long diff)

Here you can find the source of getTimeFromLong(long diff)

Description

get Time From Long

License

Open Source License

Declaration

public static String getTimeFromLong(long diff) 

Method Source Code

//package com.java2s;
/**/*w ww .  j a v  a 2 s  .  c  o m*/
 * Copyright 2003-2007 Jive Software.
 *
 * All rights reserved. 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.util.*;

public class Main {
    public static String getTimeFromLong(long diff) {
        final String HOURS = "h";
        final String MINUTES = "min";
        final String SECONDS = "sec";

        final long MS_IN_A_DAY = 1000 * 60 * 60 * 24;
        final long MS_IN_AN_HOUR = 1000 * 60 * 60;
        final long MS_IN_A_MINUTE = 1000 * 60;
        final long MS_IN_A_SECOND = 1000;
        Date currentTime = new Date();
        long numDays = diff / MS_IN_A_DAY;
        diff = diff % MS_IN_A_DAY;
        long numHours = diff / MS_IN_AN_HOUR;
        diff = diff % MS_IN_AN_HOUR;
        long numMinutes = diff / MS_IN_A_MINUTE;
        diff = diff % MS_IN_A_MINUTE;
        long numSeconds = diff / MS_IN_A_SECOND;
        diff = diff % MS_IN_A_SECOND;
        long numMilliseconds = diff;

        StringBuilder buf = new StringBuilder();
        if (numHours > 0) {
            buf.append(numHours + " " + HOURS + ", ");
        }

        if (numMinutes > 0) {
            buf.append(numMinutes + " " + MINUTES + ", ");
        }

        buf.append(numSeconds + " " + SECONDS);

        String result = buf.toString();
        return result;
    }
}

Related

  1. getTextDate(long timeInMs)
  2. getTime(long nanos)
  3. getTime(Long time)
  4. getTime(long time, boolean getTimeOnly, boolean fileSystemSafe)
  5. getTimeFromLong(long diff)
  6. getTimeLong()
  7. getTimeOf12(long time)
  8. getTimeOld(long time)
  9. getTimestampDiff(Long old)