如何在几分钟内检索两个 Date 实例之间的差异?

How can I retrieve the difference between two Date instances in minutes?

根据the Talend docs,可以使用TalendDate.dateDiffFloor计算两个日期之间的差异,最高可达秒和毫秒。

但是,文档没有说明如何(或必须指定哪个格式参数)。

我必须在几秒钟内得到这个值,因为它将输入 tInfiniteLoop 组件。

我试过 "mmmm" 和“mm”,但没有成功。在那种情况下,我得到错误:

Exception in component tMap_3
java.lang.RuntimeException: Can't support the dateType: mm ,please try "yyyy" or "MM"
    at routines.TalendDate.diffDateFloor(TalendDate.java:661)
    at timeouttester.job_2_0_1.job_2.tInfiniteLoop_2Process(job_2.java:1156)
    at timeouttester.job_2_0_1.job_2.runJobInTOS(job_2.java:1616)
    at timeouttester.job_2_0_1.job_2.main(job_2.java:1464)

我必须指定哪个参数才能获得会议记录?而且,如果不可行,如果无法通过 Talend,在 "vanilla" Java 中可能有解决方法?

我目前在 tMap 组件中使用以下语句:

TalendDate.diffDateFloor(TalendDate.parseDateInUTC("EEE, d MMM yyyy HH:mm:ss", Var.expires), TalendDate.getCurrentDate(), "mmmm") 

我不知道 Talend 但正如你所说 vanilla 对你来说也是可能的:你可以使用 class Duration 代表 duration 在类型 Temporal 的两个对象之间。例如,这样的时间 class 将是 Instant,它是时间线上的快照。 Instant 在 API 中得到了大力支持。您可以从 Date#toInstantCalendar#toInstant 等中检索一个。

看这个小例子:

Date dateBefore = new GregorianCalendar(2014, Calendar.FEBRUARY, 11).getTime();

Instant before = dateBefore.toInstant();
Instant now = Instant.now();

Duration duration = Duration.between(before, now);
System.out.println("Days: " + duration.toDays());

但是,如果我没记错的话,class Instant 本身只能精确到毫秒。如果这还不够,您可以使用另一个实现 Temporal 的 class 或创建您自己的

对于您的申请,您可能感兴趣:

Duration#toMinutes()
Duration#toMillis()

请注意,这些方法的 returned 值 (long) 向下舍入 (floor)。因此,如果 真实 的分钟数,其中 1.453toMinutes() 将 return 1toMillis() returns 1453.