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