在 Eclipse 中如何在注释中自动打印当前日期/时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5318805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 20:03:00  来源:igfitidea点击:

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

eclipsecommentsdatetime

提问by Chantz

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

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

我使用的更新Eclipse 版本是 Helios 和 Galileo(我有不同的工作站)。

回答by dLobatog

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

您没有指定您使用的是哪个版本的 Eclipse 但是,除非您使用的是非常旧的版本,否则这应该可以工作:

  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 thinkyou 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 everykind of comment in a single operation
  1. 转到 Windows/首选项。
  2. 从首选项树中选择 Java/代码样式/代码模板。
  3. 在代码模板窗口中,选择您希望时间戳出现的注释类型,例如 getter,然后单击“编辑”按钮。在“编辑模板”对话框中,将光标定位在模型注释中您喜欢的任何位置,然后单击“插入变量...”。没有时间戳变量(即显示年、月、日、小时、分钟、秒和微秒的单个变量),但您可以先输入日期,然后再输入时间,例如 ${date}${time},以获得精确到第二的东西。这对大多数人来说应该足够了......
  4. 认为您需要对希望时间戳出现的每个不同位置执行相同的步骤;我不认为有任何方法可以告诉 Eclipse在单个操作中的每种评论中放置时间戳

回答by JenEriC

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.

为使用 Eclipse 日期和时间变量的关键字(例如日期)编写模板。执行此操作后,您将能够使用 Ctrl-Space 将关键字扩展为日期。

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

有关详细信息,请查看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.

但是,您可能想要的是将您的代码放入某种版本控制系统(Subversion、git、Hg 等)中,并使用它们的功能来跟踪您的版本以及您何时签入它们。

///BR, Jens Carlberg

///BR, Jens Carlberg

回答by grepit

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}
  • 在 Eclipse/STS 中,转到 Windows--> Preferences 然后
  • 转到编辑器->模板->点击新建-->(输入名称和描述)-->在Pattenr部分添加${date}${time}

enter image description here

在此处输入图片说明

回答by rinuthomaz

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

首选项 --> Java --> 代码样式 --> 代码模板

enter image description here

在此处输入图片说明

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

然后按Shift+ Alt+J将帮助您在现有文件中添加日期和时间。

回答by A4L

The datevariable in comment templates supports a format.

date评论模板中的变量支持一种格式。

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')}

${id:date[(format[, locale])]} 以指定的格式和语言环境计算当前日期。'format' 和 'locale' 是可选参数。'format' 是一种与 java.text.SimpleDateFormat 兼容的模式。'locale' 是一个 RFC 3066 区域设置 ID。

例子:

${日期}

${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
 */