2024-07-11
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
Java 8 has made comprehensive improvements to time processing, redesigning all date, time, calendar, and time zone related APIs, and placing them all in the java.time package and subpackages.
Java 5 shortcomings:
java.util.Date
It is not thread-safe, and you must handle multi-threaded concurrency issues yourself when using this class.java.util.Date
The default date is from1900
Start, the month from 1
Start from 0
In the beginning, there was no unity. And Date
The class also lacks methods for directly manipulating dates.Java 8 Improvements:
API
Is thread-safe not onlysetter
method, and any changes to the instance will return a new instance while ensuring that the original instance remains unchanged.LocalDate: Used to represent a date without a time zone, for example: 2024-07-06.
import java.time.LocalDate;
import java.time.Month;
public class LocalDateExample {
public static void main(String[] args) {
// 获取当前日期
LocalDate today = LocalDate.now();
System.out.println("当前日期: " today);
// 创建指定日期
LocalDate specificDate = LocalDate.of(2024, Month.JULY, 6);
System.out.println("指定日期: " specificDate);
// 日期操作示例
LocalDate tomorrow = today.plusDays(1);
System.out.println("明天的日期: " tomorrow);
}
}
// 输出
当前日期: 2024-07-06
指定日期: 2024-07-06
明天的日期: 2024-07-07