TimeUtils.java 1.45 KB
package ipipan.clarin.tei.impl.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 *
 * @author mlenart
 */
public class TimeUtils {

	private final static SimpleDateFormat dateFormat = new SimpleDateFormat(
			"yyyy-MM-dd");
	private final static SimpleDateFormat timeFormat = new SimpleDateFormat(
			"HH:mm:ss");

	public static String getTimeStr(Calendar cal) {
		return timeFormat.format(cal.getTime());
		// return String.format(
		// "%s:%s:%s",
		// getStr(cal.get(Calendar.HOUR_OF_DAY)),
		// getStr(cal.get(Calendar.MINUTE)),
		// getStr(cal.get(Calendar.SECOND)));
	}

	public static String getDateStr(Calendar cal) {
		return dateFormat.format(cal.getTime());
		// logger.debug(cal);
		// String res = String.format(
		// "%s-%s-%s",
		// getStr(cal.get(Calendar.YEAR)),
		// getStr(cal.get(Calendar.MONTH)),
		// getStr(cal.get(Calendar.DAY_OF_MONTH)));
		// logger.debug(res);
		// return res;
	}

	public static String getW3CTime(long time) {
		return String.format("PT%sH%sM%sS", getHours(time), getMinutes(time),
				getSeconds(time));
	}

	private static String getStr(long t) {
		return t < 10 ? "0" + t : "" + t;
	}

	private static String getHours(long time) {
		long h = time / 1000 / 3600;
		return getStr(h);
	}

	private static String getMinutes(long time) {
		long m = (time / 1000 / 60) % 60;
		return getStr(m);
	}

	private static String getSeconds(long time) {
		return "" + (time / 1000 % 60) + "." + getStr((time % 1000 / 10));
	}
}