Here you can find the source of format(final Date date)
private static String format(final Date date)
//package com.java2s; //License from project: Apache License import java.text.SimpleDateFormat; import java.util.Date; public class Main { private static final long MILLIS_UPPER_BOUND = 1000; private static final long SECONDS_UPPER_BOUND = 60000; private static final long MINUTES_UPPER_BOUND = 3600000; private static final String MILLIS_FORMAT = "SSS"; private static final String SECONDS_MILLIS_FORMAT = "ss:SSS"; private static final String MINUTES_SECONDS_MILLIS_FORMAT = "mm:ss:SSS"; private static final String HOURS_MINUTES_SECONDS_MILLIS_FORMAT = "hh:mm:ss:SSS"; private static String format(final Date date) { final long duration = date.getTime(); final SimpleDateFormat simpleDateFormat; if (duration < MILLIS_UPPER_BOUND) { simpleDateFormat = new SimpleDateFormat(MILLIS_FORMAT); } else if (duration < SECONDS_UPPER_BOUND) { simpleDateFormat = new SimpleDateFormat(SECONDS_MILLIS_FORMAT); } else if (duration < MINUTES_UPPER_BOUND) { simpleDateFormat = new SimpleDateFormat(MINUTES_SECONDS_MILLIS_FORMAT); } else {/*from w w w . j a va 2 s . c o m*/ simpleDateFormat = new SimpleDateFormat(HOURS_MINUTES_SECONDS_MILLIS_FORMAT); } return simpleDateFormat.format(date); } }