eclipse 如何设置Eclipse日期变量格式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1131712/
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 18:50:28  来源:igfitidea点击:

How to set the Eclipse date variable format?

eclipsedatevariablescode-templates

提问by desolat

How can I set the format for the ${date} variable which can be used in Eclipse templates?

如何设置可在 Eclipse 模板中使用的 ${date} 变量的格式?

采纳答案by VonC

Update February 2016: bug 75981is officially fixed!
See Jmini's answer below

2016 年 2 月更新:错误 75981已正式修复!
下面Jmini回答

enter image description here

在此处输入图片说明



Update July 2015, 6 years later:

6 年后,2015 年 7 月更新:

The bug mentioned below seems fixed in Eclipse 4.x.
Eric Wangcomments below:

下面提到的错误似乎在 Eclipse 4.x 中已修复。
Eric Wang评论如下

@date ${id:date('YYYY-MMM-dd')} ${time} 

this give me English datetime format in eclipse 4.

这给了我 eclipse 4 中的英文日期时间格式。



Original Answer 2009 Eclipse 3.x

原始答案 2009 Eclipse 3.x

Argh! There is a long standing bug just for that: bug 75981

啊!有一个长期存在的错误:错误 75981

The ${date}variable could be enhanced to accept an argument (similar to other parameterizations added in 3.3M1), e.g. ${d:date(format)}, where formatis a pattern for SimpleDateFormat.

所述${date}变量可以被增强,以接受参数(类似于在3.3M1添加的其它的参数化),例如${d:date(format)},其中format为图案SimpleDateFormat

The only alternative would be to modify the class SimpleTemplateVariableResolver(as described in this thread), from the package org.eclipse.jface.text.templates. (You have here an example of such an extension).

唯一的选择是从包中修改类SimpleTemplateVariableResolver(如本线程中所述org.eclipse.jface.text.templates。(您在这里有一个此类扩展示例)。

This thread mentionsthe sources where you can find the class.

线程提到了您可以在其中找到该类的来源。

\eclipse\plugins\org.eclipse.platform.source_3.1.0\src\org.eclipse.text_3.1.0\src.zip

Example:

例子:

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()); } }

回答by jAgile

You could tell Eclipse to use a specific locale different from that of your operating system. Eclipse 3.5 (64 bit) doesn't use the MacOS X region setting. MacOS X english installation language with Germany as country provides a wrong date format.

您可以告诉 Eclipse 使用与您的操作系统不同的特定语言环境。Eclipse 3.5(64 位)不使用 MacOS X 区域设置。以德国为国家/地区的 MacOS X 英语安装语言提供了错误的日期格式。

You can fix it for your Eclipse installation when you append following lines to your eclipse.ini:

当您将以下几行附加到 eclipse.ini 时,您可以为 Eclipse 安装修复它:

-Duser.language=de
-Duser.region=DE

回答by Jmini

I have fixed Bug 75981with Eclipse Neon M5. You can download this Milestone Release here:

我已经用 Eclipse Neon M5修复了错误 75981。您可以在此处下载此里程碑版本:

http://www.eclipse.org/downloads/index-developer.php

http://www.eclipse.org/downloads/index-developer.php

… or wait until June 2016 for the official Neon Release.

……或者等到 2016 年 6 月正式发布 Neon。

Here a quick description of how it works:

这里简要说明它的工作原理:

  • As before you can use the date variable with no argument. Example: ${date}
  • You can use the variable with additional arguments. In this case you will need to name the variable (since you are not reusing the date somewhere else, the name of the variable doesn't matter). Example: ${mydate:date}
    • The first parameter is the date format. Example: ${d:date('yyyy-MM-dd')}
    • The second parameter is the locale. Example: ${maDate:date('EEEE dd MMMM yyyy HH:mm:ss Z', 'fr')}
  • 和以前一样,您可以使用不带参数的日期变量。例子:${date}
  • 您可以使用带有附加参数的变量。在这种情况下,您需要为变量命名(因为您没有在其他地方重用日期,所以变量的名称无关紧要)。例子:${mydate:date}
    • 第一个参数是日期格式。例子:${d:date('yyyy-MM-dd')}
    • 第二个参数是语言环境。例子:${maDate:date('EEEE dd MMMM yyyy HH:mm:ss Z', 'fr')}

Edit Template in Eclipse Preferences

在 Eclipse 首选项中编辑模板

More info about this feature on my blog: Bug 75981 is fixed!

有关此功能的更多信息,请访问我的博客:错误 75981 已修复!

回答by Aconcagua

Additional information for those stumbling over this lately (like me):

对于那些最近在这方面遇到困难的人(像我一样)的其他信息:

For ISO 8601 date format, one can use the language settings fr-CA.

对于 ISO 8601 日期格式,可以使用语言设置 fr-CA。