可以使用Java 8的时间API中的Duration类来比较两个日期相差多少秒。示例代码如下:

import java.time.Duration;
import java.time.LocalDateTime;

public class DateComparisonExample {

    public static void main(String[] args) {
        LocalDateTime date1 = LocalDateTime.of(2021, 9, 1, 10, 30, 0);
        LocalDateTime date2 = LocalDateTime.of(2021, 9, 1, 10, 30, 30);

        Duration duration = Duration.between(date1, date2);
        long seconds = duration.getSeconds();

        System.out.println("相差 " + seconds + " 秒");
    }

}

输出结果为:

相差 30 秒
``
java比较两个日期相差多少秒

原文地址: https://www.cveoy.top/t/topic/cGac 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录