Comparison of Dates
Jaime Garza <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.devel |
|---|---|
| Message-ID | <9FFDAB70F49C7F43A160826020F7CDF9745C5D22@CORPEX14-MBN1.us.responsys.com> |
Consider the following code in ftl:
<#assign todaysdate = "Jan 30, 2013"?date>
<#assign testdate = ("2013-01-30T11:00:00")?date("yyyy-MM-dd'T'HH:mm:ss")>
Todaysdate: ${todaysdate}
Testdate:${testdate}
<#if todaysdate?date <= testdate?date>
It is less than
</#if>
I get
Todaysdate: Jan 30, 2013
Testdate:Jan 30, 2013
It is less than
It turns out that the time portion is still used to compare the dates, as per the code below:
else if(ltm instanceof TemplateDateModel && rtm instanceof TemplateDateModel) {
TemplateDateModel ltdm = (TemplateDateModel)ltm;
TemplateDateModel rtdm = (TemplateDateModel)rtm;
int ltype = ltdm.getDateType();
int rtype = rtdm.getDateType();
if(ltype != rtype) {
throw new TemplateException(
"Can not compare dates of different type. Left date is of "
+ TemplateDateModel.TYPE_NAMES.get(ltype)
+ " type, right date is of "
+ TemplateDateModel.TYPE_NAMES.get(rtype) + " type.",
env);
}
if(ltype == TemplateDateModel.UNKNOWN) {
throw new TemplateException(
"Left date is of UNKNOWN type, and can not be compared.", env);
}
if(rtype == TemplateDateModel.UNKNOWN) {
throw new TemplateException(
"Right date is of UNKNOWN type, and can not be compared.", env);
}
Date first = EvaluationUtil.getDate(ltdm, left, env);
Date second = EvaluationUtil.getDate(rtdm, right, env);
comp = first.compareTo(second);
}
Here first and second still contain the time portion since SimpleDate just returns the current date without any modifications regarding its internal type. So I get "less than". The problem is compounded since I am using .now, and not a hardcoded string for the full date.
Questions:
Are you guys planning on fixing this behavior? Currently only converting a date to string, and back to a date will work.
Was this as planned?
Thanks.
------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_d2d_jan