Location>code7788 >text

IntelliJ IDEA Insert Time Text

Popularity:961 ℃/2024-09-28 14:12:41

IntelliJ IDEA Insert Time Text

Demand: In the use of IDEA to edit some text, the need to insert a specified format of the current time text, the first thing that comes to mind is to find whether there is a related IDEA plug-ins, to see that there are other apes have indeed done the relevant plug-ins, but at that time, I found the article is the need to download the offline plug-ins provided by the blogger jar packages, and the time format can be flexibly customize the still unknown, and then just had a flash of inspiration, there are other ways, a method that does not require installation of plug-ins and more flexible and free, so did not take a closer look at the next. A do not need to install plug-ins, and more flexible and free method, so did not look down.

1 and 2 can be skipped if you're interested in learning more about them, and the specifics are in 3

1. Ideas for solutions

The use of IDEA's Live Template ( I do not remember what the Chinese translation called, it seems to be called hot load template or something, the back of the unified called preset templates ) function to do, this setup is located in theFile | Settings | Editor | Live TemplatesLive Template is IDEA's preset code template tool, set the content of the code can be achieved through the complex function to dynamically inject the required package, method, time, author and other programmers want to need the content of the template, IDEA in fact, we have preset a lot of templates, the most familiar than thesoutIt is located by default under the Live Template grouping of Java in Live Templates (see below).

image-20240809011521923

So the solution idea is: use the preset template that comes with IDEA to define some keywords and get the current time and format it in the preset content triggered by that keyword.

2. Prior knowledge

image-20240809013722512

or usesoutFor example, as shown above, I have divided the editing of the preset templates into 7 sections:

  • ① Trigger keyword, i.e., Abbreviation in Fig;

  • ② Template comments, which are displayed next to each use;

    image-20240809012953552

  • ③ Template text content;

  • ④ Variables contained in the text content of the template, using the two$symbol package, the middle is the name of the variable, the variable is the core of the preset template, it is able to dynamically generate the content of the code is the main basis, the variable to be inserted into the content can be edited by some expressions (IDEA some preset variables except, for example, in the figure of the$END$(which indicates the position of the cursor after the template is triggered);

  • ⑤ Variable editing module (find an editable variable, as shown below), the editable items of the template variable include the name, expression, default value (the value when the expression returns null), and the "Skip if defined" checkbox, of which the core item is the expression, which can be used by a number of IDEA-provided functions to return a specific value. The core item is the expression, which can be used to return a specific value through some functions provided by IDEA, a list of which can be found on the IntelliJ website (List of functions), as to the last itemSkip if definedcheckbox, my current level of English understanding is that if the previous expression and the default value of the final result of the run is not empty, then the cursor does not stop at this variable (PS: if only defined variables do not define the expression, then the cursor will be in the variables in turn, the process, the preset template every trigger, jump to the next variable), directly jump to the next variable, the specific interpretation of the configuration of these items You can view the specific interpretation of these configurationsofficial websiteSelf-translation;

    image-20240809014108508

  • ⑥ Trigger this preset template key, the default is the Enter key, as can be seen from the figure, you can also configure the tab key and the space bar (None configured into this currently do not know what the significance of configuring this can only be triggered with a mouse click);

  • ⑦ Configure the language code area in which this template takes effect, which can be configured individually for each language.

    image-20240809015622825

3. Practical exercises

There are 2 main functions provided by IDEA that are used this time.date([format])cap (a poem)time([format])

function (math.) Function
time([format]) Returns the current system time.
By default, without a parameter, it returns the time in the current system format. To use a different format, provide a parameter according to the SimpleDateFormat specification. For example, the time("H:m z") returns the time formatted as 13:10 UTC.
date([format]) Returns the current system date.
By default, without a parameter, it returns the date in the current system format. To use a different format, provide a parameter according to the SimpleDateFormat specification. For example, the date("Y-MM-d, E, H:m") returns the date formatted as 2020-02-27, Thu, 16:11.

① Access to IDEAFile | Settings | Editor | Live TemplatesClick on the top left corner+No. Select Create Template Group and name it asInertTime(Depends on personal preference)

image-20240809020011760

② Select the newly created template group, and click the upper-left corner of the+No. Select Create Live Template, trigger keyword iscurt(current time, depending on personal preference 😀 ), annotated asCurrent time: yyyy-MM-dd HH:mm:ssThe content of the input template is$datetime$, edit the variable with the expression set todate("yyyy-MM-dd HH:mm:ss"), set its application scope to Everywhere, open any IDEA file and verify.

PixPin_2024-08-09_02-16-24

③ The process is basically the same, and I've created the following template based on the requirements I've encountered so far that I've been able to exhaust:

Keywords. marginal notes Template content variable expression
curdt1 Current date time: yyyy-MM-dd HH:mm:ss $datetime1$ date("yyyy-MM-dd HH:mm:ss")
curdt2 Current date and time: yyyyy-M-d H:m:s $datetime2$ date("yyyy-M-d H:m:s")
curdt3 Current date and time: yyyy year MM month dd day HH hour mm minute ss second $datetime3$ date("yyyy year MM month dd day HH hour mm minute ss second")
curdt4 Current date and time: yyyy year M month d day H hour m minute s second $datetime4$ date("yyyy year M month d day H hour m minute s second")
curdt5 Current date and time: yyyy/MM/dd HH:mm $datetime5$ date("yyyy/MM/dd HH:mm")
curdt6 Current date and time: yyyy/M/d H:m $datetime6$ date("yyyy/M/d H:m")
currd1 Current date: yyyyy-MM-dd $date1$ date("yyyy-MM-dd")
currd2 Current date: yyyyy-M-d $date2$ date("yyyy-M-d")
currd3 Current date: yyyy/MM/dd $date3$ date("yyyy/MM/dd")
currd4 Current date: yyyy/M/d $date4$ date("yyyy/M/d")
currt1 Current time: HH:mm:ss $time1$ time("HH:mm:ss")
currt2 Current time: H:m:s $time2$ time("H:m:s")

Description:

  1. yyyy-MM-dd HH:mm:sstogether withyy-M-d H:m:sDistinction.

    Difference between yyyy and yyyy: yyyy is formatted to retain only the last two digits of the year;

    Month, Day, Hour, Minute, Second Difference: only one letter format When the number is less than 10, the number has only 1 digit, and two letters will make up the two digits with zeros.

    See for specific formatting rules:SimpleDateFormat

  2. The tables are all in 24-hour format; if you want to use a 12-hour format, you can set theHChanged to lowercaseh

  3. To insert milliseconds, add an uppercase to the time formatting expressionSwhich can be formatted as milliseconds (btw: add the lowercasezcan be formatted as a time zone by adding the uppercaseE(An abbreviated week in English may be inserted).

Finally: the above is the time format I can use for my current needs, if you need other formats, you can add them according to the rules.