1

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.

Patel Harsh
  • 231
  • 1
  • 2
  • 7
  • Do you have access to the source code? If yes, you could simply read the metadata of the file. You could also modify the startup banner https://www.techiedelight.com/how-to-replace-the-spring-banner-in-spring-boot-application/ or look at this SO post https://stackoverflow.com/questions/3336392/java-print-time-of-last-compilation – Davide Oct 10 '22 at 22:42
  • This is not readily available. Ask the JVM where it found the byte code for the current class, and identify the jar file from that. With that File just get the information you want. – Thorbjørn Ravn Andersen Oct 11 '22 at 05:50
  • What's the OS please? – g00se Oct 11 '22 at 12:35

1 Answers1

0

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.

Du-Lacoste
  • 11,530
  • 2
  • 71
  • 51
  • Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. We have so much better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. – Ole V.V. Dec 10 '22 at 20:12