LocalDateTime currentTime = LocalDateTime.now();
LocalDateTime now = LocalDateTime.now(); int year = now.getYear(); // 2024 Month month = now.getMonth(); // MAY int dayOfMonth = now.getDayOfMonth(); // 20 int hour = now.getHour(); // 14 int minute = now.getMinute(); // 30
Date oldDate = new Date(); Instant instant = oldDate.toInstant(); LocalDateTime newTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
// 不推荐的写法 for (int i = 0; i < 1000; i++) {
LocalDateTime currentTime = LocalDateTime.now();
// 处理逻辑
}
// 推荐的写法 LocalDateTime batchTime = LocalDateTime.now(); for (int i = 0; i < 1000; i++) {
// 使用同一个时间实例
processData(batchTime);
}
你可能想看: