Java Code Examples for java.util.concurrent.TimeUnit.toDays()

The following are Jave code examples for showing how to use toDays() of the java.util.concurrent.TimeUnit class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: RottenFood   File: RottenFood.java   Source Code and License Vote up 6 votes
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;
}
 
Example 2
Project: yadaframework   File: YadaUtil.java   Source Code and License Vote up 6 votes
/**
 * 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;
}
 
Example 3
Project: Blizcord   File: Utils.java   Source Code and License Vote up 5 votes
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);
}