How to create a date object in Java?
In Java, you can create date objects using either the java.util.Date class or the java.time.LocalDate class.
The way to create a date object using the java.util.Date class is as follows:
import java.util.Date;
// 创建当前日期对象
Date currentDate = new Date();
// 创建指定日期对象(年份是从1900年开始计算,月份从0开始计算)
Date specificDate = new Date(120, 0, 1); // 表示2020年1月1日
The way to create a date object using the java.time.LocalDate class is as follows:
import java.time.LocalDate;
// 创建当前日期对象
LocalDate currentDate = LocalDate.now();
// 创建指定日期对象
LocalDate specificDate = LocalDate.of(2020, 1, 1); // 表示2020年1月1日
Please note that the java.util.Date class is the old date class in Java, while the java.time.LocalDate class is the new date class in Java 8 and later versions, it is recommended to use the new date class.