Java Hour Format formatTime(long time, TimeUnit unit)

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

Description

format Time

License

Open Source License

Declaration

public static String formatTime(long time, TimeUnit unit) 

Method Source Code

//package com.java2s;
/**//from  w w w  .ja v  a 2  s .  c o  m
 * SimpleTeamPvP
 * Copyright (C) 2015 Max Lee (https://github.com/Phoenix616/)
 * <p/>
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the Mozilla Public License as published by
 * the Mozilla Foundation, version 2.
 * <p/>
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * Mozilla Public License v2.0 for more details.
 * <p/>
 * You should have received a copy of the Mozilla Public License v2.0
 * along with this program. If not, see <http://mozilla.org/MPL/2.0/>.
 */

import java.text.SimpleDateFormat;
import java.util.Date;

import java.util.TimeZone;
import java.util.concurrent.TimeUnit;

public class Main {
    public static String formatTime(long time, TimeUnit unit) {
        String format = "HH:mm:ss";
        if (unit.toHours(time) < 1) {
            format = "mm:ss";
        }
        return formatTime(time, unit, format);
    }

    public static String formatTime(long time, TimeUnit unit, String format) {
        long millis = unit.toMillis(time);
        TimeZone tz = TimeZone.getTimeZone("UTC");
        SimpleDateFormat df = new SimpleDateFormat(format);
        df.setTimeZone(tz);

        return df.format(new Date(millis));
    }
}

Related

  1. formatTime(long aTime)
  2. formatTime(long ms)
  3. formatTime(long time)
  4. formatTime(long time)
  5. formatTime(long time)
  6. formatTime(String dateTimeString)
  7. formatTime_(String time)
  8. formatTime_1(long time_value)
  9. formatTime_2(long time_value, long frame_rate)