计算两个时间相隔的小时数

2024-10-09 11:10:43 浏览数 (1)

计算两个时间相隔的小时数

代码语言:javascript复制
import java.time.Duration;
import java.time.LocalDateTime;

public class Hello {
    public static void main(String[] args) {
        /**
         * now=2023-12-24T18:13:21.918
         * hours=-8
         */
        LocalDateTime now = LocalDateTime.now();
        System.out.println("now="   now);
        LocalDateTime dateTime = LocalDateTime.of(2023,12,24,10,0,0);
        long hours = Duration.between(now,dateTime).toHours();
        System.out.println("hours="   hours);

    }
}

0 人点赞