【问题标题】:Convert 64 bit windows number to time Java将 64 位 windows 数字转换为时间 Java
【发布时间】:2011-03-04 22:58:01
【问题描述】:

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

号码是129407978957060010

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

非常感谢

【问题讨论】:

  • 你需要弄清楚64位数字代表什么:秒,毫秒?从什么时候开始?
  • 自Windows UTC 启动以来是一个Windows 时间,以毫秒为单位,似乎没有办法转换它?!

标签: java windows time bit


【解决方案1】:

该时间可能代表自Jan 1. 1601 以来的 100 纳秒单位。 1601 年到 1970 年之间有 116444736000000000 100ns。

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

【讨论】:

  • 我想你可能已经做到了,当你看到你所做的事情时,它是如此简单,但如此介意!我对 Windows 中的日期系统一无所知。非常感谢 NOS,我真的很喜欢它! :)
  • 您可以将年份之间的天数计算为:(369*365+(369/4)*1-3)。基本上,每年 365 天,加上闰年每四年一天,每世纪减去一天,在 1700 年、1800 年或 1900 年没有闰年。从那里开始,100ns 滴答声在这里得到回答.
【解决方案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);

【讨论】:

    【解决方案3】:

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

    【讨论】:

    • 刚刚意识到在 Windows 上它们(可能)使用不同的时间戳偏移量。
    【解决方案4】:

    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
    

    【讨论】:

      【解决方案5】:

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

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

      setTime 将 long 表示的自 1970 年 1 月 1 日以来的所有毫秒数相加。

      【讨论】:

      • 这看起来更像是纳秒,所以你可能需要/ 1000000l
      • 实际上,它们可能是 100 纳秒的时间间隔,因为这是 Windows 中唯一常用的 64 位时间值——请参阅FILETIME
      • 没错,我刚刚测试过,返回 4102745 年 8 月 9 日晚上 11:24:20 :)
      • /1000000 给出的日期是 1974 年 2 月 6 日下午 12:39:38。/1000000 给出了 2011 年 1 月 3 日上午 11:36:29。我真的不知道他是哪个日期去吧,我们现在只是在猜测。 @Simon: .setTime 方法接受一个 long 值,因此将您的时间转换为 long 并且 java 将工作。如果确实是 Unix 时间戳,Peter Knego 应该可以工作。
      • 日期应该是 29/1/2011 18:04:55 这让我感到困惑?!
      猜你喜欢
      • 2016-02-08
      • 2017-01-21
      • 1970-01-01
      • 2021-11-18
      • 1970-01-01
      • 2012-07-26
      • 1970-01-01
      • 2023-04-10
      • 2011-12-16
      相关资源
      最近更新 更多