如果是String类型,需要转成date类型:
如:
String startTime = "2018-01-01 10:30:30";String endTime = "2018-01-08 11:45:45";//看自己的时间格式选择对应的转换对象SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");//转换成date类型Date start = sdf.parse(startTime);Date end = sdf.parse(endTime);//获取毫秒数Long startLong = start.getTime();Long endLong = end.getTime();//计算时间差,单位毫秒Long ms = endLong-startLong;//时间差转换为 \天\时\分\秒 String time = longTimeToDay(ms);//转换函数,可以封装成公用方法public static String longTimeToDay(Long ms){ Integer ss = 1000; Integer mi = ss * 60; Integer hh = mi * 60; Integer dd = hh * 24; Long day = ms / dd; Long hour = (ms - day * dd) / hh; Long minute = (ms - day * dd - hour * hh) / mi; Long second = (ms - day * dd - hour * hh - minute * mi) / ss; Long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss; StringBuffer sb = new StringBuffer(); if(day > 0) { sb.append(day+"天"); } if(hour > 0) { sb.append(hour+"小时"); } if(minute > 0) { sb.append(minute+"分"); } if(second > 0) { sb.append(second+"秒"); } if(milliSecond > 0) { sb.append(milliSecond+"毫秒"); } return sb.toString(); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48