【发布时间】:2014-10-28 05:44:38
【问题描述】:
我对 freemarker 日期有疑问。
我想比较日期并打印日期差异。
第一个日期被传递给模型(java.util.Date)
第二个是freemarker .now var。
所以我正在这样做:
<#assign currentDate = .now>
<#if currentDate?long < trip.endDate?long> <#-- Not ended -->
这工作正常,但是当我尝试获取当天差异时,如下所述:How do I calculate the difference in days between two ISO8601 dates with Freemarker?
我在做:
<#assign currentDate = .now>
${((trip.startDate?datetime("yyyy-MM-dd'T'HH:mm:ssZ")?long - currentDate?datetime("yyyy-MM-dd'T'HH:mm:ssZ")?long) / (1000 * 60 * 60 * 24))?int}
这是在扔:
Expected method. trip.startDate?datetime evaluated instead to freemarker.template.SimpleDate
然后我尝试添加?date,因为我看到有些人这样做是为了获得正确的对象类型,就像这样
<#assign currentDate = .now>
${((trip.startDate?date?datetime("yyyy-MM-dd'T'HH:mm:ssZ")?long - currentDate?datetime("yyyy-MM-dd'T'HH:mm:ssZ")?long) / (1000 * 60 * 60 * 24))?int}
它会抛出:
Cannot convert DATE into DATETIME
只是为了确保我使用的对象是java.util.Date,这是我的 Trip 类:
package com.test.planner.dto;
import java.util.Date;
public class Trip
implements Comparable<Trip> {
private Date startDate; // start date
private Date endDate; // end date
// ...
public Date getStartDate() {
return this.startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public Date getEndDate() {
return this.endDate;
}
public void setEndDate(Date endDate) {
this.endDate = endDate;
}
@Override
public int compareTo(Trip o) {
return this.getStartDate().compareTo(o.getStartDate());
}
}
我将其传递给模型:
Trip trip = new Trip();
trip.setStartDate(new Date(1410318000000L)); // Wed Sep 10 2014 00:00:00 GMT-0300 (BRT)
trip.setEndDate(new Date(1410318000000L + 691200000L)); // Plus 8 days
mav.addObject("trip", trip);
【问题讨论】:
-
你为什么打电话给
?datetime? -
因为在我提供的链接中有说明:stackoverflow.com/questions/11412040/…是不是错了?
-
链接日期采用特定格式。
标签: java date datetime freemarker