-4

Why are System.currentTimeMillis() and Calendar.getInstance().getTimeInMillis() different?

import java.util.*;
public class HelloWorld{

     public static void main(String []args){


        long t2 = Calendar.getInstance().getTimeInMillis();
        long t1 = System.currentTimeMillis();

        System.out.println(t1);
        System.out.println(t2);
     }
}

The output is:

1491925013006                                                                                                                                                     
1491925012998
satnam
  • 10,719
  • 5
  • 32
  • 42
ilw
  • 2,499
  • 5
  • 30
  • 54
  • 2
    it is different because you are calling one after another – stinepike Apr 11 '17 at 15:37
  • They look to be equivalent from their Javadoc. – Jacob G. Apr 11 '17 at 15:37
  • They are different because time has moved on between calling one and then calling another. Unless you're running it in a stasis field. – Steve Smith Apr 11 '17 at 15:40
  • 1
    Because 8 milliseconds has elapsed between the call to `getInstance()` and the call to `currentTimeMillis()`. The 8 milliseconds may be inaccurate, depending on the precision (ticks) of the underlying clock. – Andreas Apr 11 '17 at 15:57

1 Answers1

1

They are different because time has passed between when you called one vs when you called the other. Even if you call System.currentTimeMillis() over and over again, it will keep giving you different responses.

satnam
  • 10,719
  • 5
  • 32
  • 42