计算两个日期相差多少天小时分钟秒等_学习记录

计算两个日期相差多少天小时分钟秒等_学习记录

package p1;

/**
 * 计算两个日期相差多少天小时分钟秒等
 * @author Guozhu Zhu
 * @date 2018/7/22
 * @version 1.0
 */
import java.util.Date;

public class Test08 {
	
	public static void main(String[] args) {
		Date nowDate = new Date(2018, 7, 22, 10, 2, 10);
		Date endDate = new Date(2018, 7, 22, 20, 3, 20);
		System.out.println(getDatePoor(endDate, nowDate));
	}
	
	public static String getDatePoor(Date endDate, Date nowDate) {	 
	    long nd = 1000 * 24 * 60 * 60;
	    long nh = 1000 * 60 * 60;
	    long nm = 1000 * 60;
	    long ns = 1000;
	    // 获得两个时间的毫秒时间差异
	    long diff = endDate.getTime() - nowDate.getTime();
	    // 计算差多少天
	    long day = diff / nd;
	    // 计算差多少小时
	    long hour = diff % nd / nh;
	    // 计算差多少分钟
	    long min = diff % nd % nh / nm;
	    // 计算差多少秒
	    long s = diff % nd % nh % nm / ns;
	    // 输出结果
	    return day + "天" + hour + "小时" + min + "分钟" + s + "秒";
	}

}

猜你喜欢

转载自blog.csdn.net/weixin_37770023/article/details/81155129