简体   繁体   English

使用dataTable列的日期转换器

[英]Date converter using columns of dataTable primefaces

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 : 我使用的是primefaces,我有一个数据表,其中包含3列:DateStart,DateEnd和NombreOfDay,我想获取DateStart和DateEnd之间的差并将结果放在NombreOfDay上,这是我尝试做的事情:

 <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 ? 任何想法 ?

The - operator in EL operates on java.lang.Number only. EL中的-运算符仅在java.lang.Number运行。 The java.util.Date isn't a subclass of that. java.util.Date不是java.util.Date的子类。 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 . 你需要通过获得的原始时间戳出来的Date#getTime()返回以毫秒为单位的时代时间的味道方法long这是在EL解释为java.lang.Long ,一个fullworthy子类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 ) (请注意,我通过消除不必要的花括号符号输出文本来简化了EL表达式)

Ignoring the DST influences, you could just divide the result by the amount of milliseconds a full day can consist: 1000 * 60 * 60 * 24 : 忽略DST的影响,您可以将结果除以整天可以包括的毫秒数: 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 . 如果您想考虑DST的影响,那么您最好在Java方面做这项工作,最好使用可重用的EL函数 JSF utility library OmniFaces has one out the box, the of:daysBetween() ( source code here ): JSF实用程序库OmniFaces提供了一个开箱即用的of:daysBetween()此处为源代码 ):

<p:column headerText="Nbr">
    #{of:daysBetween(demande.dateStart, demande.dateEnd)}
</p:column>

You cannot subtract Date objects the way you tried to. 您不能以尝试的方式减去Date对象。 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, ie write a new method for that purpose. 在bean中执行逻辑,即为此编写一个新方法。 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: 假设您有一个Demande类,则后备bean方法可以是:

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, eg: How do you subtract Dates in Java? 关于如何在Java中获取日期之间的区别,有许多线程和库,例如: 如何在Java中减去日期?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM