用到:java.time,java.util两个jar包,系统默认的包
@Test public void ddd(){ LocalDateTime inDate = convertDateToLDT(DateUtils.strToDate("2021-04-13 17:45", DateUtils.YYYY_MM_DD_HH_MM)); LocalDateTime nowDate = convertDateToLDT(DateUtils.strToDate("2021-04-13 19:30", DateUtils.YYYY_MM_DD_HH_MM)); getMinute(inDate, nowDate); } public String getMinute(LocalDateTime inDate, LocalDateTime nowDate) { Duration duration = java.time.Duration.between(inDate, nowDate); log.info("时间差:" + duration.getSeconds()); long hour = duration.getSeconds() / ChronoUnit.HOURS.getDuration().getSeconds(); long minute = (duration.getSeconds() - ChronoUnit.HOURS.getDuration().getSeconds() * hour) / ChronoUnit.MINUTES.getDuration().getSeconds(); long second = (duration.getSeconds() - ChronoUnit.HOURS.getDuration().getSeconds() * hour) - minute * ChronoUnit.MINUTES.getDuration().getSeconds(); log.info("时间:" + hour + ":" + minute + ":" + second); return hour + ":" + minute + ":" + second; } //Date转换为LocalDateTime public LocalDateTime convertDateToLDT(Date date) { return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()); } //LocalDateTime转换为Date public Date convertLDTToDate(LocalDateTime time) { return Date.from(time.atZone(ZoneId.systemDefault()).toInstant()); }
结果:
时间差:6300
时间:1:45:0