Java Time Format formatTime(long time)

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

Description

format Time

License

Open Source License

Parameter

Parameter Description
time a parameter

Declaration

public static String formatTime(long time) 

Method Source Code

//package com.java2s;
/*/*from ww  w.  j  av  a  2 s.  c  om*/
 * ? Copyright IBM Corp. 2009,2011
 * 
 * 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 SECOND = 1000L;
    private static final long MINUTE = 60 * SECOND;
    private static final long HOUR = 60 * MINUTE;
    private static final long DAY = 24 * HOUR;

    /**
     * @param time
     * @return
     */
    public static String formatTime(long time) {
        if (time < MINUTE) {
            return (time / SECOND + " seconds"); // $NON-NLS-1$
        } else if (time < HOUR) {
            long mn = time / MINUTE;
            return (mn + " minute(s) " + formatTime(time - (mn * MINUTE))); // $NON-NLS-1$
        } else if (time < DAY) {
            long hours = time / HOUR;
            return (hours + " hour(s) " + formatTime(time - (hours * HOUR))); // $NON-NLS-1$
        } else {
            long days = time / DAY;
            return (days + " day(s) " + formatTime(time - (days * DAY))); // $NON-NLS-1$
        }
    }
}

Related

  1. formatTime(long time)
  2. formatTime(long time)
  3. formatTime(long time)
  4. formatTime(long time)
  5. formatTime(long time)
  6. formatTime(long time)
  7. formatTime(long time)
  8. formatTime(long time)
  9. formatTime(long time)