简体   繁体   中英

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

How to display the last modified date and time of the Executable jars which we create by mvn install in the application console where the spring boot application is running?

As we know that the port number is displayed in the console while the application starts running just like that I want ot see the date and time of that jar file which was last build and has the data and time for that last build.

I don't have any code as this can be a general question to ask.

Since the details are not crystal clear I will use the following code. I assume you will use Linux or Windows OS and got some files in a Directory target as per maven outputs stored.

You will need to get the files in the Directory to an Array and iterate with the below code. This will get you the last modified date in each of the files iterated in LONG and you will need SimpleDateFormat to display in Date and Time.

// 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

You may have a look below URL to come up with your own SimpleDateFormat if you do not prefer the one I used.

https://javadevtools.com/simpledateformat

Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM