一. 用SimpleDateFormat和getTime()计算时间差:
1.1 初始数据:
SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date timeDate1 = simpleFormat.parse("2021-01-05 12:18:16");
Date timeDate2 = simpleFormat.parse("2021-01-05 13:05:27");
long time1 = fromDate1.getTime();
long time2 = toDate1.getTime();
1.2 天数差
int days = (int) ((to1 - from1) / (1000 * 60 * 60 * 24));
1.3 小时差:
int hours = (int) ((to2 - from2) / (1000 * 60 * 60));
1.4 分钟差:
int minutes = (int) ((to3 - from3) / (1000 * 60));
Note:(int): int函数取整的结果是“不大于原数的最大整数”。例如,4.9取整等于4,-5.9取整等于-6。