# Solved: Java: lastModified not working?



## Gibbs (Feb 28, 2005)

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:

```
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:

```
WordCount.class was created at the same time as  Comparing.class
```
Any help appreciated. Thanks


----------



## Gibbs (Feb 28, 2005)

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


----------

