package J20250808.localDateTime; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; /** * @author WanJl * @version 1.0 * @title Demo03_LocalDateTimeTest * @description * @create 2025/8/8 */ public class Demo03_LocalDateTimeTest { /** * */ public static String dateTimeToString(LocalDateTime localDateTime,String format) { DateTimeFormatter df = DateTimeFormatter.ofPattern(format); String s = localDateTime.format(df); return s; } public static LocalDateTime stringToDateTime(String s,String format) { DateTimeFormatter df = DateTimeFormatter.ofPattern(format); LocalDateTime localDateTime = LocalDateTime.parse(s, df); return localDateTime; } public static void main(String[] args) { String s = "2025年05月05日 11:13:15"; String format="yyyy年MM月dd日 HH:mm:ss"; String timeToString = dateTimeToString(LocalDateTime.now(), format); System.out.println(timeToString); LocalDateTime dateTime = stringToDateTime(s, format); System.out.println(dateTime); } }