时间差的计算

SimpleDateFormat simpleFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
try {
Date xdsj = (Date) simpleFormat.parse("2017-09-18 10:10:30");
Date jzsj = (Date) simpleFormat.parse("2017-09-20 16:30:10");
long xdsjKs = xdsj.getTime();
long jzsjJs = jzsj.getTime();
long sjc = jzsjJs - xdsjKs;
int day = (int) (sjc / (1000 * 60 * 60 * 24));// 剩余的天数
int hour = (int) ((sjc % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));// 剩余的小时数
int minute = (int) (((sjc % (1000 * 60 * 60 * 24)) % (1000 * 60 * 60)) / (1000 * 60));// 剩余的分钟数
int second = (int) ((((sjc % (1000 * 60 * 60 * 24)) % (1000 * 60 * 60)) % (1000 * 60)) / 1000);
System.out.println("天:" + day + "小时:" + hour + "分钟:" + minute
+ "秒:" + second);
} catch (ParseException e) {
e.printStackTrace();
}

猜你喜欢

转载自blog.csdn.net/jiuweitianhu_12345/article/details/78037949