file.lastModified()
returns the last modified date. File does not seem to have any method to fetch lastAccessed date. Is there a way to programmatically fetch the last accessed date/time of any file in android ?
3 Answers
You can get the last access time using stat
or lstat
. The two methods, android.system.Os.stat(String path)
and android.system.Os.lstat(String path)
, were made public in Android 5.0. On previous Android versions you will need to use reflection or run a command in a shell.
Usage:
Android 5.0+
long lastAccessTime = Os.lstat(file.getAbsolutePath()).st_atime;
Using reflection before Android 5.0
Class<?> clazz = Class.forName("libcore.io.Libcore");
Field field = clazz.getDeclaredField("os");
if (!field.isAccessible()) {
field.setAccessible(true);
}
Object os = field.get(null);
Method method = os.getClass().getMethod("lstat", String.class);
Object lstat = method.invoke(os, file.getAbsolutePath());
field = lstat.getClass().getDeclaredField("st_atime");
if (!field.isAccessible()) {
field.setAccessible(true);
}
long lastAccessTime = field.getLong(lstat);
Note:
I don't think last access time is used on Android. From the java.nio
documentation:
If the file system implementation does not support a time stamp to indicate the time of last access then this method returns an implementation specific default value, typically the last-modified-time or a FileTime representing the epoch (1970-01-01T00:00:00Z).
I tested changing the last access time using the following command:
touch -a [PATH]
This did change the last access time when I ran the command as the root user. However, I don't think the last accessed time is updated/used on Android.

- 37,824
- 19
- 133
- 148
-
1"This did change the last access time when I ran the command as the root user. However, I don't think the last accessed time is updated/used on Android" - Do you mean to imply that lastAccessTime does not make any sense to Android as it does not support this time stamp ? – jay May 29 '16 at 08:17
-
Class.forName("libcore.io.Libcore") is resolving to libcore.io.BlockGuardOs. So, os.getClass().getDeclaredMethod("lstat", String.class); is throwing - NoSuchMethodException: lstat [class java.lang.String]. Is Class.forName("libcore.io.Libcore") resolving to expected value ? – jay May 29 '16 at 10:21
-
`Os` is an `interface` and `libcore.io.BlockGuardOs` is expected. After reading the old source code, it looks like `lstat` is implemented in `ForwardingOs`. Just change `getDeclaredMethod` to `getMethod`. I don't think lastAccessTime is used in Android, although supported. – Jared Rummler May 29 '16 at 20:40
-
1"I don't think lastAccessTime is used in Android, although supported" - were you implying that ANdroid does not return me the lastAccessedTime, but either created or last modified ? – jay May 31 '16 at 05:22
-
Right. At least on my device the lastAccessTime returns the last modified time. I can change it manually using `touch -a`. – Jared Rummler May 31 '16 at 05:55
-
Most android devices don't even support setLastModified. I must find some other way. Thank you Jared. – jay May 31 '16 at 06:35
lastModified ()
import java.io.File;
import java.util.Date;
public class FileExample {
public static void main(String[] args) {
File f = null;
String path;
long millisec;
boolean bool = false;
try{
f = new File("c:/demo.txt");
bool = f.exists();
if(bool)
{
millisec = f.lastModified();
// date and time
Date dt = new Date(millisec);
// path
path = f.getPath();
System.out.print(path+" last modified at: "+dt);
}
}catch(Exception e){
e.printStackTrace();
}
}
}

- 12,667
- 7
- 37
- 64

- 180
- 4
- 15
-
I'm looking for lastAccessedTime and not lastModified. Please refer to the question. – jay Jun 02 '16 at 10:58
Try this:
javaxt.io.File file = new javaxt.io.File("file-path");
file.getLastAccessTime();

- 420
- 10
- 19