将64位窗口编号转换为时间Java

问题描述 投票:8回答:5

如果我想在Windows中使用Java转换表示时间的64位数字,我该怎么做?

号码是129407978957060010

关于如何让它发挥作用,我感到非常困惑。数学从来都不是我的事:)

非常感谢

java windows time bit
5个回答
15
投票

Jan 1. 1601以来,那段时间可能代表100纳秒的单位。在1601年和1970年之间有116444736000000000 100ns。

Date date = new Date((129407978957060010-116444736000000000)/10000);

2
投票

假设64位值是FILETIME值,它表示自1601年1月1日以来100纳秒间隔的数量.Java Date类存储自1970年1月1日以来的毫秒数。要从前者转换为后者,你可以这样做:

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);

1
投票

Java使用Unix Timestamp。您可以使用online converter查看当地时间。

要在java中使用它:

Date date = new Date(timestamp);

更新:

似乎在Windows上他们有different time offset。因此,在Windows机器上,您将使用此计算转换为Unix时间戳:

#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;
}

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

-1
投票

我假设时间是一个很长的数字。

 Date temp = new Date();
 temp.setTime((129407978957060010-116444736000000000)/10000);

setTime添加了自1970年1月1日以来long所代表的所有毫秒数。

© www.soinside.com 2019 - 2024. All rights reserved.