簡體   English   中英

如何將 Joda 的持續時間轉換為 java.time.Duration?

[英]How can I convert a Joda's Duration to a java.time.Duration?

我想將像1d2h3m4s這樣的字符串解析為java.time.Duration 我可以使用 joda 的PeriodFormatter將字符串解析為org.joda.time.Duration但我不知道如何將其轉換為標准 Java8 的java.time.Duration

我必須連接一些已經期望java.time.Duration作為輸入的“遺留”代碼,但我想使用 joda 的parsePeriod因為1d2h3m4s java.time.Duration.parse()只接受 ISO-8601 不是持續時間格式(符合 ISO-8601 持續時間)

import org.joda.time.format.PeriodFormatter;
import org.joda.time.format.PeriodFormatterBuilder;

...

final PeriodFormatter periodFormatter = new PeriodFormatterBuilder()
        .printZeroNever()
        .appendDays().appendSuffix("d")
        .appendHours().appendSuffix("h")
        .appendMinutes().appendSuffix("m")
        .appendSeconds().appendSuffix("s").toFormatter();

org.joda.time.Duration myduration = periodFormatter.parsePeriod("1d1s").toStandardDuration(); 
java.util.time myduration2 = XXXXX

請記住,我並不是想從我的代碼中刪除org.joda.time.Period的使用,例如將 org.joda.time.Period 轉換為 java.time.Period I still want to have a org.joda.time.Period because I have more parsing option to generate those, and I need a java.time.Period / java.time.Duration because I interact with some other API/libraries that expect java.time.* .

那么,有沒有辦法將org.joda.time.Duration轉換為java.time.Duration

我提出三個選項

  1. 手動將字符串修改為 ISO 8601 格式,以便java.time.Duration可以解析它。
  2. 通過毫秒數轉換。
  3. joda.time.Duration.toString()轉換。

修改字符串

就我個人而言,我認為我不想依賴 Joda-Time。 相反,我會將您的非 ISO 8601 字符串修改為 ISO 8601 格式。

    String durationString = "1d1s";
    // Convert to ISO 8601 format
    String isoString = durationString.replaceFirst("(.*?[Dd])?(.*)", "P$1T$2");
    java.time.Duration dur = java.time.Duration.parse(isoString);
    System.out.println(dur);

Output 是:

PT24H1S

我承認正則表達式有點難以閱讀。 如果字符串中有大寫或小寫的D ,則在 ISO 字符串中,直到並包括該D在內的所有內容都放在T之前。 其他所有內容都放在T之后。

毫秒級轉換

這是一個類似於 ecerulm 答案中的想法。 我不想失去 Joda-Time 持續時間的毫秒精度,所以我依賴毫秒而不是秒(我知道對於您的示例字符串,它不會有任何區別)。

    final PeriodFormatter periodFormatter = new PeriodFormatterBuilder()
            .printZeroNever()
            .appendDays().appendSuffix("d")
            .appendHours().appendSuffix("h")
            .appendMinutes().appendSuffix("m")
            .appendSeconds().appendSuffix("s").toFormatter();

    org.joda.time.Duration myduration = periodFormatter.parsePeriod("1d1s").toStandardDuration(); 

    java.time.Duration dur = java.time.Duration.ofMillis(myduration.getMillis());

結果和以前一樣。

轉換一個字符串

    java.time.Duration dur = java.time.Duration.parse(myduration.toString());

結果還是一樣的。 不過,將持續時間格式化為 ISO 8601 字符串只是為了將其解析回來感覺像是一種浪費。

您可以使用Period#toStandardSeconds()#getSeconds()PeriodFormatter#parse()返回的org.joda.time.Period轉換為秒。 這將為您提供int的秒數

然后使用java.time.Duration.ofSeconds()從該int創建一個java.time.Duration

import org.joda.time.format.PeriodFormatterBuilder
import org.joda.time.format.PeriodFormatter

PeriodFormatter formatter = new PeriodFormatterBuilder()
        .printZeroNever()
        .appendDays().appendSuffix("d")
        .appendHours().appendSuffix("h")
        .appendMinutes().appendSuffix("m")
        .appendSeconds().appendSuffix("s")
        .toFormatter();

Period myPeriod = formatter.parsePeriod("1d2h3m4s");

java.time.Duration myDuration = java.time.Duration.ofSeconds(
        myPeriod.toStandardSeconds().getSeconds());

暫無
暫無

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

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