Here you can find the source of toTimeString(long millis, boolean showSeconds, String separatorHours, String separatorMin, String separatorSec)
Parameter | Description |
---|---|
millis | the millis |
showSeconds | the show seconds |
separatorHours | the separator hours |
separatorMin | the separator min |
separatorSec | the separator sec |
public static String toTimeString(long millis, boolean showSeconds, String separatorHours, String separatorMin, String separatorSec)
//package com.java2s; /*//from w w w.j ava2s . c o m * Copyright (c) 2016 Martin Pfeffer * * 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 { /** * To time string string. * * @param millis the millis * @param showSeconds the show seconds * @param showUnits the show units * @return the string */ public static String toTimeString(long millis, boolean showSeconds, boolean showUnits) { int h = (int) (millis / 1000) / 3600; int m = (int) (millis / 1000) % 3600 / 60; int s = (int) (millis / 1000) % 60; if (h == 0 && m == 0) { if (!showUnits) return showSeconds ? String.format("%1d", s) : "0s"; else return showSeconds ? String.format("%1ds", s) : "0s"; } if (h == 0) { if (!showUnits) return showSeconds ? String.format("%02d:%02d", m, s) : String.format("%1d", m); else return showSeconds ? String.format("%1dmin %1ds", m, s) : String.format("%1d min", m); } if (!showUnits) return showSeconds ? String.format("%02d:%02d:%02d", h, m, s) : String.format("%02d:%02d", h, m); else return showSeconds ? String.format("%1dh %1dmin %1ds", h, m, s) : String.format("%1dh %1dmin", h, m); } /** * To time string string. * * @param millis the millis * @param showSeconds the show seconds * @param separatorHours the separator hours * @param separatorMin the separator min * @param separatorSec the separator sec * @return the string */ public static String toTimeString(long millis, boolean showSeconds, String separatorHours, String separatorMin, String separatorSec) { int h = (int) (millis / 1000) / 3600; int m = (int) (millis / 1000) % 3600 / 60; int s = (int) (millis / 1000) % 60; if (h == 0 && m == 0) { if (!showSeconds) return "0min"; else return "0s"; } if (h == 0) { if (separatorMin.equals(":") && !showSeconds) { separatorMin = "min"; } return showSeconds ? String.format("%02d" + separatorMin + "%02d" + separatorSec, m, s) : String.format("%02d" + separatorMin, m); } return showSeconds ? String.format("%02d" + separatorHours + "%02d" + separatorMin + "%02d" + separatorSec, h, m, s) : String.format("%02d" + separatorHours + "%02d" + separatorMin, h, m); } }