Format a duration in ms into a string as "Days,Hours,minutes and seconds"
public class Main { public final static long ONE_SECOND = 1000; public final static long ONE_MINUTE = ONE_SECOND * 60; public final static long ONE_HOUR = ONE_MINUTE * 60; public final static long ONE_DAY = ONE_HOUR * 24; public static void millisecondToDHMS(long duration) { long temp = 0; if (duration >= ONE_SECOND) { temp = duration / ONE_DAY; if (temp > 0) { System.out.print(temp + " day"); if (temp > 1) { System.out.print("s"); } } temp = duration / ONE_HOUR; if (temp > 0) { System.out.print(temp + " hour"); if (temp > 1) { System.out.print("s"); } } temp = duration / ONE_MINUTE; if (temp > 0) { System.out.print( temp + " minute"); if (temp > 1) { System.out.print("s"); } } temp = duration / ONE_SECOND; if (temp > 0) { System.out.print(" second"); if (temp > 1) { System.out.print("s"); } } } } public static void main(String args[]) { millisecondToDHMS(System.currentTimeMillis()); } }