java中把分钟转换为时分秒HH:mm:ss字符串

java中把分钟转换为时分秒HH:mm:ss字符串,第1张

把分钟转换为时分秒:

public static String minConvertHourMinSecond(Integer min) {
		String result = "";
		if (min != null) {
			Integer m = min;
			String format;
			Object[] array;
			Integer days = m / (60 * 24);
			Integer hours = m / (60) - days * 24;
			Integer minutes;
			Integer second;
			if (days > 0) {
				hours += days * 24;
				minutes = m - hours * 60;
				second = ( m - minutes - hours * 60) / 60;
			}else{
				minutes = m - hours * 60 - days * 24 * 60;
				second = (m - minutes - hours * 60) / 60;
			}
			String hoursstr = null;
			String minutesstr  = null;
			String secondstr = null;
			if(hours < 10){
				hoursstr = "0"+hours;
			}else {
				hoursstr = hours + "";
			}
			if(minutes < 10){
				minutesstr="0"+minutes;
			}else {
				minutesstr = minutes + "";
			}
			if(second < 10){
				secondstr="0"+second;
			}else {
				secondstr = second + "";
			}
			format = "%1$s:%2$s:%3$s";
			array = new Object[]{hoursstr, minutesstr,secondstr};
			result = String.format(format, array);
		}
		return result;
	}

下面的方法是分钟转换为天时分:

 

public static String minConvertDayHourMin(Double min) {

		String result = "0分";
		if (min != null) {
			Double m = (Double) min;
			String format;
			Object[] array;
			Integer days = (int) (m / (60 * 24));
			Integer hours = (int) (m / (60) - days * 24);
			Integer minutes = (int) (m - hours * 60 - days * 24 * 60);
			if (days > 0) {
				format = "%1$,d天%2$,d时%3$,d分";
				array = new Object[]{days, hours, minutes};
			} else if (hours > 0) {
				format = "%1$,d时%2$,d分";
				array = new Object[]{hours, minutes};
			} else {
				format = "%1$,d分";
				array = new Object[]{minutes};
			}
			result = String.format(format, array);
		}
		return result;
	}

 

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/737329.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-04-28
下一篇2022-04-28

发表评论

登录后才能评论

评论列表(0条)

    保存