You can use method between from ChronoUnit.
This method converts those times to same zone (zone from the first argument) and after that, invokes until method declared in Temporal interface:
static long zonedDateTimeDifference(ZonedDateTime d1, ZonedDateTime d2, ChronoUnit unit){
return unit.between(d1, d2);
}
Since both ZonedDateTime and LocalDateTime implements Temporal interface, you can write also universal method for those date-time types:
static long dateTimeDifference(Temporal d1, Temporal d2, ChronoUnit unit){
return unit.between(d1, d2);
}
But keep in mind, that invoking this method for mixed LocalDateTime and ZonedDateTime leads to DateTimeException.
Hope it helps.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…