本文整理汇总了Java中java.util.concurrent.TimeUnit.toHours方法的典型用法代码示例。如果您正苦于以下问题:Java TimeUnit.toHours方法的具体用法?Java TimeUnit.toHours怎么用?Java TimeUnit.toHours使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.TimeUnit
的用法示例。
在下文中一共展示了TimeUnit.toHours方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: formatTimeInterval
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
private static String formatTimeInterval(long time, String format){
TimeUnit c = TimeUnit.MILLISECONDS;
long S = c.toSeconds(time);
long M = c.toMinutes(time);
long H = c.toHours(time);
long D = c.toDays(time);
long s = c.toSeconds(time - TimeUnit.MINUTES.toMillis(M));
long m = c.toMinutes(time - TimeUnit.HOURS.toMillis(H));
long h = c.toHours(time - TimeUnit.DAYS.toMillis(D));
String r = format;
r = r.replace("%S", "" + S);
r = r.replace("%M", "" + M);
r = r.replace("%H", "" + H);
r = r.replace("%D", "" + D);
r = r.replace("%s", (s > 9 ? "" : "0") + s);
r = r.replace("%m", (m > 9 ? "" : "0") + m);
r = r.replace("%h", (h > 9 ? "" : "0") + h);
return r;
}
示例2: formatTimeInterval
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
/**
* Convert from an amount of time to a string in the format xxd:hh:mm:ss
* @param amount interval that needs to be formatted
* @param timeUnit the unit of the interval
* @return a formatted string representing the input interval
*/
public static String formatTimeInterval(long amount, TimeUnit timeUnit) {
long totSeconds = timeUnit.toSeconds(amount);
long seconds = totSeconds % 60;
long totMinutes = timeUnit.toMinutes(amount);
long minutes = totMinutes % 60;
long totHours = timeUnit.toHours(amount);
long hours = totHours % 24;
long days = timeUnit.toDays(amount);
String result = String.format("%02d:%02d", minutes, seconds);
if (hours+days>0) {
result = String.format("%02d:", hours) + result;
if (days>0) {
result = String.format("%dd:", days) + result;
}
}
return result;
}
示例3: getBanPoints
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
/**
* Gets the banpoints from {@link #POINT_CONSTANTS} where the hour value matches the constant
*
* @param val The multiplier of the timeunit (e.g. 30)
* @param unit The timeunit (e.g. DAYS)
* @return The constant
*/
public static int getBanPoints(int val, TimeUnit unit) {
if(val == -1) return -1;
int points = (int) (unit.toHours(val) / 6);
if(points > MAX_POINTS) return -1;
return (int) (unit.toHours(val) / 6);
}
示例4: durationToTimeString
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
public static String durationToTimeString(long duration) {
TimeUnit scale = TimeUnit.MILLISECONDS;
long days = scale.toDays(duration);
duration -= TimeUnit.DAYS.toMillis(days);
long hours = scale.toHours(duration);
duration -= TimeUnit.HOURS.toMillis(hours);
long minutes = scale.toMinutes(duration);
duration -= TimeUnit.MINUTES.toMillis(minutes);
long seconds = scale.toSeconds(duration);
return String.format("%d days, %d hours, %d minutes, %d seconds", days, hours, minutes, seconds);
}