Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Compilation date, like C __DATE__

Tags:

java

I need to automate getting the compilation date in a specific format into one Java Source file, like the C compilers DATE define, How?

like image 833
Ayman Avatar asked Mar 28 '26 09:03

Ayman


2 Answers

The standard Java compiler has no way of doing this (I don't think C compilers do either - I'm guessing that it is the pre-processor that gives DATE, FILE etc)

You'll have to write a source file with the date as a string into it - look at ant's copy task with a filter

<copy file="pre-src/Version.java" toFile="src/Version.java">
  <filterset>
    <filter token="DATE" value="${TODAY}"/>
  </filterset>
</copy>

then in your source

public class Version {
    public static final String date = "@DATE@";
}

To get the date in various formats into the ant property TODAY look at the Tstamp task.

like image 79
Duncan McGregor Avatar answered Mar 30 '26 01:03

Duncan McGregor


You could do this using aspectj compile time weaving to initialise a variable (you would obviously want to do the assignment by converting the date in your aspect to a string and then have your code parse that string).

Of course there is also nothing stopping you actually using a c pre-processor on a java file.

like image 26
vickirk Avatar answered Mar 29 '26 23:03

vickirk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!