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"?
java -Duser.language=fr -Duser.country=FR -Duser.variant=UTF-8 ...
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!
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.