Java Program to get milliseconds between dates


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("
Date Two = "+dateTwo);       long res = MILLIS.between(dateOne, dateTwo);       System.out.println("
Milliseconds between two dates = " + res);    } }

Output

Date One = 2019-04-12T11:18:29.654947300
Date Two = 2019-04-10T11:20
Milliseconds between two dates = -172709654

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jul-2019

599 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements