import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class DateExample {
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);
// 解析字符串为日期
String dateStr = "2023-10-05";
LocalDate parsedDate = LocalDate.parse(dateStr, formatter);
System.out.println("解析后的日期: " + parsedDate);
}
}
LocalDate.now()
方法获取当前的日期。DateTimeFormatter
类指定日期的格式,并通过 format()
方法将日期对象转换为指定格式的字符串。LocalDate.parse()
方法将字符串解析为 LocalDate
对象,解析时需要提供与字符串格式匹配的 DateTimeFormatter
。上一篇:java date 获取前一天
下一篇:java的反射机制
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站