Here you can find the source of formatTime(long time, TimeUnit unit)
public static String formatTime(long time, TimeUnit unit)
//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)); } }