In Eclipse how to automatically print current date

2019-02-02 10:25发布

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).

5条回答
在下西门庆
2楼-- · 2019-02-02 11:02

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
 */
查看更多
甜甜的少女心
3楼-- · 2019-02-02 11:05

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.

查看更多
手持菜刀,她持情操
4楼-- · 2019-02-02 11:19

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
查看更多
Deceive 欺骗
5楼-- · 2019-02-02 11:24

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

查看更多
Root(大扎)
6楼-- · 2019-02-02 11:26

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

查看更多
登录 后发表回答