如何使用Thymeleaf将org.joda.time.LocalDateTime转换或反序列化为毫秒?

时间:2016-01-10 19:48:45

标签: java jquery jodatime thymeleaf joda-convert

我有Thymeleaf代码(此代码需要我为JQuery添加参数 - DatePicker):

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]"

现在页面看起来像:

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

但我不会像这个例子那样转换为毫秒:

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

我可以使用Thymeleaf并制作类似于JsonDeserializer的东西..在构建页面之前将数据转换为毫秒?

结果:

我决定发送毫秒列表,第二个我现在正在使用下一个代码,以防需要将服务器端的变量添加到JS:


bool <= 0;
d<=-d;
 when d>0;

1 个答案:

答案 0 :(得分:1)

嗯,看起来在Thymeleaf看起来没有内置功能。 但您可以使用以下函数编写简单的Javascript函数将DateTime转换为Time(以毫秒为单位):

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

另一方面,如果您需要以毫秒为单位的时间,为什么不在Java端转换它并将处理后的最终变量集(包含以毫秒为单位的时间)发送给Thymeleaf?