gpt4 book ai didi

java - 将 double 表示的小时数转换为持续时间

转载 作者:行者123 更新时间:2023-12-02 09:37:06 25 4
gpt4 key购买 nike

我有一个以 Double 形式存储在数据库中的持续时间,但在 LocalTime 上的操作中,我需要它的类型为 Duration。我需要能够从双倍更改为持续时间,反之亦然。

我预计 3.5 小时的持续时间为 3 小时 30 分钟。

最佳答案

    // the Double value
Double someTime = 3.5;

// two ways of converting it to Duration
// (we need to use minutes because Duration.of expects a long, which cannot be fractional
Duration someDuration = Duration.ofMinutes((long) (someTime * 60));
Duration someDuration2 = Duration.of((long) (someTime * 60), ChronoUnit.MINUTES);

// convert the duration back to Double
Double someTimeAgain = (double)someDuration.toMinutes() / 60;

关于java - 将 double 表示的小时数转换为持续时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57410559/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com