Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert 64 bit windows number to time Java

If I wanted to convert a 64 bit number that reprasents time in Windows using Java how would I do this?

The number is 129407978957060010

I'm just very confused as to how I would get this to work. Maths was never my thing :)

Many thanks

like image 496
Simon Avatar asked Mar 04 '11 22:03

Simon


3 Answers

That time is probably representing 100 nanosecond units since Jan 1. 1601. There's 116444736000000000 100ns between 1601 and 1970.

Date date = new Date((129407978957060010-116444736000000000)/10000);
like image 163
nos Avatar answered Nov 06 '22 10:11

nos


Assuming the 64-bit value is a FILETIME value, it represents the number of 100-nanosecond intervals since January 1, 1601. The Java Date class stores the number of milliseconds since January 1, 1970. To convert from the former to the latter, you can do this:

long windowsTime = 129407978957060010; // or whatever time you have

long javaTime = windowsTime / 10000    // convert 100-nanosecond intervals to milliseconds
                - 11644473600000;      // offset milliseconds from Jan 1, 1601 to Jan 1, 1970

Date date = new Date(javaTime);
like image 37
casablanca Avatar answered Nov 06 '22 10:11

casablanca


Java uses Unix Timestamp. You can use an online converter to see your local time.

To use it in java:

Date date = new Date(timestamp);

Update:

It seem that on Windows they have different time offset. So on Windows machine you'd use this calculation to convert to Unix Timestamp:

#include <winbase.h>
#include <winnt.h>
#include <time.h>

void UnixTimeToFileTime(time_t t, LPFILETIME pft)
{
  // Note that LONGLONG is a 64-bit value
  LONGLONG ll;

  ll = Int32x32To64(t, 10000000) + 116444736000000000;
  pft->dwLowDateTime = (DWORD)ll;
  pft->dwHighDateTime = ll >> 32;
}
like image 1
Peter Knego Avatar answered Nov 06 '22 09:11

Peter Knego