package com.coolerfall.easyutil;

import java.sql.Date;
import java.text.SimpleDateFormat;

/**
 * Contains some time/data utils.
 *
 * @author Vincent Cheung
 * @since  Mar. 31, 2015
 */
public class TimeUtils {
	/**
	 * Format millisecond according to format.
	 *
	 * @param  millis milliseconds
	 * @param  format date format(such as: yyyy-MM-dd HH:mm:ss)
	 * @return        formatted date string
	 */
	public static String formatMillis(long millis, String format) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
		Date date = new Date(millis);

		return dateFormat.format(date);
	}

	/**
	 * Format unix timestamp according to format.
	 *
	 * @param  unixTime unix timestamp
	 * @param  format   date format(such as: yyyy-MM-dd HH:mm:ss)
	 * @return          formatted date string
	 */
	public static String formatUnix(long unixTime, String format) {
		return formatMillis(unixTime * 1000, format);
	}

	/**
	 * Format current timestamp according to date format.
	 *
	 * @param  format date format(such as: yyyy-MM-dd HH:mm:ss)
	 * @return        formatted date string
	 */
	public static String formatNow(String format) {
		return formatMillis(System.currentTimeMillis(), format);
	}
}

@@ -1197,11 +1197,1524 @@
 mat);%0A%09%7D
+%0A%0A%09/**%0A%09 * Format millisecond duration with specified format string, the duration is%0A%09 * not timestamp but only a duration of time.%0A%09 *%0A%09 * @param msec   millisecond%0A%09 * @param format format(such as: dd:HH:mm:ss:SSSS),%0A%09 *               the max unit is day and the min unit is millisecond%0A%09 * @return       formatted string%0A%09 */%0A%09public static String formatDuration(long msec, String format) %7B%0A%09%09int day = (int) (msec / 86400000);%0A%09%09int hour = (int) (msec %25 86400000 / 3600000);%0A%09%09int minute = (int) (msec %25 86400000 %25 3600000 / 60000);%0A%09%09int second = (int) (msec %25 86400000 %25 3600000 %25 60000 / 1000);%0A%09%09int millisec = (int) (msec %25 86400000 %25 3600000 %25 60000 %25 1000);%0A%0A%09%09String hourStr = Integer.toString(hour);%0A%09%09String minuteStr = Integer.toString(minute);%0A%09%09String secondStr = Integer.toString(second);%0A%09%09String millisecondStr = Integer.toString(millisec);%0A%0A%09%09if (format.contains(%22dd%22) && hour %3C 10) %7B%0A%09%09%09hourStr = %220%22 + hourStr;%0A%09%09%7D%0A%0A%09%09if (format.contains(%22HH%22) && minute %3C 10) %7B%0A%09%09%09minuteStr = %220%22 + minuteStr;%0A%09%09%7D%0A%0A%09%09if (format.contains(%22mm%22) && second %3C 10) %7B%0A%09%09%09secondStr = %220%22 + secondStr;%0A%09%09%7D%0A%0A%09%09if (millisec %3C 10) %7B%0A%09%09%09millisecondStr = %2200%22 + millisecondStr;%0A%09%09%7D else if (millisec %3C 100) %7B%0A%09%09%09millisecondStr = %220%22 + millisecondStr;%0A%09%09%7D%0A%0A%09%09String result = format.replace(%22dd%22, Integer.toString(day));%0A%09%09result = result.replace(%22HH%22, hourStr);%0A%09%09result = result.replace(%22mm%22, minuteStr);%0A%09%09result = result.replace(%22ss%22, secondStr);%0A%09%09result = result.replace(%22SSSS%22, millisecondStr);%0A%0A%09%09return result;%0A%09%7D
 %0A%7D%0A

add format duration