Delphi DateUtils has the UnixToDateTime() and DateTimeToUnix() functions for converting between TDateTime and Unix marks, which are expressed as seconds from the Unix era (January 1, 1970 00:00) .: 00 GMT):
// 1325606144 = Jan 3 2012 3:55:44 PM GMT uses DateUtils; var DT: TDateTime; Unix: Int64; begin DT := UnixToDateTime(1325606144); // returns Jan 3 2012 3:55:44 PM Unix := DateTimeToUnix(EncodeDate(2012, 1, 3) + EncodeTime(15, 55, 44, 0)); // returns 1325606144 end;
The Java Date class, on the other hand, is based on milliseconds since the Unix era . This is easy to take into account:
uses DateUtils; function JavaToDateTime(Value: Int64): TDateTime; begin Result := UnixToDateTime(Value div 1000); end; function DateTimeToJava(const Value: TDateTime): Int64; begin Result := DateTimeToUnix(Value) * 1000; end;
As an alternative:
uses SysUtils, DateUtils; // UnixDateDelta is defined in SysUtils... function JavaToDateTime(Value: Int64): TDateTime; begin Result := IncMilliSecond(UnixDateDelta, Value); end; function DateTimeToJava(const Value: TDateTime): Int64; begin Result := MilliSecondsBetween(UnixDateDelta, Value); if Value < UnixDateDelta then Result := -Result; end;
Anyway:
// 1325606144000 = Jan 3 2012 3:55:44 PM GMT var DT: TDateTime; Java: Int64; begin DT := JavaToDateTime(1325606144000); // returns Jan 3 2012 3:55:44 PM Java := DateTimeToJava(EncodeDate(2012, 1, 3) + EncodeTime(15, 55, 44, 0)); // returns 1325606144000 end;
source share