0

I'm using primefaces and I have a datatable witch contains 3 columns: DateStart, DateEnd, and NombreOfDay, I want to get the difference between the DateStart and DateEnd and put the result on NombreOfDay, this is what I try to do :

 <p:dataTable var="demande"   value="#{myBean.allDemandes}"> 
 <p:column headerText="Start:" >
 <h:outputText value="#{myBean.dateStart}" >
 <f:convertDateTime type="date" pattern="dd.MM.yyyy" timeZone="CET" />   </h:outputText></p:column>

 <p:column headerText="End:" > 
<h:outputText value="#{myBean.dateEnd}" >
<f:convertDateTime type="date" pattern="dd.MM.yyyy" timeZone="CET" /></h:outputText>
 </p:column>
 <!-- Nombre of days between two date -->
 <p:column headerText="Nbr"   >
    <h:outputText value="#{demande['dateEnd']-demande['dateStart']}"  >
    </h:outputText>
    </p:column>

but I get this error:

   java.lang.IllegalArgumentException: Cannot convert 31/03/15 00:00 of type class java.util.Date to Number
at org.apache.el.lang.ELArithmetic.coerce(ELArithmetic.java:407) [jbossweb-7.0.13.Final.jar:]
at org.apache.el.lang.ELArithmetic.subtract(ELArithmetic.java:315) [jbossweb-7.0.13.Final.jar:]
at org.apache.el.parser.AstMinus.getValue(AstMinus.java:41) [jbossweb-7.0.13.Final.jar:]
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189) [jbossweb-7.0.13.Final.jar:]

any idea ?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
sad
  • 49
  • 1
  • 13

2 Answers2

2

The - operator in EL operates on java.lang.Number only. The java.util.Date isn't a subclass of that. You need to get the raw timestamp out of it via Date#getTime() method which returns the epoch time in milliseconds in flavor of long which is in EL interpreted as java.lang.Long, a fullworthy subclass of java.lang.Number.

So, basically the below would print the difference in milliseconds:

<p:column headerText="Nbr">
    #{demande.dateEnd.time - demande.dateStart.time}
</p:column>

(note that I simplified the EL expression by getting rid of unnecessary brace notation and output text)

Ignoring the DST influences, you could just divide the result by the amount of milliseconds a full day can consist: 1000 * 60 * 60 * 24:

<p:column headerText="Nbr">
    #{(demande.dateEnd.time - demande.dateStart.time) / (1000 * 60 * 60 * 24)}
</p:column>

If you'd like to take the DST influences into account, then you'd really better do the job in Java side, preferably with a reusable EL function. JSF utility library OmniFaces has one out the box, the of:daysBetween() (source code here):

<p:column headerText="Nbr">
    #{of:daysBetween(demande.dateStart, demande.dateEnd)}
</p:column>
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
1

You cannot subtract Date objects the way you tried to. The - operator expects the arguments to be a number (or at least can be casted). This is where the exception occurs. What would you even expect as a result? Days? Minutes?

Do the logic within the bean, i.e. write a new method for that purpose. This way, you'd have something like:

<h:outputText value="#{myBean.calcDurationInDays(demande)}" />

Given that you have a class Demande, your backing bean method could be:

public int calcDurationInDays(Demande demande){
   //calculate difference between demande's start and end date
   return difference;
}

There are numerous threads and libraries on how to get the difference between to dates in java, e.g.: How do you subtract Dates in Java?

Community
  • 1
  • 1
jp-jee
  • 1,502
  • 3
  • 16
  • 21
  • thanks @jp-jee I follow your proposition and I used the Joda Time API so that become easy – sad Mar 31 '15 at 08:47