Search code examples
htmlspringthymeleaf

property inside local date time in thymeleaf


LocalDateTime as application.property

My application.properties looks like this:

date.format='dd-MM-yyyy'

<p th:text="${#temporals.format(localDateTime, 'dd-MM-yyyy')}"></p>

How to replace 'dd-MM-yyyy' wit my property?


Solution

    1. You can get property from @environment.

      @environment.getProperty('date.format')
      
    2. But this will not solve it for you, next step is to preprocess this expression with __ notation

      <p th:text="${#temporals.format(localDateTime, __${@environment.getProperty('date.format')}__)}">
      
    3. But even this version will not work, because you format needs quotes format(localDateTime, 'dd-MM-yyyy') and you are passing format(localDateTime, dd-MM-yyyy) so the final working version should look like this:

      <p th:text="${#temporals.format(localDateTime, __${''''[email protected]('date.format')+''''}__)}">
      

    So as you can see it is much easier to just pass format from controller)