计算 两个时间的直接差
eg:2019-1-10 5:20:26 ~ 2018-12-26 15:40:56
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Task_04 {
public static void main(String[] args) throws ParseException {
SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date timeOne=time.parse("2018-12-26 15:40:56");
Date timeTwo = time.parse("2019-1-10 5:20:26");
/*
getTime 方法返回一个整数值,这个整数代表了从 1970 年 1 月 1 日开始计算到
Date 对象中的时间之间的毫秒数
1分钟=60秒;
1小时=60分钟=3600秒
1天=24小时=3600*24 秒
*/
long timeThree=(timeTwo.getTime()-timeOne.getTime())/1000;
long day=timeThree/(24*3600);
long hour=timeThree%(24*3600)/3600;
long min=timeThree%3600/60;
long sec=timeThree%60;
System.out.println("相差:"+day+"天"+hour+"小时"+min+"分"+sec+"秒");
}
}