Renaming Photos by Timestamp
Including my cellphone, I used to have three cameras (used because my waterproof one died on me right after my recent trip to Palawan). Most smartphones already save photos with timestamps as the filenames but as for digital cameras, as far as I know, it’s not really the case. In general, the file naming convention varies from one device to another. The lines in Java that follow are not really anything special; it just reads the Last Modified metadata and uses it as the filename. I’ve been using this for quite some time now just because I want to be able to view photos chronologically once I consolidate them from multiple devices. That simple. Okay, and a bit of an OCD, too.
String dirName = "C:/Photos/Maldives"; //in my dreams String fileNameExt = ".jpg"; //JPEG format String fileNamePref = "RIMG"; //just blank to rename all photos String fileNameFmt = "yyyyMMdd_HHmmss"; //e.g. 20161018_161534 for October 18, 2016 4:15:34 PM (should be unique enough!) File directory = new File(dirName); File[] photos = directory.listFiles(); for(File photo : photos) { String filename = photo.getName(); if(filename.toLowerCase().endsWith(fileNameExt) && filename.startsWith(fileNamePref)) { SimpleDateFormat sdf = new SimpleDateFormat(fileNameFmt); String newFilename = sdf.format(photo.lastModified()) + fileNameExt; File renamedPhoto = new File(photo.getParent() + "/" + newFilename); if(!photo.renameTo(renamedPhoto)) { System.out.println("Failed to rename: " + photo.getAbsolutePath()); } } else { System.out.println("Not a " + fileNamePref + "*" + fileNameExt + ": " + photo.getAbsolutePath()); } }
This is under the assumption that the date and time in your devices have been set correctly. Different time zones are no longer within the scope of this post but is accommodated in—apparently someone already thought of the same—Renaming jpeg files with date+time stamp. Ideally, all photos should be renamed successfully since no two photos can be taken at exactly the same time (burst shots are another story). But with multiple cameras, there can simply be conflict sometimes. Lastly, you should be careful when copying or moving photos because sometimes the Last Modified data are changed to the date you copied or moved them. Should that be the case, the given source code no longer holds any value.