Java - Period Between Two Dates and Times

Introduction

There are two ways to get the amount of time between two dates and times.

  • Use the between() method on one of the constants in the ChronoUnit enum.
  • Use the until() method on one of the datetime-related classes, for example, LocalDate, LocalTime, LocalDateTime, ZonedDateTime, etc.

The ChronoUnit enum has a between() method, which takes two datetime objects and returns a long.

The until() method takes two parameters.

The first parameter is the end date or time.

The second parameter is the time unit in which to compute the elapsed time.

The following code uses both methods to compute the amount of time between two dates and times.

Demo

import static java.time.temporal.ChronoUnit.DAYS;
import static java.time.temporal.ChronoUnit.HOURS;
import static java.time.temporal.ChronoUnit.MINUTES;

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Month;

public class Main {
  public static void main(String[] args) {
    LocalDate ld1 = LocalDate.of(2014, Month.JANUARY, 7);
    LocalDate ld2 = LocalDate.of(2014, Month.MAY, 18);
    long days = DAYS.between(ld1, ld2);

    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);
  }//from ww  w  .j  av  a  2  s . co m
}

Result

Related Topic