3

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?

2 Answers 2

7

If your environment supports EL 2.2, just call its toString() method directly.

<c:if test="#{bean.myDate.toString() eq 'Wed Jan 01 00:00:00 CET 1001'}">

If yours doesn't, convert it to String first by inlining it as body of <c:set>.

<c:set var="myDate">#{bean.myDate}</c:set>
<c:if test="#{myDate eq 'Wed Jan 01 00:00:00 CET 1001'}">

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 Jan would fail and if the webserver is moved to non-CET timezone, then the test on CET would fail). Don't you rather want to compare it on year 1001 directly or a fixed pattern such as 1001-01-01 or so? Further I also wonder why you don't do the test in the rendered attribute of the JSF <h:outputText> component.

Sign up to request clarification or add additional context in comments.

3 Comments

Sorry, what do you mean with "compare on year directly or so"?
Works for me with JSTL 1.2 on Mojarra 2.1.14. I recall some problems when MyFaces is used. What JSF impl/version are you using? And what JSTL version? Does your environment really not support EL 2.2? Some of your previously asked questions implies that you're using Java EE 6 and thus implicitly Servlet 3.0 and thus implicitly EL 2.2. What servletcontainer impl/version are you actually using? As to comparing on year, just check if year equals 1001 or a fixed pattern without locale/timezone sensitiveness such as yyyy-MM-dd.
@BalusC public Boolean getShow(){ if(this.myDate.toString() .equals("Wed Jan 01 00:00:00 CET 1001")){ // do not show it return false; }else{ return true; } } and in IHM I do this : <h:outputText value="#{bean.myDate }" rendered="#{bean.show}"> </h:outputText>
0

in jsf 1.2

this code works for me :

                           <webuijsf:button
                            actionExpression="#{guiRettificaBilPuntuale_2.button4_action}"
                            id="button4" text="#{bundle.TastoModificaRiga}" 
                            rendered="#{ bilDetailsTable.value.settStatus eq 'N' or bilDetailsTable.value.starttime eq aLGUISessionBean_01.monthCurrentPublBilDate ? false : true}">

and starttime and monthCurrentPublBilDate are both dates (java.Date) so you can transform string in Date and compare them

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.