|NO.Z.00041|——————————|BigDataEnd|——|Java&核心类库.V11|——|Java.v11|DateTimeFormatter类|概念使用|
一、[DateTimeFormatter类的概念和使用]
### --- DateTimeFormatter类的概述
~~~ # 基本概念
~~~ ——> java.time.format.DateTimeFormatter类主要用于格式化和解析日期。
二、常用的方法
方法声明 | 功能介绍 |
static DateTimeFormatter ofPattern(String pattern) | 根据参数指定的模式来获取对象 |
String format(TemporalAccessor temporal) | 将参数指定日期时间转换为字符串 |
TemporalAccessor parse(CharSequence text) | 将参数指定字符串转换为日期时间 |
三、编程代码
package com.yanqi.task13;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
public class DateTimeFormatterTest {
public static void main(String[] args) {
// 1.获取当前系统的日期时间并打印
LocalDateTime now = LocalDateTime.now();
System.out.println("now = " + now);
// 2.按照指定的格式准备一个DateTimeFormatter类型的对象
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// 3.实现日期时间向字符串类型的转换并打印
String str = dateTimeFormatter.format(now);
System.out.println("调整格式后的结果是:" + str);
// 4.实现字符串类型到日期时间类型的转换并打印
TemporalAccessor parse = dateTimeFormatter.parse(str);
System.out.println("转回去的结果是:" + parse);
}
}
四、编译打印
D:\JAVA\jdk-11.0.2\bin\java.exe "-javaagent:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=53691:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath E:\NO.Z.10000——javaproject\NO.H.00001.javase\javase\out\production\javase com.yanqi.task13.DateTimeFormatterTest
now = 2021-07-30T20:04:14.817453700
调整格式后的结果是:2021-07-30 20:04:14
转回去的结果是:{},ISO resolved to 2021-07-30T20:04:14
Process finished with exit code 0
Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
——W.S.Landor
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通