使用 LocalDateTime 查询今天日期Date类型
由于 LocalDateTime 类型包含日期和时间信息,因此需要使用其 toLocalDate() 方法将其转换为 LocalDate 类型,最后使用 LocalDate 类型的 atStartOfDay() 方法转换为 Date 类型。以下是示例代码:
// 获取当前时间的 LocalDateTime 类型
LocalDateTime now = LocalDateTime.now();
// 转换为 LocalDate 类型
LocalDate today = now.toLocalDate();
// 转换为 Date 类型
Date date = Date.from(today.atStartOfDay(ZoneId.systemDefault()).toInstant());
请注意,上述代码使用了系统默认时区(ZoneId.systemDefault())。如果需要使用特定时区,请将其替换为相应的 ZoneId。
原文地址: https://www.cveoy.top/t/topic/bsSj 著作权归作者所有。请勿转载和采集!