This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes delete files from a directory if datetime stamp matches Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "delete files from a directory if datetime stamp matches" Watch "delete files from a directory if datetime stamp matches" New topic
Author

delete files from a directory if datetime stamp matches

Mei Jones
Greenhorn

Joined: Oct 19, 2012
Posts: 8
I have the below code and eventhough the file name and datetime stamp matches it does not delete the file.

Can anyone shed some light ?

Here's the results I got


deleteme.docx File modification date and time : 23-07-2012 07:55
23-07-2012 07:55File not deleted!


I know there should be a issue with my comparison here --> if ((sdf.format(d)).toString() == s)

it gets compiled with no data type errors

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Mei Jones wrote:
I know there should be a issue with my comparison here --> if ((sdf.format(d)).toString() == s)


When comparing objects' states ("contents"), use equals(), not ==. This includes comparing Strings for equality. The == operator tests if two references have the same value (both point to the same object or both are null). The equals() method, if properly written, tells if two objects have equal "contents" by whatever equality semantics are defined for the class in question.

Also, you don't need the toString() there, since format() returns a String.

Also, if you're comparing dates, you should compare them as Dates, not as Strings.
Mei Jones
Greenhorn

Joined: Oct 19, 2012
Posts: 8
Thank you,.. Yes it worked..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: delete files from a directory if datetime stamp matches
 
Similar Threads
resolving java.lang.NumberFormatException
date format
Date Conversion
How do i print 1 as 01?
How to get the timestamp from the File IO