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
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 CenterFile Exchange 中查找有关 Dates and Time 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by