- 我们定义两个字符串
String a = "2020-3-1 16:46:00";
String b = "2020-3-1 17:46:00";
- String转date
SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//注意大小写
Date time1 = s.parse(a);
Date time2 = s.parse(b);
- 计算时间差
long ss= (time2 .getTime()-time1 .getTime())/1000;//除以1000是为了转换成秒
long mm= ss/60; // 多少分
long hh= mm/60; // 多少小时
- 我们输出来看看
System.out.println("时间差为:"+ss+"秒");
System.out.println("时间差为:"+mm+"分");
System.out.println("时间差为:"+hh+"小时");