import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class GetDateExample {
public static void main(String[] args) {
// 获取当前日期
LocalDate currentDate = LocalDate.now();
System.out.println("当前日期: " + currentDate);
// 格式化日期输出
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String formattedDate = currentDate.format(formatter);
System.out.println("格式化后的日期: " + formattedDate);
// 获取特定日期
LocalDate specificDate = LocalDate.of(2023, 10, 5);
System.out.println("特定日期: " + specificDate);
}
}
LocalDate.now()
方法可以获取当前的日期。DateTimeFormatter
类可以将日期格式化为指定的字符串格式。这里使用了 yyyy-MM-dd
的格式。LocalDate.of(year, month, day)
方法可以创建一个特定的日期对象。如果你需要更多的日期操作,可以参考 Java 的 java.time
包,它提供了丰富的日期和时间处理功能。
上一篇:java 枚举定义
下一篇:java date获取年份
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站