计算两个localDateTime的时间差(小时差),保留一位小数

上代码:

public String LocalDateTimeHourMinus( LocalDateTime startTime, LocalDateTime endTime){
ZoneId zone = ZoneId.systemDefault();
Instant instantStart = startTime.atZone(zone).toInstant();
Instant instantEnd = endTime.atZone(zone).toInstant();
java.util.Date dateStart = Date.from(instantStart);
java.util.Date dateEnd = Date.from(instantEnd);
Calendar instance = Calendar.getInstance();
instance.setTime(dateStart);
long timeInMillis1 = instance.getTimeInMillis();
Calendar instance2 = Calendar.getInstance();
instance2.setTime(dateEnd);
long timeInMillis2 = instance2.getTimeInMillis();
//此处可根据自己需要来计算结果 天/时/分/秒
double hours = (timeInMillis2 - timeInMillis1)/1000/60/60.0;
DecimalFormat df = new DecimalFormat("##.#");
String dff=df.format(hours);
return dff;
}

你可能感兴趣的:(时间计算)