1、计算间隔xx天xx小时xx分xx秒
public static String LocalDateTimeIntervalCompute(LocalDateTime startTime,LocalDateTime endTime){
String result="";
LocalDateTime fromDateTime = startTime;
LocalDateTime toDateTime = endTime;
// LocalDateTime fromDateTime = LocalDateTime.parse(startTime, DateTimeFormatter.ofPattern("yyyy-MM" +
// "-dd HH:mm:ss"));
// LocalDateTime toDateTime = LocalDateTime.parse(endTime,DateTimeFormatter.ofPattern("yyyy-MM" +
// "-dd HH:mm:ss"));
LocalDateTime tempDateTime = LocalDateTime.from( fromDateTime );
long days = tempDateTime.until( toDateTime, ChronoUnit.DAYS);
tempDateTime = tempDateTime.plusDays( days );
long hours = tempDateTime.until( toDateTime, ChronoUnit.HOURS);
tempDateTime = tempDateTime.plusHours( hours );
long minutes = tempDateTime.until( toDateTime, ChronoUnit.MINUTES);
tempDateTime = tempDateTime.plusMinutes( minutes );
long seconds = tempDateTime.until( toDateTime, ChronoUnit.SECONDS);
if(days!=0){
result+=days+"天";
}
if(hours!=0){
result+=hours+"小时";
}
if(minutes!=0){
result+=minutes+"分";
}
if(seconds!=0){
result+=seconds+"秒";
}
return result;
}
2、秒数转换成xx天xx小时xx分xx秒
public static String toDayHoursMinSec(Long time){
String result="";
long day = TimeUnit.SECONDS.toDays(time);
long hours = TimeUnit.SECONDS.toHours(time) - TimeUnit.DAYS.toHours(TimeUnit.SECONDS.toDays(time));
long minutes = TimeUnit.SECONDS.toMinutes(time) - TimeUnit.HOURS.toMinutes(TimeUnit.SECONDS.toHours(time));
long seconds = TimeUnit.SECONDS.toSeconds(time) - TimeUnit.MINUTES.toSeconds(TimeUnit.SECONDS.toMinutes(time));
if(day!=0){
result+=day+"天";
}
if(hours!=0){
result+=hours+"小时";
}
if(minutes!=0){
result+=minutes+"分";
}
if(seconds!=0){
result+=seconds+"秒";
}
return result;
}