windows 将 64 位窗口数转换为时间 Java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5200192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert 64 bit windows number to time Java
提问by Simon
If I wanted to convert a 64 bit number that reprasents time in Windows using Java how would I do this?
如果我想使用 Java 在 Windows 中转换一个代表时间的 64 位数字,我该怎么做?
The number is 129407978957060010
号码是129407978957060010
I'm just very confused as to how I would get this to work. Maths was never my thing :)
我只是很困惑我如何让它发挥作用。数学从来不是我的事:)
Many thanks
非常感谢
回答by nos
That time is probably representing 100 nanosecond units since Jan 1. 1601. There's 116444736000000000 100ns between 1601 and 1970.
自1601年1 月 1 日以来,该时间可能代表 100 纳秒单位。在 1601 年和 1970 年之间有 116444736000000000 100ns。
Date date = new Date((129407978957060010-116444736000000000)/10000);
回答by casablanca
Assuming the 64-bit value is a FILETIMEvalue, it represents the number of 100-nanosecond intervals since January 1, 1601. The Java Dateclass stores the number of milliseconds since January 1, 1970. To convert from the former to the latter, you can do this:
假设64位的值是一个FILETIME值,它表示从1601年1月1日起间隔100纳秒Date的次数。Java类存储的是从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);
回答by Peter Knego
Java uses Unix Timestamp. You can use an online converterto see your local time.
Java 使用Unix 时间戳。您可以使用在线转换器查看本地时间。
To use it in java:
要在 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:
似乎在 Windows 上它们有不同的时间偏移。因此,在 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;
}
回答by user989383
public static void main(String as[]){
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
回答by Sorean
I assume that the time is a long number.
我假设时间是一个很长的数字。
Date temp = new Date();
temp.setTime((129407978957060010-116444736000000000)/10000);
setTime adds all the milliseconds that the long represents since January 1, 1970.
setTime 将 long 表示自 1970 年 1 月 1 日以来的所有毫秒数相加。

