答案 0 :(得分:13)
很久以前这个问题已得到解答。但是如果有人在这里摆动一个适合我的解决方案,类似于Supah Fly所建议但支持jar和文件。
private long classBuildTimeMillis() throws URISyntaxException, IllegalStateException, IllegalArgumentException {
URL resource = getClass().getResource(getClass().getSimpleName() + ".class");
if (resource == null) {
throw new IllegalStateException("Failed to find class file for class: " +
getClass().getName());
}
if (resource.getProtocol().equals("file")) {
return new File(resource.toURI()).lastModified();
} else if (resource.getProtocol().equals("jar")) {
String path = resource.getPath();
return new File(path.substring(5, path.indexOf("!"))).lastModified();
} else {
throw new IllegalArgumentException("Unhandled url protocol: " +
resource.getProtocol() + " for class: " +
getClass().getName() + " resource: " + resource.toString());
}
}
但这不会处理zip文件或静态上下文,如果事情向南发生,它会抛出异常而不是返回null。这有点友善:
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;
}
答案 1 :(得分:12)
答案 2 :(得分:12)
由于这一点从未被提及过,任何想要通过任何必要手段解决这个问题的人都可能会发现这是一个合适而又笨拙的解决方案:
new Date(new File(getClass().getClassLoader().getResource(getClass().getCanonicalName().replace('.', '/') + ".class").toURI()).lastModified()))
它可能不太漂亮,而且很可能在其他平台上不兼容,但这是我发现在本机Java中找出当前类的编译日期的唯一方法。
答案 3 :(得分:5)
答案 4 :(得分:4)
答案 5 :(得分:1)
答案 6 :(得分:1)
答案 7 :(得分:1)
使用@CompileTime并创建一个字段
<input @input="updateValue($event.target.value)">
答案 8 :(得分:0)
这是我的类,用于检测Java程序的构建时间。它也使用this回答中的代码。
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.attribute.FileTime;
import java.text.DateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.Locale;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class BuildDate
{
private static Date buildDate;
static
{
try
{
buildDate = setBuildDate();
} catch (Exception exception)
{
exception.printStackTrace();
}
}
public static String getBuildDate()
{
int style = DateFormat.FULL;
Locale locale = Locale.getDefault();
DateFormat dateFormat = DateFormat.getDateInstance(style, locale);
DateFormat timeFormat = DateFormat.getTimeInstance(style, locale);
return dateFormat.format(buildDate) + " " + timeFormat.format(buildDate);
}
private static Date setBuildDate() throws Exception
{
if (ProgramDirectoryUtilities.runningFromIntelliJ())
{
return getClassBuildTime();
} else
{
return getNewestFileDate();
}
}
private static Date getNewestFileDate() throws Exception
{
String filePath = ProgramDirectoryUtilities.getJARFilePath();
File file = new File(filePath);
ZipFile zipFile = new ZipFile(file);
Enumeration entries = zipFile.entries();
long millis = -1;
while (entries.hasMoreElements())
{
ZipEntry entry = (ZipEntry) entries.nextElement();
if (!entry.isDirectory())
{
FileTime fileTime = entry.getLastModifiedTime();
long currentMillis = fileTime.toMillis();
if (millis < currentMillis)
{
millis = currentMillis;
}
}
}
return new Date(millis);
}
/**
* 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() throws IOException, URISyntaxException
{
Date date = null;
Class<?> currentClass = new Object()
{
}.getClass().getEnclosingClass();
URL resource = currentClass.getResource(currentClass.getSimpleName() + ".class");
if (resource != null)
{
switch (resource.getProtocol())
{
case "file":
date = new Date(new File(resource.toURI()).lastModified());
break;
case "jar":
{
String path = resource.getPath();
date = new Date(new File(path.substring(5, path.indexOf("!"))).lastModified());
break;
}
case "zip":
{
String path = resource.getPath();
File jarFileOnDisk = new File(path.substring(0, path.indexOf("!")));
try (JarFile jarFile = new JarFile(jarFileOnDisk))
{
ZipEntry zipEntry = jarFile.getEntry(path.substring(path.indexOf("!") + 2));//Skip the ! and the /
long zeTimeLong = zipEntry.getTime();
date = new Date(zeTimeLong);
}
break;
}
}
}
return date;
}
}
实用程序类:
import java.io.File;
import java.lang.invoke.MethodHandles;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class ProgramDirectoryUtilities
{
public static String getJARFilePath() throws URISyntaxException
{
return new File(MethodHandles.lookup().lookupClass().getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getAbsolutePath();
}
public static boolean runningFromJAR()
{
try
{
String jarFilePath = new File(MethodHandles.lookup().lookupClass().getProtectionDomain()
.getCodeSource()
.getLocation()
.getPath()).
toString();
jarFilePath = URLDecoder.decode(jarFilePath, "UTF-8");
try (ZipFile zipFile = new ZipFile(jarFilePath))
{
ZipEntry zipEntry = zipFile.getEntry("META-INF/MANIFEST.MF");
return zipEntry != null;
}
} catch (Exception exception)
{
return false;
}
}
public static String getProgramDirectory()
{
if (runningFromJAR())
{
return getCurrentJARDirectory();
} else
{
return getCurrentProjectDirectory();
}
}
private static String getCurrentProjectDirectory()
{
return new File("").getAbsolutePath();
}
private static String getCurrentJARDirectory()
{
try
{
return new File(MethodHandles.lookup().lookupClass().getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getParent();
} catch (URISyntaxException exception)
{
exception.printStackTrace();
}
return null;
}
public static boolean runningFromIntelliJ()
{
String classPath = System.getProperty("java.class.path");
return classPath.contains("idea_rt.jar");
}
}
答案 9 :(得分:0)
我认为这是最好的解决方案。 我正在使用eclipse的导出功能“ Runnable jar-file”。此函数生成文件“ META-INF / MANIFEST.MF”,我正在使用该文件来确定导出时间。 那段时间告诉我我何时构建程序。 在eclipse下,将仅显示参数“ obj”的类的编译时。
ID object
FROM datetime64[ns, UTC]
TO datetime64[ns, UTC]
time_delta int64
dtype: object