[记录三]计算两日期之间的时间差

tech2022-09-15  104

计算两时间之间的用时(年月日时分秒)

public String timeSub(String time1,String time2) throws ParseException{//time1 开始时间 time2 结束时间 SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); long newTime1=simpleDateFormat.parse(time1).getTime(); long newTime2=simpleDateFormat.parse(time2).getTime(); long result=newTime2-newTime1; long nm=1000*60;//分 long nh=1000*60*60;//小时 long nd=1000*60*60*24;//天数 long hour=result%nd/nh;//获取相差的小时数 long min=result%nd%nh/nm;//获取相差的分钟数 long mm=result%nd%nh%nm/1000;//获取相差的秒数 long day=result/nd;//获取相应的天数 String re="共用时"; if(day>0) re=re+day+"天"; if(hour>0) re=re+hour+"小时"; if(min>0) re=re+min+"分"; if(mm>0) re=re+mm+"秒"; return re; }
最新回复(0)