JAVA计算两个时间的时间差
现在是:2015-07-23 12:59:28
过去是:2015-07-23 09:44:23
要获得两个日期差,差的形式为:xx天xx小时xx分xx秒
示例代码如下:
public static void main(String[] args) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date1 = sdf.parse("2015-07-23 09:44:23");
Date date2 = sdf.parse("2015-07-23 12:59:28");
long l = date2.getTime() - date1.getTime();
long day = l / (24 * 60 * 60 * 1000);
long hour = (l / (60 * 60 * 1000) - day * 24);
long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
System.out.println(day + "天" + hour + "小时" + min +"分" + s + "秒");
}
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/16342.html