Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert milliseconds to TDateTime in Java

I'm in an Android project that requires saving a file with TDateTime type (Delphi). I have my date in milliseconds, but I don't know how to convert milliseconds to TDateTime.

I have something like this:

Date dateInMillis = new Date(System.currentTimeMillis());
double dateInDouble = ???;

I'll be glad for any tips that can help me to resolve this.

like image 341
Daniel S. Avatar asked Dec 20 '22 00:12

Daniel S.


1 Answers

Delphi's TDateTime measures time in days. Java follows the Unix standard and measures in milliseconds. To convert between the two you need to scale by the number of milliseconds in a day, 86400000.

The other difference is that the two systems use a different epoch. The Unix epoch, as used by Java, is 00:00, 1 Jan 1970. The Delphi epoch is 00:00, 30 December 1899. The Unix epoch, represented as a Delphi TDateTime is 25569.

So, to convert from milliseconds from the Unix epoch, to days from the Delphi epoch you perform the following calculation:

double delphiDateTime = unixMillis/86400000.0 + 25569.0;
like image 159
David Heffernan Avatar answered Dec 24 '22 00:12

David Heffernan