Convert the conversion date in Java to an integer and then to Delphi time
I have a server device configuration. I need to use UDP to change the equipment date configuration The server is written in Java and device, and written in Delphi
Therefore, the data flow is as follows:
Java server (Java date) – > UDP (integer date) – > Delphi device (Delphi date)
The problem is that when I pass the date as an integer, Java calculates the milliseconds and Delphi seconds of 1970 I pass the date to the following date: today Gettime() / 1000, but the device regards this as the date of 2008, when we were in 2012
I can change the Java code, but the device is a third party and I can't access its source code
There is a difference between Java and Delphi date parsing. Is this difference allowed?
Editor: Thank you m ДΓΓБД Ll, I noticed that I multiplied by 1000 instead of dividing by it. I now have a better appointment, but it's still wrong (in 2033, it's some kind of situation, now it's 2008)
Solution
UNIX timestamp is the same as that used in Java On the other hand, Delphi's TDateTime is based on the start date of 12:01 a.m. on 18 / 22 / 18 (it is com compatible), so some conversion is required These functions will be achieved; I also added a quick test code to show that the transformation works in both ways
const UnixStartDate = 25569.0; function DateTimeToUnixTime(const ADateTime: TDateTime): Cardinal; begin Result := Round(ADateTime - UnixStartDate) * 86400; end; function UnixTimeToDateTime(const UnixDate: Cardinal): TDateTime; begin Result := UnixDate / 86400 + UnixStartDate; end; procedure TForm1.Button1Click(Sender: TObject); var StartDate: TDateTime; UnixDate: Cardinal; begin StartDate := Date(); Memo1.Lines.Add('Start Date: ' + DateToStr(StartDate)); UnixDate := DateTimeToUnixTime(StartDate); Memo1.Lines.Add('DateTimeToUnixTime = ' + IntToStr(UnixDate)); Memo1.Lines.Add('UnixTimeToDateTime = ' + DateToStr(UnixTimeToDateTime(UnixDate))); end;