I am trying to get information on when was the last date a file was modified in java. I have the date in long format. I dont know why the long object is showing the year to be 1970.Below is my code


File mypath=new File(my_global_folder+"/"+mydata.getItemAtPosition(position));
 long time=mypath.lastModified();
 //When i try to convert long to Date the year part reads 1970
 Date mydate= new Date(time);


Can anyone help me modify the code to give the right lat modified date

I can't see anything wrong with that code. I pasted it into a little stand-alone test and it works perfectly...

JFileChooser fileChooser = new JFileChooser();
int result = fileChooser.showOpenDialog(null);
File f = fileChooser.getSelectedFile();
long time = f.lastModified();
System.out.println(new Date(time));
commented: Am debugging on android, when i toast the date in string format, it shows 1970 as the year +0

Sorry - I don't have an Adroid SDK to test on.
The Date class was superceeded by the NIO Path/Files classes years ago. Maybe try the NIO equivalent? ...

System.out.println(Files.getLastModifiedTime(f.toPath()));
commented: Thankslet me try that code +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.