我在freemarker日期有問題。的錯誤在Freemarker中打印日期天差異
我想比較日期並打印日差。 將第一個日期傳遞給模型(java.util.Date) 第二個是自由標記.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
然後我tryied加入?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'? – 2014-09-03 18:22:10
因爲我提供的鏈接中聲明:http://stackoverflow.com/questions/11412040/how-do-i-calculate-the-difference-in-days-between-two-iso8601-dates-with-freemar 那是錯的嗎? – 2014-09-03 18:23:00
在該鏈接日期是在特定的格式。 – 2014-09-03 18:24:45