0

I'm wondering why I always get a difference between this date in milliseconds.

Any idea ?

Here is the output :

date = 1572794103293 ms 
date2 = 1572794103341 ms  
date3 = 1572794103341 ms  
date4 = 1572794103341 ms

and here is the code :

Date date = createDate();
Date date2 = createDate();
Date date3 = createDate();
Date date4 = createDate();
System.out.println(date.getTime());
System.out.println(date2.getTime());
System.out.println(date3.getTime());
System.out.println(date4.getTime());

private static Date createDate() {
      Calendar c = GregorianCalendar.getInstance();
        c.set(2019, Calendar.NOVEMBER, 03, 16, 15, 03);
        return c.getTime();
}
Elarbi Mohamed Aymen
  • 1,617
  • 2
  • 14
  • 26
Tokishiro
  • 11
  • 2
  • 2
    Because you don't set the milliseconds with `set` so each `GregorianCalendar` will use it's own. `set(int year, int month, int date, int hourOfDay, int minute, int second)`. – AxelH Apr 25 '18 at 13:22
  • Oh, I thought the question was why there is a difference between `date` and the rest, but none between `date2`, `date3` and `date4`. – Max Vollmer Apr 25 '18 at 13:25
  • @MaxVollmer Yes i'd like an answer to that to. – Tokishiro Apr 25 '18 at 13:31
  • There is no difference because the call was to quick probably. It doesn't take much time to create an instance. – AxelH Apr 25 '18 at 13:33
  • @AxelH Yes, but it's reproducibly quicker on any consecutive call after the first. See my answer. – Max Vollmer Apr 25 '18 at 13:34

2 Answers2

1

In the documentation of Calendar.set, it is said :

Sets the values for the fields YEAR, MONTH, DAY_OF_MONTH, HOUR, MINUTE, and SECOND. Previous values of other fields are retained. If this is not desired, call clear() first.

The reason is that not all fields are set with this method, in you case, you don't have MILLISECOND set. So it keep the value when the instance was created.

The call of Calendar.clear will

Sets all the calendar field values and the time value (millisecond offset from the Epoch) of this Calendar undefined.

A quick example :

    Calendar c = GregorianCalendar.getInstance();
    c.clear();
    c.set(2019, Calendar.NOVEMBER, 03, 16, 15, 03);

    System.out.println(c.getTimeInMillis());

1572794103000

Milliseconds being undefined will give 0

AxelH
  • 14,325
  • 2
  • 25
  • 55
1

In addition to the comments and AxelH's answer mentioning the fact that the milliseconds of a Calendar instance aren't changed by Calendar.set, there is still the curiosity that you get the same value for date2, date3 and date4, but a different value for date.

This is because the very first time you call createDate() the JVM has to initialize the Date class, which happens after Calendar c was initialized.

Thus on the first call c.getTime() needs more time than on the consecutive calls, which you can see as the difference between the value of date and the other 3 instances.

If you add a call to new Date() before the first call to createDate(), the difference between each value should be the same.

Please note that this does not fix your issue, it just hides it if your machine is fast enough. It's merely an explanation for the particular values you get.

Max Vollmer
  • 8,412
  • 9
  • 28
  • 43
  • That's not an important problem, those calls will never be done in sequence, it would be call in different time, different request, so the milliseconds values would be more "random". – AxelH Apr 26 '18 at 05:49