File template $DATE format

Hi,

How can I change the format used by the $DATE macro in file templates? I am getting dates like this:

28/02/2006

I want ISO dates, like this:

2006-02-28

I'm using IDEA 5.1 on Win XP. In my Windows Regional Settings, I have specified my preferred short date format as yyyy-MM-dd, but I don't know if this is relevant.

Thanks

John Hurst
Wellington, New Zealand

26 comments

It would also be nice to get the $DATE into live templates.

0

Hi all,

Should I assume since there's no answer, that there is no easy way to do this?

John Hurst
Wellington, New Zealand

0

I played around with this for a bit and basically determined that I have no idea how IntelliJ comes to the format that it uses. It doesn't get it from Windows region settings and there doesn't appear to be any place to specify it in IntelliJ configuration. Here's how it looks when I imbed the date:

Mar 8, 2006

That format is basically what I have for the long format in my regional settings minus the day of the week. However, even if I change it to something completely different, it has no impact on the file templates.

Since IntelliJ filetemplates are based on Velocity, I tried using velocity style formatting to get it to do what I want but it simply ignores it. For the record, here's how you do it in velocity:

$date.get('yyyy-M-d')

0

Hi Marc,

Thanks for your reply.

I suspected the region settings too, but didn't find any correlation between them and what I get with IDEA. On this machine, my Windows short date format is yyyy-MM-dd, and my long date format is dddd, d MMMM yyyy. Neither bears any relation to the format IDEA is putting in for $DATE, i.e. dd/MM/yyyy.

Funnily enough, I get the Mar 09, 2006 style on my home machine.

I suspect it is using a Java locale setting. Perhaps I will investigate that, but I was hoping someone from JetBrains might provide an easy and authoritative answer.

John Hurst
Wellington, New Zealand

0

Hello John,

JH> How can I change the format used by the $DATE macro in file
JH> templates? I am getting dates like this:
JH>
JH> 28/02/2006
JH>
JH> I want ISO dates, like this:
JH>
JH> 2006-02-28
JH>
JH> I'm using IDEA 5.1 on Win XP. In my Windows Regional Settings, I
JH> have specified my preferred short date format as yyyy-MM-dd, but I
JH> don't know if this is relevant.

There is no way to customize the date format with the file templates mechanism.
You can use $YEAR-$MONTH-$DAY instead of $DATE, or use Velocity features
to get a correct format.

--
Dmitry Jemerov
Software Developer
JetBrains, Inc.
http://www.jetbrains.com
"Develop with pleasure!"


0

Dmitry,

Thanks for your reply.

$YEAR-$MONTH-$DAY gives me "$YEAR-$MONTH-9" with IDEA 5.1 on Windows.

With spaces, i.e. $YEAR -$MONTH -$DAY, I get "2006 -3 -9". I admit this is an improvement, but not quite there yet! (I really want to see "2006-03-09". Strange though that may seem, it is the ISO format for dates, and has a number of advantages!)

Any more suggestions?

John Hurst,
Wellington, New Zealand

0

Whoops,

I see you suggested using Velocity features ... I will look into that. (I don't have any experience with Velocity.)

Thanks

John Hurst,
Wellington, New Zealand

0

Hello John,

Dmitry,

Thanks for your reply.

$YEAR-$MONTH-$DAY gives me "$YEAR-$MONTH-9" with IDEA 5.1 on Windows.

With spaces, i.e. $YEAR -$MONTH -$DAY, I get "2006 -3 -9". I admit
this is an improvement, but not quite there yet! (I really want to
see "2006-03-09". Strange though that may seem, it is the ISO format
for dates, and has a number of advantages!)

Any more suggestions?


$-$-$

--
Serge Baranov
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"


0

Thank you, Serge! You have brought great happiness into my life today.

Your suggestion and a little reading in the Velocity reference manual gives this awkward but effective incantation:

$-#if($MONTH.length()<2)0$#else$#end-#if($DAY.length()<2)0$#else$#end

This seems to be necessary if I really want the months and days to format with two digits. (And I do.)

Thanks

John Hurst
Wellington, New Zealand

0

Does this only work in the File Template?

0

You can make 'the formula' a bit shorter by omitting else branches:
$-#if($MONTH.length()<2)0#end$-#if($DAY.length()<2)0#end$

--
Dmitry

0

Oh, yeah. Doh.

And I called myself a programmer! :)

John Hurst
Wellington, New Zealand

0

I know this is an old post, but I'm looking for this exact thing, but in a live template, not a file template.

Is there any way to accomplish that? I've tried passing the Velocity-style argument string to the date() function in a variable expression, and using the whole Velocity-style $ expression in the template itself, no go.

Any ideas?

Thanks,
Dave

0

Hello Dave,

The date() live template function doesn't offer any way to customize the
date format. You can file a YouTrack issue or, better yet, submit a patch
that adds this feature.

I know this is an old post, but i'm looking for this exact thing, but
in a livew template, not a file template.

Is there any way to accomplish that? I've tried passing the
Velocity-style argument string to the date() function in a variable
expression, and using the whole Velocity-style $ expressoni in the
template itself, no go.

Any ideas?


--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0

I just upgraded from IDEA 9 to 10, and
in IDEA 9 I was getting $DATE resolved to Dec 1, 2010 but IDEA 10 resolves it to 12 /1/10

I prefer the IDEA 9 rendering, how can I make it look like that?

0

IntelliJ IDEA uses the system default short date format to format the value of the variable. It's not configurable in the IDE.

0

It should be. Shoudn't be hard, and different people and companies have different likes and standards.

Dave

0

Hello Dave,

It should be. Shoudn't be hard, and different people and companies
have different likes and standards.


Feel free to file a feature request in YouTrack.

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0

I changed my Region setting for $DATE's short format so that date displays as Jan 18, 2011.
So when at command line I execute the "date" command, it is showing "Jan 18, 2011".
But I'm still getting the $DATE as 1/18/11 within IDEA10.
I rebooted my machine (Windows XP professional SP3), restarted IDEA 10, and still get the $DATE as 1/18/11.

So, it seems it is getting the $DATE from some other place????

0

I only tested this for IntelliJ Live Templates.
Set the $date$ Expression to:
groovyScript("new java.text.SimpleDateFormat(\"yyyy/MM/dd\").format(new java.util.Date())")

0

Just use this with brackets:

 

${YEAR}-${MONTH}-$DAY

0

${YEAR}-${MONTH}-$DAY in a live template gives me ${YEAR}-${MONTH}-$DAY, exactly like that, unevaluated.

IntelliJ IDEA 2017.2.5
Build #IU-172.4343.14
Windows 10 Pro

0

Hi,

Try the below thing, it is working for me.

$today.year-$today.month-$today.day

 

-Ramo

0

Ramo's solution doesn't apply to Live Templates, which is where I personally need it. However, it did get me to look at this again, and I have a solution that seems to be working for me in IDEA 2017.3. I'm using Ultimate, but it should work in other versions too AFAIK.

  1. In the template itself, use the expression $d$, where 'd' can be any variable name you choose; we'll define it in the next step.
  2. Click Edit Variables.
  3. Create a variable with the name you used above, and a value like this: date("yyyy-MM-dd"), where 'yyyy-MM-dd' can be any supported date format.
  4. Check 'Skip if defined', and click OK.
  5. Save the live template.

Hope this helps others, it certainly helps me.

2

Thanks @Dave Merril, works fine for me! Very nice and clean solution

0

Thanks, @Dave. I'm glad to vote the answer, so others can nav to it by one click: SORT BY "Votes"

0

Please sign in to leave a comment.