convert minutes to hours in java code example

Example 1: seconds to hours java

int s=123456;
			int sec = s % 60;
		    int min = (s / 60)%60;
		    int hours = (s/60)/60;
            
		    String strSec=(sec<10)?"0"+Integer.toString(sec):Integer.toString(sec);
		    String strmin=(min<10)?"0"+Integer.toString(min):Integer.toString(min);
		    String strHours=(hours<10)?"0"+Integer.toString(hours):Integer.toString(hours);
		    
		    System.out.println(strHours + ":" + strmin + ":" + strSec);

Example 2: java milliseconds to days hours minutes seconds

package com.roytuts.time;
import java.util.concurrent.TimeUnit;
public class MillisToDayHrMinSec {
	public static void main(String[] args) {
		final long milliseconds = 5478965412358l;
		final long dy = TimeUnit.MILLISECONDS.toDays(milliseconds);
		final long hr = TimeUnit.MILLISECONDS.toHours(milliseconds)
				- TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(milliseconds));
		final long min = TimeUnit.MILLISECONDS.toMinutes(milliseconds)
				- TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(milliseconds));
		final long sec = TimeUnit.MILLISECONDS.toSeconds(milliseconds)
				- TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(milliseconds));
		final long ms = TimeUnit.MILLISECONDS.toMillis(milliseconds)
				- TimeUnit.SECONDS.toMillis(TimeUnit.MILLISECONDS.toSeconds(milliseconds));
		System.out
				.println(String.format("%d Days %d Hours %d Minutes %d Seconds %d Milliseconds", dy, hr, min, sec, ms));
	}
}

Tags:

Java Example