Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 10th, 2012  |  syntax: None  |  size: 1.52 KB  |  hits: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Converto date in Java to integer and then to Delphi Time
  2. const
  3.   UnixStartDate = 25569.0;
  4.  
  5. function DateTimeToUnixTime(const ADateTime: TDateTime): Cardinal;
  6. begin
  7.   Result := Round(ADateTime - UnixStartDate) * 86400;
  8. end;
  9.  
  10. function UnixTimeToDateTime(const UnixDate: Cardinal): TDateTime;
  11. begin
  12.   Result := UnixDate / 86400 + UnixStartDate;
  13. end;
  14.  
  15. procedure TForm1.Button1Click(Sender: TObject);
  16. var  StartDate: TDateTime;
  17.   UnixDate: Cardinal;
  18. begin
  19.   StartDate := Date();
  20.   Memo1.Lines.Add('Start Date: ' + DateToStr(StartDate));
  21.   UnixDate := DateTimeToUnixTime(StartDate);
  22.   Memo1.Lines.Add('DateTimeToUnixTime = ' + IntToStr(UnixDate));
  23.   Memo1.Lines.Add('UnixTimeToDateTime = ' + DateToStr(UnixTimeToDateTime(UnixDate)));
  24. end;
  25.        
  26. function TzSpecificLocalTimeToSystemTime(
  27.   lpTimeZoneInformation: PTimeZoneInformation;
  28.   lpLocalTime, lpUniversalTime: PSystemTime): BOOL; stdcall; external 'kernel32.dll';
  29.  
  30. function SystemTimeToTzSpecificLocalTime(
  31.   lpTimeZoneInformation: PTimeZoneInformation;
  32.   lpUniversalTime, lpLocalTime: PSystemTime): BOOL; stdcall; external 'kernel32.dll';
  33.  
  34. function JavaToDelphiDateTime(const dt: int64): TDateTime;
  35. var
  36.   t: TSystemTime;
  37. begin
  38.   DateTimeToSystemTime(25569 + (dt / 86400000), t);
  39.   SystemTimeToTzSpecificLocalTime(nil, @t, @t);
  40.   Result := SystemTimeToDateTime(t);
  41. end;
  42.  
  43. function DelphiToJavaDateTime(const dt: TDateTime): int64;
  44. var
  45.   t: TSystemTime;
  46. begin
  47.   DateTimeToSystemTime(dt, t);
  48.   TzSpecificLocalTimeToSystemTime(nil, @t, @t);
  49.   Result := Round((SystemTimeToDateTime(t) - 25569) * 86400000)
  50. end;