很多方法,这里介绍两个很方便的实现
Demo1
@Test
public void tr(){
final LocalDateTime fromDate = LocalDateTime.now();
final LocalDateTime toDate = LocalDateTime.now().plusHours(56);
long minutes = ChronoUnit.MINUTES.between(fromDate, toDate);
long hours = ChronoUnit.HOURS.between(fromDate, toDate);
System.out.println(minutes);
System.out.println(hours);
}
Demo2
@Test
public void tr2(){
final LocalDateTime fromDate = LocalDateTime.now();
final LocalDateTime toDate = LocalDateTime.now().plusHours(56);
final Duration duration = Duration.between(fromDate, toDate);
final long minuts = duration.toMinutes();
final long hours = duration.toHours();
System.out.println(minuts);
System.out.println(hours);
}
格式化操作如下,把项目代码粘过来了,直接用的java8的stream操作,还有hutool包,从Page对象中处理的逻辑,这里有更多Java8 Stream的操作
Page<SyncRecordVo> vos = all.map(e -> JSONUtil.toBean(JSONUtil.toJsonStr(e), SyncRecordVo.class));
vos.map(e -> {
e.setSyncTime(DateUtil.formatBetween(ChronoUnit.MILLIS.between(e.getStartTime(), e.getEndTime()), BetweenFormater.Level.SECOND));
if (e.getSyncStatus().equals(SyncEnum.SyncStatus.SYNC_FAIL.getKey())) {
e.setIsOperate(YesOrNoEnum.YES.getCode());
} else {
e.setIsOperate(YesOrNoEnum.NO.getCode());
}
return e;
}
);
这里的格式化意思是,将两个LocalDateTime的时间差,转成时分秒格式
12小时12分12秒