DateTimeFormatter使用方法
时间: 2023-11-16 20:57:59 浏览: 43
DateTimeFormatter是Java 8中的一个日期时间格式化类,用于将日期时间对象转换为字符串或将字符串转换为日期时间对象。使用方法如下:
1. 创建DateTimeFormatter对象:
a. 通过ofPattern方法传入格式化字符串创建DateTimeFormatter对象,例如:DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
b. 通过ofLocalizedDateTime方法传入FormatStyle和Locale创建DateTimeFormatter对象,例如:DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM).withLocale(Locale.CHINA);
2. 将日期时间对象转换为字符串:
a. 调用format方法,传入日期时间对象,返回字符串,例如:String str = formatter.format(LocalDateTime.now());
3. 将字符串转换为日期时间对象:
a. 调用parse方法,传入字符串和DateTimeFormatter对象,返回日期时间对象,例如:LocalDateTime dateTime = LocalDateTime.parse("2022-01-01 00:00:00", formatter);
相关问题
DateTimeFormatter 方法使用
DateTimeFormatter 是 Java 8 中引入的一个日期时间格式化工具类,用于将日期时间对象格式化为字符串或者将字符串解析为日期时间对象。下面是 DateTimeFormatter 的常用方法:
1. ofPattern(String pattern):使用指定的模式创建一个 DateTimeFormatter 对象。
2. format(TemporalAccessor temporal):将 TemporalAccessor 对象(例如 LocalDate、LocalDateTime、ZonedDateTime 等)格式化为字符串。
3. parse(CharSequence text):将字符串解析为 TemporalAccessor 对象。
4. withLocale(Locale locale):指定地区进行格式化或解析。
5. withZone(ZoneId zone):指定时区进行格式化或解析。
下面是一个简单的示例代码:
```
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formatDateTime = now.format(formatter);
System.out.println("格式化后的时间:" + formatDateTime);
String dateTimeStr = "2022-06-08 10:10:10";
LocalDateTime dateTime = LocalDateTime.parse(dateTimeStr, formatter);
System.out.println("解析后的时间:" + dateTime);
```
DateTimeFormatter的使用方法
DateTimeFormatter 可以用于日期时间的格式化和解析。使用方法如下:
1. 创建 DateTimeFormatter 对象。可以使用 DateTimeFormatter.ofPattern(String pattern) 方法,传入一个格式化模式字符串作为参数,来创建 DateTimeFormatter 对象。例如:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
这个 formatter 可以用于格式化和解析一个字符串,使其符合 "年-月-日 时:分:秒" 的格式。
2. 格式化日期时间。可以使用 formatter.format(LocalDateTime dateTime) 方法,将一个 LocalDateTime 对象按照 formatter 中定义的格式进行格式化,例如:
LocalDateTime dateTime = LocalDateTime.now();
String formattedDateTime = formatter.format(dateTime);
这个 formattedDateTime 变量的值就是当前时间按照 "年-月-日 时:分:秒" 的格式格式化后的结果。
3. 解析字符串为日期时间。可以使用 formatter.parse(CharSequence text) 方法,将一个符合 formatter 中定义的格式的字符串解析为 LocalDateTime 对象,例如:
String text = "2021-07-22 10:30:00";
LocalDateTime dateTime = LocalDateTime.parse(text, formatter);
这个 dateTime 变量就是解析后的 LocalDateTime 对象。
注意,DateTimeFormatter 中的格式化符号和普通的日期格式化字符串有所不同,具体可参考 JDK 文档。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)