计算时间和日期差异 [英] Calculating Time and Date difference

查看:137
本文介绍了计算时间和日期差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写一份报告来计算 iReport 中两个日期变量之间的 TotalTimeDifference

I am currently writing a report to calculate the TotalTimeDifference between two date variables in iReport.

我要比较的两个变量是 MO_DATECREATED MO_DATECOMPLETED 我正在尝试仅计算 时间 差异。

The two variables that I am comparing are MO_DATECREATED and MO_DATECOMPLETED and I am trying to calculate the time difference only.

我试过设置一个变量做了两个变量的简单减法 - 当然这根本没有用。

I have tried setting up a variable that does a simple subtraction of the two variables- and of course that has not worked at all.

我会附上我现在拥有的照片,但我正在寻找方式比较两个变量(包含日期/时间)并打印出时间差异的变量。

I will attach photoes of what I currently have but I am looking for the way to compare the two variables (which contains date/time) and printing out a variables with the difference in time.

示例:如果MO 开始于1/2/15 12:55 pm 在2015年1月3日下午1:55完成我想打印时差,或者花了多长时间,因为 25小时

Example: If the MO was started at 1/2/15 12:55pm and completed at 1/3/15 1:55pm i want to print the time difference, or how long it took, as 25 hours

我如何在iReport中执行此操作?感谢您帮助新手!

How can I do this in iReport? Thank you for helping out a newbie!

编辑回答后,我想在节目时间:

EDIT After answer, I would like to the show days to:

推荐答案

你不需要变量,直接使用 textFieldExpression

You do not need variables for this, use directly the textFieldExpression

如果字段 MO_DATECREATED MO_DATECOMPLETED 被声明:

as java.lang.Date

as java.lang.Date

<field name="MO_DATECREATED" class="java.lang.Date">
    <fieldDescription><![CDATA[]]></fieldDescription>
</field>

textFieldExpression ($ F {MO_DATECOMPLETED} .getTime() - $ F {MO_DATECREATED} .getTime())/(1000 * 60 * 60)+hours

嘿,那就是 java 正是如此,要了解它的作用,请查看:如何计算java中的时差?

Hey thats java exactly so to understand what it does check out this: How to calculate time difference in java?

as java.lang.String

as java.lang.String

<field name="MO_DATECREATED" class="java.lang.String">
    <fieldDescription><![CDATA[]]></fieldDescription>
</field>

我们需要将它们解析为 Date object's首先..你的模式是 mm / dd / yy hh:mm a

We need to parse them to Date object's first.. your pattern is mm/dd/yy hh:mm a

(new java.text.SimpleDateFormat("mm/dd/yy hh:mm a").parse($F{MO_DATECOMPLETED}).getTime()-new java.text.SimpleDateFormat("mm/dd/yy hh:mm a").parse($F{MO_DATECREATED}).getTime())/(1000*60*60) + " hours"

考虑到它们可能 null 我们最好再添加 printWhenExpression

Considering that they maybe null we better add a printWhenExpression as well

完成结果

<textField>
     <reportElement x="0" y="0" width="100" height="20" uuid="eac93a84-7901-4205-b09c-556d48dc05e1">
        <printWhenExpression><![CDATA[new java.lang.Boolean($F{MO_DATECREATED}!=null && $F{MO_DATECOMPLETED}!=null)]]></printWhenExpression>
    </reportElement>
    <textFieldExpression><![CDATA[(new java.text.SimpleDateFormat("mm/dd/yy hh:mm a").parse($F{MO_DATECOMPLETED}).getTime()-new java.text.SimpleDateFormat("mm/dd/yy hh:mm a").parse($F{MO_DATECREATED}).getTime())/(1000*60*60) + " hours"]]></textFieldExpression>
</textField>

毫无疑问,它们更好是 java.lang.Date 对象,报告填写速度更快,解析错误没有风险,您可以正确导出到excel ecc。要格式化 java.lang.Date 对象,只需使用pattern属性。

No doubt that it is better that they are java.lang.Date object, both the report will fill faster, no risk for parsing error and you can export correctly to excel ecc. To format a java.lang.Date object as you wish just use the pattern property.

编辑:用户已选择 java.util.Date 并询问他如何显示分钟数,为此我创建了一个关于如何创建一个表达两个日期之间时差的单个表达式作为年,月,日,小时,分钟,秒,现在已回答

Users has opted for the java.util.Date and asked how he can display also minutes, for this I have created a general question on How to create a single expression displaying time difference between two Date's as years, months, days, hours, minutes, seconds and it is now answered

这是临时解决方案

<textField>
     <reportElement x="0" y="0" width="100" height="20" uuid="eac93a84-7901-4205-b09c-556d48dc05e1">
        <printWhenExpression><![CDATA[new java.lang.Boolean($F{MO_DATECREATED}!=null && $F{MO_DATECOMPLETED}!=null)]]></printWhenExpression>
    </reportElement>
    <textFieldExpression><![CDATA[($F{MO_DATECOMPLETED}.getTime()-$F{MO_DATECREATED}.getTime()) / (24* 60 * 60 * 1000)  + " days " +($F{MO_DATECOMPLETED}.getTime()-$F{MO_DATECREATED}.getTime()) / (60 * 60 * 1000) % 24 + " hours " +  ($F{MO_DATECOMPLETED}.getTime()-$F{MO_DATECREATED}.getTime()) / (60 * 1000) % 60 + " minutes"]]></textFieldExpression>
</textField>

这篇关于计算时间和日期差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆