How can I set the format for the ${date} variable which can be used in Eclipse templates?
4 Answers
Update February 2016: bug 75981 is officially fixed!
See Jmini's answer below
Update July 2015, 6 years later:
The bug mentioned below seems fixed in Eclipse 4.x.
Eric Wang comments below:
@date ${id:date('YYYY-MMM-dd')} ${time}
this give me English datetime format in eclipse 4.
Original Answer 2009 Eclipse 3.x
Argh! There is a long standing bug just for that: bug 75981
The
${date}
variable could be enhanced to accept an argument (similar to other parameterizations added in 3.3M1), e.g.${d:date(format)}
, whereformat
is a pattern forSimpleDateFormat
.
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).
This thread mentions the 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()); } }
-
2`@date ${id:date('YYYY-MMM-dd')} ${time}` this give me English datetime format in eclipse 4.5. – Eric Jul 24 '15 at 05:58
-
@EricWang Thank you for the update. I have updated the answer accordingly. – VonC Jul 24 '15 at 06:03
-
The update doesn't work for me. I replace the class http://bit.ly/1NzZfJs in the jar using my custom format. – Paul Vargas Nov 28 '15 at 08:48
-
Using Eclipse 4.6. This works for me. I use the SimpleDateFormat to include time also: ${d:date('yyyy-MM-dd HH:mm:ss')} – Osmund Francis Aug 18 '16 at 16:40
I have fixed Bug 75981 with Eclipse Neon M5. You can download this Milestone Release here:
http://www.eclipse.org/downloads/index-developer.php
… or wait until June 2016 for the official Neon Release.
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')}
- The first parameter is the date format. Example:
More info about this feature on my blog: Bug 75981 is fixed!

- 9,189
- 2
- 55
- 77
-
1Well done! +1. I have referenced your answer in mine, for more visibility. – VonC Feb 05 '16 at 20:16
-
-
1Ah, it looks like I can just include the time formatting in the SimpleDateFormat so I won't even need to use ${time}. – Felix Bembrick Feb 17 '16 at 22:35
-
1@FelixBembrick Yeah, I also used the SimpleDateFormat for date-time: ${d:date('yyyy-MM-dd HH:mm:ss')} – Osmund Francis Aug 18 '16 at 16:37
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.
You can fix it for your Eclipse installation when you append following lines to your eclipse.ini:
-Duser.language=de
-Duser.region=DE

- 151
- 1
- 2
Additional information for those stumbling over this lately (like me):
For ISO 8601 date format, one can use the language settings fr-CA.

- 24,880
- 4
- 34
- 59
-
3append this line to your eclipse.ini:-Duser.language=fr-ca, then the date format will be "2014-08-07". – Soli Aug 07 '14 at 04:54
-
Small disadvantage: there are a few locations where you get french date format in the GUI, too. Example: 14-10-27 (comment's date) in compare local history. – Aconcagua Oct 27 '14 at 14:51