-2

Possible Duplicate:
Determine file creation date in Java

I have a file named abc.txt which is created by my application in c:. Now I have to write logic in such way that if file is created within 1 hour timeframe then I have to print:

file is created within 1 hour itself

and if file is created more than 1 hour before then to print the statement:

It has been more then 1 hour the file is created

How can I achieve this?

Community
  • 1
  • 1
user1726942
  • 407
  • 2
  • 4
  • 10

2 Answers2

1

java.io.File.lastModified() should suit your needs.

sp00m
  • 47,968
  • 31
  • 142
  • 252
  • @spOOm please post the code so that I can grasp – user1726942 Oct 23 '12 at 08:50
  • @user1726942 I think you could try by yourself. `lastModified()` returns *the time that the file denoted by this abstract pathname was last modified*. From that, writing an algorithm that checks whether the file has been modified during the last hour or not is not so hard. – sp00m Oct 23 '12 at 08:55
  • @SpOOm thanks a lot for guiding, Bro I am stuck up thats why I need your advise ,Thanks a lot if possible then request you to please post the code..!! – user1726942 Oct 23 '12 at 08:59
1

Below is the code

File f = new File("path of file");
Date now = new Date();
Date filedate = new Date(f.lastModified());
if((now.getTime()-filedate.getTime())>60000){
    System.out.println("It has been more then 1 hour the file is created");
}else{
    System.out.println("file is created within 1 hour itself");
}
DDK
  • 1,032
  • 18
  • 39