1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Java: lastModified not working?

Discussion in 'Software Development' started by Gibbs, Feb 16, 2007.

Thread Status:
Not open for further replies.
  1. Gibbs

    Gibbs Thread Starter

    Joined:
    Feb 28, 2005
    Messages:
    609
    I'm learning Java and am currently on a simple tutorial explaining how to compare the modified dates of files.

    However it is outputting they are the same, when they aren't. Theres a two day difference between the modified date of WordCount.class and Comparing.class.

    Heres my code:
    Code:
    import java.io.*;
    
    public class ComparingFileDates
    {
    	static String compare;
    	static long modify1;
    	static long modify2;
    	static void comparing()
    	{
    		if (modify1 == modify2)
    		compare = " was created at the same time as ";
    		else if(modify1 < modify2)
    		compare = " file is older than";
    		else
    		compare = " file is newer than";
    	}
    
    	public static void main(String[] args)
    	{
    
    		//String test1 = "mytest.java";
    		//String test2 = "Comparing.java";
    			
    		File file1 = new File("WordCount.class");
    		File file2 = new File("Comparing.class");
    
    			modify1 = file1.lastModified();
    			modify2 = file2.lastModified();
    
    			comparing();
    
    		System.out.println(file1 + compare + ' ' + file2);
    	}
    }
    Output:
    Code:
    WordCount.class was created at the same time as  Comparing.class
    Any help appreciated. Thanks
     
  2. Gibbs

    Gibbs Thread Starter

    Joined:
    Feb 28, 2005
    Messages:
    609
    Ah. Had the files in the wrong directory. That'll teach me to use a file check in future. It works so problem solved I guess :rolleyes:
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/544716

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice