In Eclipse how to automatically print current date/time in the comments?

34,562

Solution 1

You didn't specify which version of Eclipse you're using but, unless you are on a very old version, this should work:

  1. Go to Windows/Preferences.
  2. Select Java/Code Style/Code Templates from the preferences tree.
  3. In the code templates window, select the type of comments where you want timestamps to appear, e.g. getters, and click the Edit button. In the Edit Template dialog, positition the cursor wherever you like in the model comment, then click "Insert Variable...". There is no timestamp variable (i.e. a single variable that shows year, month, day, hour, minute, second, and microseconds) but you could do a date and then a time, e.g. ${date}${time}, to get something accurate to the second. That should be good enough for most people....
  4. I think you need to do the same steps for each of the different places where you want the timestamp to appear; I don't think there is any way to tell Eclipse to put a timestamp in every kind of comment in a single operation

Solution 2

Write a template for a keyword, for example date, that uses Eclipse date and time variables. After doing this, you will be able to expand the keyword into a date with Ctrl-Space.

For details, have a look at http://www.ibm.com/developerworks/opensource/library/os-eclipse-galcode/index.html

However, what you probably want instead is putting your code into some sort of versioning system (Subversion, git, Hg, ...) and use their capabilities to keep track on your versions and when you checked them in.

///BR, Jens Carlberg

Solution 3

The date variable in comment templates supports a format.

From the context help:

${id:date[(format[, locale])]} Evaluates to the current date in the specified format and locale. 'format' and 'locale' are optional parameters. 'format' is a pattern compatible with java.text.SimpleDateFormat. 'locale' is an RFC 3066 locale ID.

Examples:

${date}

${currentDate:date('yyyy-MM-dd')}

${d:date('EEEE dd MM yyyy', 'fr_CH')}

So setting a template to:

/**
 * modified by @author ${user} last on ${d:date('yyyy-MM-dd HH:mm:ss.SSS')}
 */

will result in a comment like:

/**
 * modified by @author Chantz last on 2017-08-04 09:54:23.130
 */

Solution 4

All the previous posts are correct:

  • In Eclipse/STS, Go to Windows-->Preferences and then
  • Go to Editor->Templates-> Click on New--> (put in a name and desciption) --> in the Pattenr Section add the ${date}${time}

enter image description here

Solution 5

Preferences --> Java --> Code Style --> Code Templates

enter image description here

Then press Shift + Alt + J will help you add date and time in existing file.

Share:
34,562
Chantz
Author by

Chantz

Updated on August 17, 2020

Comments

  • Chantz
    Chantz over 3 years

    I have element-level comments in my code & I need to say when was the last time I modified a piece of code. Since it might be difficult to do this automatically when I save the document in question, I was looking for some semi-automatic solution where I press a shortcut & poof the date/time appears at my cursor.

    E.g.

    /**
     * modified by @author Chantz last on <ENTER CURRENT DATE TIME HERE>
     */
    public class EclipsePrintDateTimePlease {
    ...
    

    UPDATE Eclipse versions I use are Helios & Galileo (I have different workstations).

  • Chantz
    Chantz about 13 years
    I am using either Eclipse Helios or Galileo (I have different workstations that I use frequently.)
  • Andreas Mayer
    Andreas Mayer about 11 years
    +1 for suggesting the versioning system. Such information should not be included in the source code itself and much less maintained manually. That's what versioning systems are for.
  • Jook
    Jook over 10 years
    @AndreasMayer although you are right, I find myself very often in the situation that I would like a code-included version tracking very much. It's just an unneccessary task to put in author, date and current or previous commit manually into your code, but it is very helpful in order to distinguish releases even after a longer time of absence - and when it's not automated you WILL end up with insecurities and building it time and time again.
  • rpax
    rpax over 9 years
    Great! Just what I was looking for!
  • grepit
    grepit over 9 years
    @rpax happy I could help .