简体   繁体   中英

Stringtemplate - How to set date format to locale format

I'm processing Excel files with ExcelExplorer based on Stringtemplate4 (ST). The files contain several columns with dates. By default, the dates are rendered following the "MM/dd/yy" date format.

Is there a way to render the dates as "dd/MM/yyyy"?

I've tried it in several ways:

  • I've tried defining it via the command line, without success.
    • Defining LC_ALL=fr_FR doesn't work.
    • Defining LC_TIME="dd/MM/yyyy" doesn't work. See Setting java locale settings
    • Calling java with the following command line options doesn't work.

java -Duser.language=fr -Duser.country=FR -Duser.variant=UTF-8 ...

I've tried the following templates without success:

renderRow(row) ::= <<

<row.MyDate; format="dd/MM/yyyy"> 
>>

Although attribute MyDate is defined as a Date type, the above doesn't work. I don't want to define MyDate as a Date type in Java as proposed in Format date in String Template email

NB: After checking, I found out that ExcelExporter/ST defines attribute MyDate as a Date type!

The following template doesn't work either :

renderRow(row; format="dd/MM/yyyy") ::= <<

<row.MyDate> 
>>

You need to add a renderer to your STGroup for each class you want to format:

    dir = STGroupDir(templateDirectory, '$', '$')
    dir.registerRenderer(Number.class, NumberRenderer())
    dir.registerRenderer(Date.class, DateRenderer())

Now, in my templates, I can use

<row.MyDate; format="dd/MM/yyyy"> <row.MyDate; format="dd/MM/yyyy"> format string is used with java.text.SimpleDateFormat

or

<row.MyNumber; format="%,d"> <row.MyNumber; format="%,d"> format string is used with java.util.Formatter

If you need a custom formatter, take a look at the DateRenderer , it would be pretty straightforward to create your own.

Here's the documentation:

https://github.com/antlr/stringtemplate4/blob/master/doc/renderers.md

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM