Results 1 to 5 of 5

Thread: How to get a file last modification date in java?

  1. #1
    Join Date
    Nov 2009
    Posts
    45

    How to get a file last modification date in java?

    Hello to all,
    I am new to this forum. I am working on live project where I use java as front end and SQL as back end. In my project I have to show last modification date of file. I tried various method. But none of them worked out. Can anyone tell me how to get a file last modification date in java? Please help me.
    Thank you.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How to get a file last modification date in java?

    Hey it is very easy process to get a file last modification date in java. You have to Create an instance of file object to do this. I have wriiten follwing program for you. In that program I have use one variable named "lastModifiedfile" to store last modified date of that file. It is very easy process.


    Code:
    import java.io.File;
    import java.util.Date;
    
    public class FLModificationDate
    {
        public static void main(String[] args)
        {
           
            File file1 = new File("samplefile.java");
            
            Long lastModifiedfile = file.lastModified();
    
           
            Date dates = new Date(lastModifiedfile);
    
            
            System.out.println(dates);
        }
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: How to get a file last modification date in java?

    I have written following program to get a file last modification date in java. It is very simple program. In following program I have use one class named filemodEG and using this class I have created one object to get modified date of particular file. You have to just pass this file to above object to get modified date.


    Code:
    import java.io.*;
     
    import java.util.*;
     
    public class filemodEG{
     
      public static void main(String[] args) throws IOException{
        BufferedReader ins = new BufferedReader(new 
                              InputStreamReader(System.in));
        System.out.println("Enter file names  : ");
        File filenames = new File(ins.readLine());
     
        if (filenames.isDirectory()){
     
          if (filenames.exists()){
     
           long ts = filenames.lastModified();
     
           System.out.println("Directory names : " + filenames.getName());
            System.out.println("Dfile 
                    modification date and time : " + new Date(ts)); 
         }
       
       else{
        
        System.out.println("file not found!");
     
            System.exit(0);
        
      }
       
     }
      
      else{
     
          if (filename.exists()){
     
           long t = filename.lastModified();
      
          System.out.println("File names : " + filenames.getName());
            System.out.println("File 
                        modification date and time is as follows  : " + new Date(ts));
     
         }
      else{
        
        System.out.println("such type of File not found!");
     
           System.exit(0);
       
       }
       
     }
      
    }
     
    }

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: How to get a file last modification date in java?

    It is very simple to get a file last modification date in java. You have to just use lastModified() method from a file object to do this. Before running this program make sure that you have set your system date correct. Just use following code in your program to do this. It is very simple program.

    Code:
    String str1 = "fileeg1.dat";
    String str2 = "fileeg2.dat";
    
    File feg1 = new File(str1);
    File feg2 = new File(str2);
    
    if (feg1.lastModified() < feg2.lastModified())
    System.out.println((str1 + " IS OLDER THAN " + Str2);

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: How to get a file last modification date in java?

    I have written following program for you. Just try to understand it. It is very simple program. In following program I have use class named as "lastModifieddateEg". I have use two string named as sis1 and sis2 to get two file and then I have passed this values to to file named as feg1 and feg2.


    Code:
    Code:
    
    import java.io.File;
    
    public class lastModifieddateEg {
    
        public static void main(String[] args) {
    
            String sis1 = "c:\\sample1.txt";
            String sis2 = "c:\\sample2.txt";
    
            File feg1 = new File(sis1);
            File feg2 = new File(sis2);
    
            if (feg1.lastModified() < feg2.lastModified()) {
                System.out.println(sis1 + " is older than " + sis2);
                System.out.println(feg1.lastModified());
                System.out.println(feg2.lastModified());
            } else {
                System.out.println(s2 + " is older than " + s1);
            }
        }

Similar Threads

  1. Exactly match date in java
    By Vodka in forum Software Development
    Replies: 5
    Last Post: 13-02-2010, 02:00 AM
  2. Find date in java
    By Allan.d in forum Software Development
    Replies: 3
    Last Post: 09-12-2009, 10:43 AM
  3. How to do the Date comparison in Java
    By Rock Villa in forum Windows Software
    Replies: 3
    Last Post: 06-08-2009, 09:44 AM
  4. Prevent modification of values on a protected Excel file
    By DotNetUser in forum Software Development
    Replies: 5
    Last Post: 02-05-2009, 01:12 PM
  5. Replies: 5
    Last Post: 27-01-2009, 07:45 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,536,926.55306 seconds with 17 queries