tags:

views:

1778

answers:

5

I would like to extract the date a jpg file was created. Java has the lastModified method for the File object, but appears to provide no support for extracting the created date from the file. I believe the information is stored within the file as the date I see when I hover the mouse pointer over the file in Win XP is different than what I can get by using JNI with "dir /TC" on the file in DOS.

+3  A: 

The date is stored in the EXIF data in the jpeg. There's a java library and a viewer in java that might be helpful.

pjz
A: 

You probably need something to access the exif data. Google suggests this library.

Dan Dyer
+2  A: 

I use this metadata library: http://www.drewnoakes.com/code/exif/

Seems to work pretty well, although bear in mind that not all JPEG images have this information, so it can't be 100% fool-proof.

If the EXIF metadata doesn't contain the created date, then you'll probably have to make do with Java's lastUpdated - unless you want to resort to Runtime.exec(...) and using system functions to find out (I wouldn't recommend this, though!)

Phill Sacre
+4  A: 

The information is stored within the image in a format called EXIF or link text. There several libraries out there capable of reading this format, like this one

amo-ej1
Great! Thanks for all of the helpful comments! I believe I will use the library at drewnoakes.com.
+1  A: 

The code example below asks the user for a file path and then outputs the creation date and time:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static void main(final String[] args) {
        try {
            // get runtime environment and execute child process
            Runtime systemShell = Runtime.getRuntime();
            BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Enter filename: ");
            String fname=(String)br1.readLine();
            Process output = systemShell.exec("cmd /c dir /a "+fname);
             // open reader to get output from process
            BufferedReader br = new BufferedReader (new InputStreamReader(output.getInputStream()));

            String out="";
            String line = null;

            int step=1;
             while((line = br.readLine()) != null ) 
              {
                 if(step==6)
                {
                out=line;
                }
                 step++;
                 }          // display process output

            try{
            out=out.replaceAll(" ","");
            System.out.println("CreationDate: "+out.substring(0,10));
            System.out.println("CreationTime: "+out.substring(10,15));
            }
            catch(StringIndexOutOfBoundsException se)
            {
                System.out.println("File not found");
            }
            }
          catch (IOException ioe){ System.err.println(ioe); }
          catch (Throwable t) { t.printStackTrace();}
    }
}
smink
Thanks for the code snippet, but I actually need to get the date from the picture metadata, not from the file system.