Java Data Type How to - Count milliseconds between two dates








Question

We would like to know how to count milliseconds between two dates.

Answer

import static java.time.temporal.ChronoUnit.MILLIS;

import java.time.LocalDateTime;
public class Main {
  public static void main(String[] argv) {
    long millisBetween = MILLIS.between(LocalDateTime.now(), LocalDateTime.now().plusMinutes(1));
    System.out.println("millisBetween = " + millisBetween);
  }
}

The code above generates the following result.