Java毫秒转换成日期格式 - 风言枫语

时间:2024-02-19 14:54:15
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Demo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println(converLongTimeToStr(3630000));
	}

	/**
	 * 转换毫秒数成“分、秒”,如“01:53”。若超过60分钟则显示“时、分、秒”,如“01:01:30
	 * 
	 * @param 待转换的毫秒数
	 * */
	public static String converLongTimeToStr(long time) {
		int ss = 1000;
		int mi = ss * 60;
		int hh = mi * 60;

		long hour = (time) / hh;
		long minute = (time - hour * hh) / mi;
		long second = (time - hour * hh - minute * mi) / ss;

		String strHour = hour < 10 ? "0" + hour : "" + hour;
		String strMinute = minute < 10 ? "0" + minute : "" + minute;
		String strSecond = second < 10 ? "0" + second : "" + second;
		if (hour > 0) {
			return strHour + ":" + strMinute + ":" + strSecond;
		} else {
			return strMinute + ":" + strSecond;
		}
	}
}


posted on 2013-09-30 22:08  风言枫语  阅读(1104)  评论(0编辑  收藏  举报