
Java
Javaimport Java.time.ZoneId;import Java.time.ZonedDateTime;import Java.time.format.DateTimeFormatter;import Java.util.Date;public class TimeDifference {public static void mAIn(String[] args) {// Set start and end datesDate startDate = new Date("2022-01-01", 0, 0);Date endDate = new Date("2022-01-04, 23:59:59");// Use ZonedDateTime to get UTC datesZonedDateTime startTime = ZonedDateTime.of(startDate, ZoneId.systemDefault());ZonedDateTime endTime = ZonedDateTime.of(endDate, ZoneId.systemDefault());// Use DateTimeFormatter to format datesDateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");// Get start and end UTC hour valuesint startTimeHour = startTime.getHour();int endTimeHour = endTime.getHour();// Calculate time difference in hoursint timeDifference = endTimeHour - startTimeHour;System.out.println("The time difference between the start and end dates is " + timeDifference + " hours.");}}以上代码将输出起始时间和终止时间之间的小时数差异。请注意,上述代码中使用的是Java 8的编译版本,并且假设起始日期和终止日期在2022年1月1日和2022年1月4日之间。如果需要更准确的结果,请确保将输入参数转换为UTC日期和时间格式,并考虑时区差异可能对结果产生的影响。Copyright © 2025 IZhiDa.com All Rights Reserved.
知答 版权所有 粤ICP备2023042255号