gpt4 book ai didi

java - Joda time 计算两个日期时间对象之间的天、小时、分钟和秒

转载 作者:行者123 更新时间:2023-12-01 06:49:48 25 4
gpt4 key购买 nike

我正在尝试查找两个日期/时间之间的天、小时、分钟和秒。我以为我可以使用句点,我尝试了下面的代码,但我得到了一个无意义的答案。

    DateTime dt1 = new DateTime("2004-12-13T21:39:45.618-06:00");
DateTime dt2 = new DateTime("2004-12-13T21:39:45.618-08:00");
Period p = new Period(dt1, dt2);

System.out.println("Test: " + p);

从中我得到输出:

I/System.out: Test: PT2H

不确定这是什么意思。

感谢您的帮助

最佳答案

toString() Period中的方法给你的值为 ISO8601持续时间格式。

来自 API:

Gets the value as a String in the style of the ISO8601 duration format. Technically, the output can breach the ISO specification as weeks may be included. For example, "PT6H3M5S" represents 6 hours, 3 minutes, 5 seconds.

当您要求分别获取小时分钟时,您可以使用方便的从 API 获取 方法:

import org.joda.time.DateTime;
import org.joda.time.Period;

...
DateTime dt1 = new DateTime("2004-12-13T21:39:45.618-06:00");
DateTime dt2 = new DateTime("2004-12-13T21:39:45.618-08:00");
Period p = new Period(dt1, dt2);

System.out.println("DAYS: " + p.getDays())
System.out.println("HOURS: " + p.getHours());
System.out.println("MINUTES: " + p.getMinutes());
System.out.println("SECONDS: " + p.getSeconds());

或者按照其他答案建议使用 PeriodFormatter .

关于java - Joda time 计算两个日期时间对象之间的天、小时、分钟和秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39490450/

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