11

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

Simon
  • 133
  • 3
  • 6
  • You need to clarify what the 64-bit number represents: second, milliseconds? Since what time? – payne Mar 04 '11 at 23:03
  • Check out this post http://stackoverflow.com/questions/5199478/java-display-windows-utc-time/5199640#5199640 – Bala R Mar 04 '11 at 23:05
  • It is a Windows time in milliseconds since Windows UTC started, there does not seem to be a way to convert it?! – Simon Mar 04 '11 at 23:46

4 Answers4

19

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);
nos
  • 223,662
  • 58
  • 417
  • 506
  • I think you may have done it, it's so simple when you look at what you have done but so mind messing! I don't know anything about the date system in Windows. Thank you so much NOS I really do appericiate it! :) – Simon Mar 04 '11 at 23:53
  • You can calculate # of days between the years as: (369*365+(369/4)*1-3). Basically, 365 days per year, plus one day per four years for leap year, minus a day per century b/c there's no leap year in the years 1700, 1800, or 1900. From there, the 100ns ticks work out as answered here. – Nils Sep 22 '13 at 18:58
3

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);
casablanca
  • 69,683
  • 7
  • 133
  • 150
1

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;
}
Peter Knego
  • 79,991
  • 11
  • 123
  • 154
0

public static void main(String as[]){

     String windowNTTimeStr = "131007981071882420";
     String finalDate = "";
    try {
//Windows NT time is specified as the number of 100 nanosecond intervals since January 1, 1601.
//UNIX time is specified as the number of seconds since January 1, 1970. There are 134,774 days (or 11,644,473,600 seconds) between these dates.
//NT to Unix : Divide by 10,000,000 and subtract 11,644,473,600.
//Unix to NT : Add 11,644,473,600 and multiply by 10,000,000

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Long windowsTime = Long.parseLong(windowNTTimeStr);
            long javaTime = windowsTime / 10000 - 11644473600000L;
            Date date = new Date(javaTime);
            Calendar c = Calendar.getInstance();
            c.setTime(new Date(javaTime));

            Calendar cCurrent = Calendar.getInstance();
            cCurrent.setTime(new Date());
            cCurrent.add(Calendar.YEAR, 100);

            if (!(c.getTime().getYear() > cCurrent.getTime().getYear())) {
                finalDate = sdf.format(c.getTime());
            }
        } catch (Exception e) {
            finalDate = null;
        }
        System.out.println(" Final Date is "+finalDate);
 }  //Expected out put Final Date is 2016-02-24 20:05:07
user989383
  • 391
  • 4
  • 3