import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class LocalDateToStringExample {
public static void main(String[] args) {
// 创建一个 LocalDate 对象,表示当前日期
LocalDate localDate = LocalDate.now();
// 定义日期格式
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
// 将 LocalDate 转换为 String
String dateAsString = localDate.format(formatter);
// 输出转换后的字符串
System.out.println("LocalDate 转换为 String: " + dateAsString);
}
}
LocalDate 对象:使用 LocalDate.now() 获取当前日期。DateTimeFormatter.ofPattern("yyyy-MM-dd") 指定日期的格式为 "年-月-日"。localDate.format(formatter) 方法将 LocalDate 对象转换为指定格式的字符串。System.out.println 输出转换后的字符串。下一篇:java对象转json字符串方法
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站