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);
	}

	/**
	 * Format millisecond duration with specified format string, the duration is
	 * not timestamp but only a duration of time.
	 *
	 * @param msec   millisecond
	 * @param format format(such as: dd:HH:mm:ss:SSSS),
	 *               the max unit is day and the min unit is millisecond
	 * @return       formatted string
	 */
	public static String formatDuration(long msec, String format) {
		int day = (int) (msec / 86400000);
		int hour = (int) (msec % 86400000 / 3600000);
		int minute = (int) (msec % 86400000 % 3600000 / 60000);
		int second = (int) (msec % 86400000 % 3600000 % 60000 / 1000);
		int millisec = (int) (msec % 86400000 % 3600000 % 60000 % 1000);

		String hourStr = Integer.toString(hour);
		String minuteStr = Integer.toString(minute);
		String secondStr = Integer.toString(second);
		String millisecondStr = Integer.toString(millisec);

		if (format.contains("dd") && hour < 10) {
			hourStr = "0" + hourStr;
		}

		if (format.contains("HH") && minute < 10) {
			minuteStr = "0" + minuteStr;
		}

		if (format.contains("mm") && second < 10) {
			secondStr = "0" + secondStr;
		}

		if (millisec < 10) {
			millisecondStr = "00" + millisecondStr;
		} else if (millisec < 100) {
			millisecondStr = "0" + millisecondStr;
		}

		String result = format.replace("dd", Integer.toString(day));
		result = result.replace("HH", hourStr);
		result = result.replace("mm", minuteStr);
		result = result.replace("ss", secondStr);
		result = result.replace("SSSS", millisecondStr);

		return result;
	}
}

@@ -49,16 +49,49 @@
 l.Date;%0A
+import java.text.ParseException;%0A
 import j
@@ -2743,11 +2743,601 @@
 sult;%0A%09%7D
+%0A%0A%09/**%0A%09 * Convert time from one format to another format.%0A%09 *%0A%09 * @param  oldFormat old format(such as: yyyy-MM-dd HH:mm:ss)%0A%09 * @param  newFormat new format(such as: yyyy/MM/dd HH:mm:ss)%0A%09 * @param  time      old formated time%0A%09 * @return           new formated time%0A%09 */%0A%09public static String convertFormat(String oldFormat, String newFormat, String time) %7B%0A%09%09SimpleDateFormat oldFmt = new SimpleDateFormat(oldFormat);%0A%09%09SimpleDateFormat newFmt = new SimpleDateFormat(newFormat);%0A%0A%09%09try %7B%0A%09%09%09return newFmt.format(oldFmt.parse(time));%0A%09%09%7D catch (ParseException e) %7B%0A%09%09%09return null;%0A%09%09%7D%0A%09%7D
 %0A%7D%0A

add convertFormat in TimeUtils