【发布时间】:2009-07-15 14:21:41
【问题描述】:
如何设置可在 Eclipse 模板中使用的 ${date} 变量的格式?
【问题讨论】:
标签: eclipse date variables code-templates
如何设置可在 Eclipse 模板中使用的 ${date} 变量的格式?
【问题讨论】:
标签: eclipse date variables code-templates
2016 年 2 月更新:bug 75981 已正式修复!
见Jmini的answer below
2015 年 7 月更新,6 年后:
下面提到的错误似乎已在 Eclipse 4.x 中修复。
Eric Wang comments below:
@date ${id:date('YYYY-MMM-dd')} ${time}
这给了我 Eclipse 4 中的英文日期时间格式。
原始答案 2009 Eclipse 3.x
啊!有一个长期存在的错误:bug 75981
${date}变量可以被增强以接受一个参数(类似于其他 3.3M1 中添加的参数化),例如${d:date(format)},其中format是SimpleDateFormat的模式。
唯一的选择是修改包org.eclipse.jface.text.templates中的类SimpleTemplateVariableResolver(如this thread中所述)。 (你这里有一个example of such an extension)。
这个thread mentions 是你可以找到课程的来源。
\eclipse\plugins\org.eclipse.platform.source_3.1.0\src\org.eclipse.text_3.1.0\src.zip
例子:
public static class Date extends SimpleTemplateVariableResolver {
/**
* Creates a new date variable
*/
public Date() {
super("date", TextTemplateMessages.getString("GlobalVariables.variable.description.date")); //$NON-NLS-1$ //$NON-NLS-2$ }
protected String resolve(TemplateContext context) {
//return DateFormat.getDateInstance().format(new java.util.Date());
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
return df.format(new java.util.Date()); } }
【讨论】:
@date ${id:date('YYYY-MMM-dd')} ${time} 这给了我 Eclipse 4.5 中的英文日期时间格式。
您可以告诉 Eclipse 使用与您的操作系统不同的特定语言环境。 Eclipse 3.5(64 位)不使用 MacOS X 区域设置。以德国为国家/地区的 MacOS X 英文安装语言提供了错误的日期格式。
当您在 eclipse.ini 中附加以下行时,您可以为您的 Eclipse 安装修复它:
-Duser.language=de
-Duser.region=DE
【讨论】:
我已经用 Eclipse Neon M5 修复了 Bug 75981。您可以在此处下载此里程碑版本:
http://www.eclipse.org/downloads/index-developer.php
... 或等到 2016 年 6 月正式发布霓虹灯。
这里简要介绍一下它的工作原理:
${date}
${mydate:date}
${d:date('yyyy-MM-dd')}
${maDate:date('EEEE dd MMMM yyyy HH:mm:ss Z', 'fr')}
有关此功能的更多信息,请访问我的博客:Bug 75981 is fixed!
【讨论】:
对于最近遇到这个问题的人(比如我)的补充信息:
对于 ISO 8601 日期格式,可以使用语言设置 fr-CA。
【讨论】: