How to compare java.util.Date value with String using JSTL and EL?
This is the model:
private Date myDate; // Wed Jan 01 00:00:00 CET 1001
This is the view:
<c:if test="#{bean.myDate eq 'Wed Jan 01 00:00:00 CET 1001'}">
<h:outputText value="#{bean.myDate}">
<f:convertDateTime pattern="EEEE dd MMM yyyy @ HH:mm:ss" />
</h:outputText>
</c:if>
However the condition always evaluates false. How is how can I solve it?
If your environment supports EL 2.2, just call its
toString()method directly.If yours doesn’t, convert it to
Stringfirst by inlining it as body of<c:set>.Unrelated to the concrete problem, this is a rather strange way of comparing dates. It’s very sensitive to changes in locale and timezone (if the webpage visitor uses a non-English locale and your webapp supports non-English locales, then the test on
Wed Janwould fail and if the webserver is moved to non-CET timezone, then the test onCETwould fail). Don’t you rather want to compare it on year1001directly or a fixed pattern such as1001-01-01or so? Further I also wonder why you don’t do the test in therenderedattribute of the JSF<h:outputText>component.