Java Program to get milliseconds between dates

Java 8Object Oriented ProgrammingProgramming

Let’s say the following are the two dates for which we want the milliseconds difference −

LocalDateTime dateOne = LocalDateTime.now();
LocalDateTime dateTwo = LocalDateTime.of(2019, 4, 10, 11, 20);

Now, get the milliseconds between the above two dates with MILLIS.between −

long res = MILLIS.between(dateOne, dateTwo);

Example

import java.time.LocalDateTime;
import static java.time.temporal.ChronoUnit.MILLIS;
public class Demo {
   public static void main(String[] argv) {
      LocalDateTime dateOne = LocalDateTime.now();
      LocalDateTime dateTwo = LocalDateTime.of(2019, 4, 10, 11, 20);
      System.out.printf("Date One = "+dateOne);
      System.out.printf("\nDate Two = "+dateTwo);
      long res = MILLIS.between(dateOne, dateTwo);
      System.out.println("\nMilliseconds between two dates = " + res);
   }
}

Output

Date One = 2019-04-12T11:18:29.654947300
Date Two = 2019-04-10T11:20
Milliseconds between two dates = -172709654
raja
Published on 26-Apr-2019 06:28:35
Advertisements