Created
July 10, 2019 05:45
-
-
Save M-Razavi/3e2364732021350723f4f69b704e5480 to your computer and use it in GitHub Desktop.
Java print time of last compilation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static final Date buildDate = getClassBuildTime(); | |
/** | |
* Handles files, jar entries, and deployed jar entries in a zip file (EAR). | |
* @return The date if it can be determined, or null if not. | |
*/ | |
private static Date getClassBuildTime() { | |
Date d = null; | |
Class<?> currentClass = new Object() {}.getClass().getEnclosingClass(); | |
URL resource = currentClass.getResource(currentClass.getSimpleName() + ".class"); | |
if (resource != null) { | |
if (resource.getProtocol().equals("file")) { | |
try { | |
d = new Date(new File(resource.toURI()).lastModified()); | |
} catch (URISyntaxException ignored) { } | |
} else if (resource.getProtocol().equals("jar")) { | |
String path = resource.getPath(); | |
d = new Date( new File(path.substring(5, path.indexOf("!"))).lastModified() ); | |
} else if (resource.getProtocol().equals("zip")) { | |
String path = resource.getPath(); | |
File jarFileOnDisk = new File(path.substring(0, path.indexOf("!"))); | |
//long jfodLastModifiedLong = jarFileOnDisk.lastModified (); | |
//Date jfodLasModifiedDate = new Date(jfodLastModifiedLong); | |
try(JarFile jf = new JarFile (jarFileOnDisk)) { | |
ZipEntry ze = jf.getEntry (path.substring(path.indexOf("!") + 2));//Skip the ! and the / | |
long zeTimeLong = ze.getTime (); | |
Date zeTimeDate = new Date(zeTimeLong); | |
d = zeTimeDate; | |
} catch (IOException|RuntimeException ignored) { } | |
} | |
} | |
return d; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment