Java 获取两个时间的时间查 如 1 天 2 小时 30 分钟

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateUtil {
public static void main(String[] args) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date1 = format.parse("2012-10-11 07:18:54");
Date date2 = format.parse("2012-12-11 09:18:44");
String diff = diffDate(date1.getTime(), date2.getTime());
System.out.println(diff);
}
/**
*
获取两个时间的时间查 如
1

2
小时
30
分钟
*/
public static String diffDate(long from, long to) {
String diff = "";
long nd = 1000 * 24 * 60 * 60;
long nh = 1000 * 60 * 60;
long nm = 1000 * 60;
// long ns = 1000;
//
获得两个时间的毫秒时间差异
long _diff = to - from;
if (_diff <= 0) {
diff = "0d";
return diff;
}
//
计算差多少天
long day = _diff / nd;
//
计算差多少小时
long hour = _diff % nd / nh;
//
计算差多少分钟
long min = _diff % nd % nh / nm;
//
计算差多少秒
//
输出结果
// long sec = diff % nd % nh % nm / ns;
if (day > 0) {
diff += day + "d ";
}
if (hour > 0) {
diff += hour + "h ";
}
if (min > 0) {
diff += min + "m";
}
return diff;
}

猜你喜欢

转载自zysnba.iteye.com/blog/2410863