TL; DR
aDate.toInstant() .toEpochMilli()
java.time
The modern approach uses java.time classes. They crowd out difficult old heritage classes such as java.util.Date .
Instant instant = Instant.now(); // Capture current moment in UTC.
1970-01-01T00:00:00Z millisecond counter from the 1970-01-01T00:00:00Z .
long millis = instant.toEpochMilli() ;
Conversion
If the java.util.Date object is passed to you, convert it to java.time. Call the new methods added to the old classes.
Instant instant = myJavaUtilDate.toInstant() ; long millis = instant.toEpochMilli() ;
Basil bourque
source share