Java 7 nio . 2文件。getLastModifiedTime時區

[英]Java 7 NIO.2 Files.getLastModifiedTime time zone


I'm writing a program which needs to determine files/directories last modified time. I want to handle this time using Joda Time, and I'm using Java 7 NIO.2 class Files to get file last modified time. Its getLastModifiedTime() method returns an instance of FileTime class, which has convenient method toMillis(), whose result I pass to Joda Time DateTime class constructor:

我正在編寫一個程序,它需要確定上次修改的文件/目錄。我想使用Joda時間來處理這個時間,我使用Java 7 NIO.2類文件來獲得最后修改時間的文件。它的getLastModifiedTime()方法返回一個FileTime類的實例,該實例具有方便的方法toMillis(),其結果是我傳遞給Joda Time DateTime類構造函數:

new DateTime(Files.getLastModifiedTime(path).toMillis());

However, I have a feeling that I'm doing this wrong, since DateTime(long) constructor explicitly mentions that DateTime instance will be created with default time zone. FileTime docs, however, do not mention its time zone anywhere. I looked through FileTime code; it seems to be very simple, and its toString() method suggests that it is using UTC time zone (it creates a Calendar in UTC time zone and sets its milliseconds directly).

但是,我有一種感覺,我做錯了,因為DateTime(long)構造函數明確提到了DateTime實例將被創建為默認時區。然而,FileTime文檔沒有提到它的時區。我查看了FileTime代碼;它看起來非常簡單,它的toString()方法表明它使用UTC時區(它在UTC時區中創建一個日歷,並直接設置它的毫秒)。

So, does FileTime uses UTC or local time? What is the correct way to convert FileTime to DateTime?

那么,FileTime是否使用UTC或本地時間?將FileTime轉換為DateTime的正確方法是什么?

2 个解决方案

#1


6  

A Java millisecond timestamp is a UTC timestamp. It's what FileTime.toMillis() returns, and what the DateTime constructor expects. The same applies to other Java API methods; e.g. the System.currentTimeMillis() method, the java.util.Date constructor, and so on.

Java毫秒時間戳是UTC時間戳。它是FileTime.toMillis()返回的內容,以及DateTime構造函數所期望的。這同樣適用於其他Java API方法;currenttimemillis()方法,java.util。日期構造函數,等等。

They all work the same way. And, indeed, so do other Unix / Linux / OSX library methods in other programming languages.

他們的工作方式都是一樣的。而且,實際上,其他編程語言中的其他Unix / Linux / OSX庫方法也是如此。

The only case where this breaks is if someone configures / sets the system clock incorrectly.

唯一的例外是,如果某人配置/設置系統時鍾錯誤。

#2


1  

FileTime.toMillis() API says it returns the value in milliseconds, since the epoch (1970-01-01T00:00:00Z). new DateTime(millis) creates a DateTime instance wich holds time in milliseconds from the Java epoch of 1970-01-01T00:00:00Z and Chronology in the default time zone which determines how the millisecond value is converted into the date time fields.

tomillis () API說它從時間(1970-01- 01t00:00 - 00z)開始以毫秒為單位返回值。新DateTime(millis)創建了一個DateTime實例,它以毫秒為單位,從1970-01- 01t00:00 z的Java時代到默認時區的時間,它決定了毫秒值是如何轉換為日期時間字段的。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/08/26/72f4d0b7a6d0ce965ed6d2d48baa558d.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com