簡體   English   中英

如何以分鍾為單位檢索兩個Date實例之間的差異?

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

根據Talend文檔 ,可以使用TalendDate.dateDiffFloor來計算兩個日期之間的差值, 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在“原始” Java中執行此操作?

我目前在tMap組件中使用以下語句:

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

我對Talend毫無頭緒,但是正如您所說,也可以使用vanilla :您可以使用Duration類,它表示兩個Temporal類型的對象之間的duration Temporal 這樣的時間類別例如可以是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());

但是,如果我沒有記錯的話, Instant類本身僅精確到毫秒。 如果這還不夠,您可以使用另一個實現Temporal類或創建自己的類。

對於您的應用程序,您可能感興趣:

Duration#toMinutes()
Duration#toMillis()

請注意,這些方法的返回值( long )向下取整( floor )。 因此,如果實際分鍾toMinutes()例如1.453 ,則toMinutes()將返回1toMillis()將返回1453

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM