首页  >  文章  >  Java  >  如何计算两个 Joda-Time DateTime 之间的时间差(以分钟为单位)?

如何计算两个 Joda-Time DateTime 之间的时间差(以分钟为单位)?

Barbara Streisand
Barbara Streisand原创
2024-11-02 14:50:03334浏览

How do you calculate the time difference between two Joda-Time DateTimes in minutes?

Joda-Time DateTimes:计算以分钟为单位的时差

如何确定两个 Joda-Time DateTimes 之间的时差几分钟内?方法如下:

要计算两个 DateTimes 之间的时间差(以分钟为单位),请使用以下方法:

<code class="java">public class DateTimeDifferenceExample {

    public static void main(String[] args) {
        // Create two DateTimes
        DateTime datetime1 = new DateTime();
        DateTime datetime2 = datetime1.plusMinutes(30);

        // Calculate the difference using Duration
        Duration duration = new Duration(datetime1, datetime2);
        long minutesDifference = duration.getStandardMinutes();

        // Alternatively, you can use Minutes.minutesBetween() method:
        long minutesDifference2 = Minutes.minutesBetween(datetime1, datetime2).getMinutes();

        // Output the difference
        System.out.println("Minutes difference: " + minutesDifference);
        System.out.println("Minutes difference using Minutes.minutesBetween(): " + minutesDifference2);
    }
}</code>

输出:

Minutes difference: 30
Minutes difference using Minutes.minutesBetween(): 30

以上是如何计算两个 Joda-Time DateTime 之间的时间差(以分钟为单位)?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn