본문 바로가기

컴퓨터/IT

java compile된 시간 찍기 (compile timestamp)

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;


public class TestCompileTime {

    public TestCompileTime() {
        try {
            System.out.println("build date:>>"+TestCompileTime.getCompileTimeStamp(this.getClass()));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new TestCompileTime();
    }

    /**
     * get date a class was compiled by looking at the corresponding class file in the jar.
     * @author Zig
    */
    public static Date getCompileTimeStamp( Class<?> cls ) throws IOException
    {
       ClassLoader loader = cls.getClassLoader();
       String filename = cls.getName().replace('.', '/') + ".class";
       // get the corresponding class file as a Resource.
       URL resource=( loader!=null ) ?
                    loader.getResource( filename ) :
                    ClassLoader.getSystemResource( filename );
       URLConnection connection = resource.openConnection();
       // Note, we are using Connection.getLastModified not File.lastModifed.
       // This will then work both or members of jars or standalone class files.
       long time = connection.getLastModified();
       return( time != 0L ) ? new Date( time ) : null;
    }
}