Java .lastModified format

2 visualizzazioni (ultimi 30 giorni)
DCagigas
DCagigas il 4 Ago 2017
Commentato: DCagigas il 7 Ago 2017
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 Commento
DCagigas
DCagigas il 4 Ago 2017
Modificato: DCagigas il 4 Ago 2017
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

Accedi per commentare.

Risposte (1)

Sindhu Yerragunta
Sindhu Yerragunta il 7 Ago 2017
Modificato: Sindhu Yerragunta il 7 Ago 2017
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 Commento
DCagigas
DCagigas il 7 Ago 2017
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

Accedi per commentare.

Categorie

Scopri di più su Dates and Time in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by