Java Time Format formatTime(long time)

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

Description

format Time

License

Apache License

Declaration

public static String formatTime(long time) 

Method Source Code

//package com.java2s;
/*/*from   w w  w. j ava2s .  c  om*/
 * Copyright (C) 2013 Maciej G?rski
 *
 * 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 formatTime(long time) {
        StringBuffer buffer = new StringBuffer();
        if (time < 0) {
            time = -time;
            buffer.append('-');
        }
        long millisecond = time % 1000;
        time /= 1000;
        long second = time % 60;
        time /= 60;
        long minute = time % 60;
        time /= 60;
        long hour = time;
        if (hour > 0) {
            buffer.append(hour);
            buffer.append(':');
            if (minute < 10) {
                buffer.append('0');
            }
        }
        buffer.append(minute);
        buffer.append(':');
        if (second < 10) {
            buffer.append('0');
        }
        buffer.append(second);
        if (hour == 0 && minute == 0 && second < 20) {
            buffer.append('.');
            if (millisecond < 100) {
                buffer.append('0');
            }
            if (millisecond < 10) {
                buffer.append('0');
            }
            millisecond /= 10;
            if (millisecond > 0) {
                buffer.append(millisecond);
            }
        }
        return buffer.toString();
    }
}

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)