博客
关于我
java日期格式化DateTimeFormatter
阅读量:782 次
发布时间:2019-03-24

本文共 1980 字,大约阅读时间需要 6 分钟。

前言

使用旧的
Date对象时,常用
SimpleDateFormat进行格式化显示。而使用新的
LocalDateTime
ZonedLocalDateTime时,格式化显示需要使用
DateTimeFormatter。与
SimpleDateFormat不同的是,
DateTimeFormatter不仅不是单一对象,还具备线程安全特性。这意味着在使用
DateTimeFormatter时,只需创建一个实例即可全局使用,而无需在方法内部频繁创建新对象。尽管如此,关于线程安全的知识点我们会在后续内容中详细探讨。

1. 年份

G:公元表示。
u
y
Y:年份表示。
y
Y的区别在于
Y用于不带年份的表示方式,而
y则表示当前年份。

2. 月份

M:当前月份的数字表示。
L:表示当前年份的第几月份。

3. 星期几

E:星期几表示(例如:星期日)。
F:表示当前月份已经过了多少个完整的星期。
W:表示当前年份已经过了多少个完整的星期。
w:表示当前年份从某一年开始已经过了多少个完整的星期。

4. 天数

D:表示已过的天数。
d:表示当前的日期。

5. 12小时计数

a:上午或下午的表示。
h:12小时制的小时数,默认逢0变为12。
K:12小时制的小时数,逢12变为0,范围是0-11。

6. 24小时计数

H:24小时制的小时数,范围是0-23。
k:24小时制的小时数,逢0变为24,范围是1-24。

7. 分钟

m:表示当前小时已经过了多少分钟。

8. 秒

s:表示当前分钟已经过了多少秒。

9. 毫秒

A:表示当前秒已经过了多少毫秒。

10. 纳秒

n:表示当前秒已经过了多少纳秒。
N:表示当前日期已经过了多少纳秒(每天过失去24小时,所以一天等于
N=24*60*60*1e9=86,400,000,000纳秒)。

示例代码测试

LocalDateTime提供了对日期时间进行操作的便捷方法,可以直接获取当前时间或指定时间的
LocalDateTime实例。以下是一些常用的示例代码:
LocalDateTime now = LocalDateTime.now();  // 获取当前执行时刻  System.out.println("当前时间:" + now);  // 时间格式化示例  System.out.println("用格式化标记输出:");  // 年份示例  System.out.println(now.format(DateTimeFormatter.ofPattern("Y")));  // 年份表示(不带年前缀)  System.out.println(now.format(DateTimeFormatter.ofPattern("y")));  // 月份示例  System.out.println(now.format(DateTimeFormatter.ofPattern("M")));  // 星期示例  System.out.println(now.format(DateTimeFormatter.ofPattern("E")));  System.out.println(now.format(DateTimeFormatter.ofPattern("a")));  // 12小时制示例  System.out.println(now.format(DateTimeFormatter.ofPattern("h")));  // 24小时制示例  System.out.println(now.format(DateTimeFormatter.ofPattern("H")));  // 分钟和秒示例  System.out.println(now.format(DateTimeFormatter.ofPattern("m")));  System.out.println("指定时间的例子:");  LocalDateTime now1 = LocalDateTime.of(2020, 1, 1, 23, 59, 59);  now1 = now1.plusSeconds(2);  System.out.println("2020-01-01 23:59:59 加2秒后变为" + now1);

通过这些代码示例,可以看出DateTimeFormatter对日期时间格式化的强大灵活性。与传统的SimpleDateFormat相比,它不仅更安全,而且支持更高级的格式化需求,如时间区支持和线程安全等。 如果需要改造旧项目中的日期时间显示逻辑,可以考虑将所有SimpleDateFormat实例替换为DateTimeFormatter,以提升代码的线程安全性和整体可维护性。

转载地址:http://iivkk.baihongyu.com/

你可能感兴趣的文章
tableviewcell 中使用autolayout自适应高度
查看>>
Symbolic Aggregate approXimation(SAX,符号聚合近似)介绍-ChatGPT4o作答
查看>>
Orcale表被锁
查看>>
svn访问报错500
查看>>
sum(a.YYSR) over (partition by a.hy_dm) 不需要像group by那样需要分组函数。方便。
查看>>
ORCHARD 是什么?
查看>>
Struts2中使用Session的两种方法
查看>>
Stream API:filter、map和flatMap 的用法
查看>>
STM32工作笔记0032---编写跑马灯实验---寄存器版本
查看>>
Static--用法介绍
查看>>
ssm旅游信息管理系统的设计与实现bus56(程序+开题)
查看>>
order by rand()
查看>>
SSM(Spring+SpringMvc+Mybatis)整合开发笔记
查看>>
ViewHolder的改进写法
查看>>
Orderer节点启动报错解决方案:Not bootstrapping because of 3 existing channels
查看>>
org.apache.axis2.AxisFault: org.apache.axis2.databinding.ADBException: Unexpected subelement profile
查看>>
sql查询中 查询字段数据类型 int 与 String 出现问题
查看>>
org.apache.commons.beanutils.BasicDynaBean cannot be cast to ...
查看>>
org.apache.dubbo.common.serialize.SerializationException: com.alibaba.fastjson2.JSONException: not s
查看>>
sqlserver学习笔记(三)—— 为数据库添加新的用户
查看>>