当前位置:首页 > Java API 与类库手册 > 正文

Java优学网LocalDateTime入门解析:从基础创建到高效使用技巧

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);

}

Java优学网LocalDateTime入门解析:从基础创建到高效使用技巧

你可能想看:

相关文章:

文章已关闭评论!