LocalDate是Java 8中提供的日期类,它表示不带时区和时间的日期,可以用于表示生日、纪念日、节假日等。以下是使用LocalDate的示例代码:
import java.time.LocalDate;
public class Example { public static void main(String[] args) { // 获取当前日期 LocalDate today = LocalDate.now(); System.out.println("今天的日期是:" + today);
// 创建指定日期
LocalDate date = LocalDate.of(2022, 10, 1);
System.out.println("指定日期是:" + date);
// 获取日期中的年、月、日
int year = today.getYear();
int month = today.getMonthValue();
int day = today.getDayOfMonth();
System.out.println("今天的日期是:" + year + "年" + month + "月" + day + "日");
}
}
在上面的代码中,我们首先获取当前日期并打印出来,然后创建了一个指定日期的对象,并打印出来。最后,我们使用getYear()、getMonthValue()和getDayOfMonth()方法获取了日期中的年、月、日,并打印出来。