java取db时间差12小时

253 2024-03-10 07:18

import java.time.Duration; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; public class TimeDifferenceExample { public static void main(String[] args) { LocalDateTime dateTime1 = LocalDateTime.parse("2022-01-01T12:00:00"); LocalDateTime dateTime2 = LocalDateTime.parse("2022-01-02T00:00:00"); Instant instant1 = dateTime1.atZone(ZoneId.systemDefault()).toInstant(); Instant instant2 = dateTime2.atZone(ZoneId.systemDefault()).toInstant(); Duration duration = Duration.between(instant1, instant2); long diffInHours = duration.toHours(); if (diffInHours == 12) { System.out.println("时间差为12小时"); } } }
顶一下
(0)
0%
踩一下
(0)
0%
相关评论
我要评论
点击我更换图片