簡體   English   中英

將可執行文件 jar 的最后修改日期和時間顯示到正在運行的應用程序的控制台

[英]Displaying the executable jar files last modified date and time to the console of the running application

如何在運行 spring 啟動應用程序的應用程序控制台中顯示我們通過 mvn install 創建的可執行文件 jars 的最后修改日期和時間?

正如我們所知,當應用程序開始運行時,端口號會顯示在控制台中,就像我希望看到上次構建的 jar 文件的日期和時間一樣,該文件具有上次構建的數據和時間。

我沒有任何代碼,因為這可能是一個普遍的問題。

由於細節不是很清楚,我將使用以下代碼。 我假設您將使用LinuxWindows OS ,並根據存儲的maven輸出在Directory目標中獲取一些文件。

您需要將目錄中的文件獲取到一個Array並使用以下代碼進行迭代。 這將為您提供在LONG中迭代的每個文件的最后修改日期,您將需要SimpleDateFormat以在日期和時間中顯示。

// get the directory and iterate file one by one then the code:
File file = new File("\home\noname\xyz.txt"); // assume read file is xyz.txt
String fileName = file.getAbsoluteFile().getName(); 
long fileLastModifiedDate = file.lastModified(); 
// returns last modified date in long format 
System.out.println(fileLastModifiedDate); 
// e.g. 1644199079746
Date date = new Date(fileLastModifiedDate); 
// create date object which accept long
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd MMM yyyy hh:mm:ss"); 
// this is the format I assume, but can be changed as preferred : ‎12 ‎December ‎2022, ‏‎11:59:22
String myDate = simpleDateFormat.format(date); 
// accepts date and returns String value
System.out.println("Last Modified Date" + myDate); 
// displays: 12 ‎December ‎2022, ‏‎11:59:22

如果您不喜歡我使用的那個,您可以查看下面的 URL 以提出您自己的 SimpleDateFormat。

https://javadevtools.com/simpledateformat

謝謝。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM