1.DateTimeFormatter
final修饰,线程安全,用于打印和解析日期-时间对象的格式化程序。
创建DateTimeFormatter:
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.systemDefault());
2.格式化日期
例如:LocalDateTime
LocalDateTime localDateTime = LocalDateTime.now(); System.out.println(dateTimeFormatter.format(localDateTime));
输出:2019-12-28 23:06:12
3.解析日期
(1)使用 LocalDateTime.parse
例如:解析 "2019-12-28 23:06:12"
LocalDateTime localDateTime2 = LocalDateTime.parse("2019-12-28 23:06:12", dateTimeFormatter); System.out.println("localDateTime2:"+dateTimeFormatter.format(localDateTime2));
输出:localDateTime2:2019-12-28 23:06:12
(2)DateTimeFormatter.parse
例如:解析 "2019-12-28 23:06:12"
LocalDateTime localDateTime3 = LocalDateTime.from(dateTimeFormatter.parse("2019-12-28 23:06:12")); System.out.println("localDateTime2:"+dateTimeFormatter.format(localDateTime3));
输出:localDateTime3:2019-12-28 23:06:12
4.常用符号含义
原文地址:https://www.cnblogs.com/xkzhangsanx/p/12113489.html
时间: 2024-11-29 13:38:41