Java examples for Date Time:Duration
Computing the Amount of Time Elapsed Between Two Dates and Times
import java.time.LocalDate; import java.time.LocalTime; import java.time.Month; import static java.time.temporal.ChronoUnit.DAYS; import static java.time.temporal.ChronoUnit.HOURS; import static java.time.temporal.ChronoUnit.MINUTES; public class Main { public static void main(String[] args) { LocalDate ld1 = LocalDate.of(2019, Month.JANUARY, 7); LocalDate ld2 = LocalDate.of(2019, Month.MAY, 18); long days = DAYS.between(ld1, ld2); /*from w w w .j a va 2 s . c om*/ LocalTime lt1 = LocalTime.of(7, 0); LocalTime lt2 = LocalTime.of(9, 30); long hours = HOURS.between(lt1, lt2); long minutes = MINUTES.between(lt1, lt2); System.out.println("Using between (days): " + days); System.out.println("Using between (hours): " + hours); System.out.println("Using between (minutes): " + minutes); // Using the until() method long days2 = ld1.until(ld2, DAYS); long hours2 = lt1.until(lt2, HOURS); long minutes2 = lt1.until(lt2, MINUTES); System.out.println("Using until (days): " + days2); System.out.println("Using until (hours): " + hours2); System.out.println("Using until (minutes): " + minutes2); } }