//导包
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
//Date 类型的时间使用SimpleDateFormat
Date date = new Date();
SimpleDateFormat sdf = new SimleDateFormat("yyyy-MM-dd HH:mm:ss");
String format = sdf.format(date);
System.out.println(format);
//LocalDateTime格式化使用DateTimeFormatter
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format1 = now.format(formatter);
System.out.println(format1);`
Date类型与LocalDateTime格式化为String
最新推荐文章于 2024-06-11 15:08:19 发布