Programming Archive

Renaming Photos by Timestamp

Posted on October 18, 2016 at 16:15 Java No Comments

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.

Printing XYZ in Java using Asterisks

Posted on March 25, 2014 at 23:23 Java No Comments

Assumption is that dimension should always be an odd number (you have to be able to have immediately understood why, else please stop reading this this instant). It’s accomplished using brute force, because I just wanted to see it working and prove to myself that I haven’t gone totally rusty yet.

package letter;

public class LetterGenerator {
   public static void main(String[] args) {
      int dim = 7;

      for(int i = 0; i < dim; i++) {
         for(int j = 0; j < dim; j++) {
            if(i == j || i + j == dim - 1) {
               System.out.print("*");
            } else {
               System.out.print(" ");
            }
         }

         System.out.println();
      }

      System.out.println();

      for(int i = 0; i < dim; i++) {
         for(int j = 0; j < dim; j++) {
            if(((i == j || i + j == dim - 1) && i < dim / 2 + 1) || (i > dim / 2 && j == dim / 2)) {
               System.out.print("*");
            } else {
               System.out.print(" ");
            }
         }

         System.out.println();
      }

      System.out.println();

      for(int i = 0; i < dim; i++) {
         for(int j = 0; j < dim; j++) {
            if(i == 0 || i + j == dim - 1 || i == dim - 1) {
               System.out.print("*");
            } else {
               System.out.print(" ");
            }
         }

         System.out.println();
      }
   }
}
eXTReMe Tracker