5

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> 
>>
  • 2
    After reading the article in [Format date in String Template email] (https://stackoverflow.com/questions/2728623/format-date-in-string-template-email), the proper way to solve this is to add a property method in java while leaving the template **unchanged**. This solution strictly maintains model-view separation. – Eric Belpaire Jul 04 '17 at 16:21

1 Answers1

0

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"> format string is used with java.text.SimpleDateFormat

or

<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

mikeb
  • 10,578
  • 7
  • 62
  • 120