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
请先登录再写评论。
It would also be nice to get the $DATE into live templates.
Hi all,
Should I assume since there's no answer, that there is no easy way to do this?
John Hurst
Wellington, New Zealand
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')
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
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!"
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
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
Hello John,
$-$-$
--
Serge Baranov
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
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
Does this only work in the File Template?
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
Oh, yeah. Doh.
And I called myself a programmer! :)
John Hurst
Wellington, New Zealand
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
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.
--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"
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?
IntelliJ IDEA uses the system default short date format to format the value of the variable. It's not configurable in the IDE.
It should be. Shoudn't be hard, and different people and companies have different likes and standards.
Dave
Hello Dave,
Feel free to file a feature request in YouTrack.
--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"
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????
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())")
Just use this with brackets:
${YEAR}-${MONTH}-$DAY
${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
Hi,
Try the below thing, it is working for me.
$today.year-$today.month-$today.day
-Ramo
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.
Hope this helps others, it certainly helps me.
Thanks @Dave Merril, works fine for me! Very nice and clean solution
Thanks, @Dave. I'm glad to vote the answer, so others can nav to it by one click: SORT BY "Votes"
Also having issue trying to find day of week name for live templates. Hope this helps someone else.
- Dave Spiegel
I used live template variable expression date("EEEEE yyyy-MM-dd HH:mm:ssZ") to get Friday 2019-07-19 11:25:50-0600
Couple helpful tables with definitions and example at Jenkov Tutorials site which seem to work properly with live templates:
Here is one which shows Java date format character definitions: