Java examples for java.util:Date Format
format Elapsed Time
//package com.java2s; import java.util.Formatter; import java.util.Locale; public class Main { public static void main(String[] argv) throws Exception { long elapsedSeconds = 2; System.out.println(formatElapsedTime(elapsedSeconds)); }/* www .ja v a 2s .c o m*/ private static final String ELAPSED_TIME_HMMSS = "%1$d:%2$02d:%3$02d"; private static final String ELAPSED_TIME_MMSS = "%1$02d:%2$02d"; public static CharSequence formatElapsedTime(long elapsedSeconds) { long hours = 0; long minutes = 0; long seconds = 0; if (elapsedSeconds >= 3600) { hours = elapsedSeconds / 3600; elapsedSeconds -= hours * 3600; } if (elapsedSeconds >= 60) { minutes = elapsedSeconds / 60; elapsedSeconds -= minutes * 60; } seconds = elapsedSeconds; Formatter f = new Formatter(new StringBuilder(10), Locale.getDefault()); if (hours > 0) { return f.format(ELAPSED_TIME_HMMSS, hours, minutes, seconds) .toString(); } else { return f.format(ELAPSED_TIME_MMSS, minutes, seconds).toString(); } } }