14

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
  • 5,883
  • 10
  • 56
  • 79

4 Answers4

7

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

JenEriC
  • 758
  • 7
  • 10
  • 1
    +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. – Andreas Mayer Feb 14 '13 at 14:53
  • @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. – Jook Sep 02 '13 at 11:19
5

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
 */
A4L
  • 17,353
  • 6
  • 49
  • 70
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

grepit
  • 21,260
  • 6
  • 105
  • 81
4

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.

rinuthomaz
  • 1,393
  • 2
  • 23
  • 38