Effacer les filtres
Effacer les filtres

Java .lastModified format

2 vues (au cours des 30 derniers jours)
DCagigas
DCagigas le 4 Août 2017
Commenté : DCagigas le 7 Août 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 commentaire
DCagigas
DCagigas le 4 Août 2017
Modifié(e) : DCagigas le 4 Août 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

Connectez-vous pour commenter.

Réponses (1)

Sindhu Yerragunta
Sindhu Yerragunta le 7 Août 2017
Modifié(e) : Sindhu Yerragunta le 7 Août 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 commentaire
DCagigas
DCagigas le 7 Août 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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Interactive Control and Callbacks dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by