Java .lastModified format

2 ビュー (過去 30 日間)
DCagigas
DCagigas 2017 年 8 月 4 日
コメント済み: DCagigas 2017 年 8 月 7 日
Hi everyone,
I am trying to implement a (date,time) check using the ".lastModified" field of a Java file object.
However, I am do not understand the format of the answer:
>> FileObj = java.io.File(getenv('WINDIR'));
>> FileObj.lastModified ans = 1.498033301774000e+12
Are these the milliseconds elapsed since 01.Jan.1970? If so the following code should provide today's date (04.Aug.2017):
>> secs = FileObj.lastModified/1000;
>> [today.y,today.m,today.d,today.h,today.mi,today.s] = datevec(datenum([1970 1 1 0 0 secs]));
>> today
today =
y: 2017
m: 6
d: 21
h: 8
mi: 21
s: 41.774000000001251
Any ideas? Thanks in advance.
Cheers
  1 件のコメント
DCagigas
DCagigas 2017 年 8 月 4 日
編集済み: DCagigas 2017 年 8 月 4 日
My bad...
If I check my "C:Windows" folder it was indeed modified on the 21.Jun.17, at 10:21:41. Which means 2 hours have to be added to the script:
[today.y,today.m,today.d,today.h,today.mi,today.s] = datevec(datenum([1970 1 1 2 0 Millisecs/1000]));
today =
y: 2017
m: 6
d: 21
h: 10
mi: 21
s: 41.774000000004889

サインインしてコメントする。

回答 (1 件)

Sindhu Yerragunta
Sindhu Yerragunta 2017 年 8 月 7 日
編集済み: Sindhu Yerragunta 2017 年 8 月 7 日
Hi Dicagigas,
Please refer the following Java documentation
The returned milliseconds from lastModified are measured since the epoch (00:00:00 GMT, January 1, 1970). So you need to add GMT difference of your country to the hour argument of 'datenum' function.
For example, for India the time is GMT+5.30, so you need to add an offset of 5 hours 30 minutes.
[today.y,today.m,today.d,today.h,today.mi,today.s] = datevec(datenum([1970 1 1 5 30 secs]));
Hope this answers your query.
-Sindhu
  1 件のコメント
DCagigas
DCagigas 2017 年 8 月 7 日
Thanks Sindhu for the quick reply.
May I ask you one further question? Are there any files/folders that will keep the "true" data/time even if the user modifies Windows date/time?
Cheers

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by