Search code examples
javajqueryjodatimethymeleafjoda-convert

How to convert or deserialize org.joda.time.LocalDateTime to milliseconds using Thymeleaf?


I have Thymeleaf code (this code need for me to add parameter for JQuery - DatePicker):

<table th:attr="data-availible-dates=${defoultSetting.avalibleDates}">

now page looks like:

data-availible-dates="[2014-01-09T00:00:00.000, 2014-01-14T00:00:00.000, 2014-01-10T00:00:00.000, 2014-01-23T00:00:00.000, 2014-01-15T00:00:00.000, 2014-01-06T00:00:00.000, 2014-01-24T00:00:00.000, 2014-01-20T00:00:00.000, 2014-01-16T00:00:00.000, 2014-01-21T00:00:00.000, 2014-01-08T00:00:00.000, 2014-01-22T00:00:00.000, 2014-01-17T00:00:00.000, 2014-01-13T00:00:00.000]"

but I wont to convert to milliseconds like this example:

data-availible-dates="[1451170800000, 1452380400000, 1452466800000, 1452553200000]"

Can I use Thymeleaf and make something similar to JsonDeserializer.. which will be convert data to milliseconds before build the page ?

Result:

I decided to send the list of milliseconds, and second I am using now next code in case when need to add variables from server side to JS:

<script type="text/javascript" th:inline="javascript">
/*<![CDATA[*/
    var avalibleDates = /*[[${defoultSetting.avalibleDates}]]*/;
/*]]>*/
</script>

Solution

  • Hmm, doesn't look like there's a built-in function to do that in Thymeleaf. But you can write simple Javascript function to convert DateTime to Time in milliseconds, using function below:

    var myDate = new Date("2012-02-10T13:19:11+0000");
    var result = myDate.getTime();
    

    On the other hand, if you need to have time in milliseconds, why you will not convert it on Java side and send processed final variable set (containing times in milliseconds) to Thymeleaf?