17/05/2011 12:14 PM <DIR> .
17/05/2011 12:14 PM <DIR> ..
22/02/2011 02:40 PM 1,086,518 subDirectoryTestOld.txt
12/05/2011 01:43 PM 1,720,223 subDirectoryTestNew.txt
2 File(s) 2,806,741 bytes
I expect files TestOld.txt, subDirectoryTestOld.txt and folder SubTestDirectory to be picked up but nothing was found.
Line 25 is crucial to determining whether the timestamp of all files and folder are over 30 days but it is not meeting this condition and I am not clear on whether the reason behind subtraction between System.currentTimeMillis() - (daysBack * 24 * 60 * 60 * 1000) to get purgeTime (line 22) either.
I am running JDK1.6.0_25 and Netbeans 7.0 on Windows XP platform.
Is your number of days over 24? If so, you have an overflow problem.
If the number of days is 25, the value will be 25 * 24 * 60 * 60 * 1000. The mathematical value is 2160000000. However, this is larger than Integer.MAX_VALUE, and therefore the value overflows to -12516353. As a result, the purge time will be in the future, and will never be met. Values larger than 25 will only make the problem worse; it's even possible the overflow is so bad that the multiplication results in a positive value again leading to perhaps purge all files.
The fix is easy:
1) declare daysBack as a long
2) cast daysBack as a long: 3) Use explicit long literals inside the calculation: For all three solutions, the fact that the first and/or second operand is a long turns the entire result into a long, allowing a value of 2160000000 without overflowing.
This is a common mistake. It happens so many times it's even described in the Java Puzzlers book.
Thank you so much for having identified what appears to be a simple issue to a Java Guru can baffled a novice programmer quite many hours. Btw, can I confirm that both of the following suggestions 2 & 3 are identical:
I have used the latter one but would like to know whether they are equivalent.
Lastly, please let me know what is wrong with the questions that I have been posting so I won't make the same mistake in the future.