//准备一个LocalDateTime
LocalDateTime now = LocalDateTime.now();
//创建一个日期格式化器对象出来
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyy年MM月dd日 HH:mm:ss");
//格式化时间打印
String format = formatter.format(now);
System.out.println(format);
//把日期字符串,转为时间对象
String strStr ="2023年03月24日 15:03:21";
LocalDateTime ldt = LocalDateTime.parse(strStr,formatter);
System.out.println(ldt);
}
}
此代码片段展示了如何在Java中使用LocalDateTime进行日期时间操作。首先获取当前时间,然后用DateTimeFormatter格式化为字符串。接着,将一个格式化的日期时间字符串解析回LocalDateTime对象。
1206

被折叠的 条评论
为什么被折叠?



