当前位置: 首页 > news >正文

JDK17日期格式‘MMM’导致九月Sept/Sep格式化异常问题❗❗❗

🔍 起因


JDK-8256837

As discussed in daily call , we are using the ‘YYYY-MM-DD’ as the date format , so no impact for us and it will format the year-month-date to be ‘2025-09-02’. This JDK month format issue especially happened in some HSBCnet applications because they more like to use ‘MMM’ as the month format .

  • "A known bug in JDK 17, tracked as JDK-8281302, changed the short name for September from "Sep" to "Sept" when using Locale.UK for date formatting and parsing. This change specifically affects September and can cause parsing errors when dealing with historical data or external sources that still use "Sep".
  • This issue is present in JDK versions 16, 17, 18, and 19. While other months retain their three-letter abbreviations, September uniquely adopted a four-letter abbreviation in this specific locale setting.
  • To address this, particularly when parsing dates from external sources or legacy systems, a common workaround involves performing a string replacement, such as replacing "Sep" with "Sept" before attempting to parse the date string with SimpleDateFormat or other date/time APIs."

******  整理 by 

Moshow郑锴@https://zhengkai.blog.csdn.net/

在 JDK17 中,SimpleDateFormat 使用 "MMM" 格式时,如果 Locale 是 Locale.UK,它只接受 "Sept" 而不是 "Sep",这确实是一个令人困惑的变化。

🔍 为什么必须是 "Sept" 而不是 "Sep"

这是因为:

  • SimpleDateFormat"MMM" 格式依赖于 Locale 的月份缩写资源

  • 在 JDK17 的 Locale.UK 中,九月的缩写是 "Sept",而不是 "Sep"

  • 所以当你传入 "Sep" 时,它无法匹配 Locale.UK 的预期格式,从而抛出 ParseException

这在 JDK8 中可能没有问题,因为早期版本对缩写更宽容,或者 Locale.UK 的资源不同。

✅ 解决方案

1. ✅ 改为使用 Locale.ENGLISH
DateFormat df = new SimpleDateFormat("yyyy MMM dd", Locale.ENGLISH);
df.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = df.parse("2015 Sep 30"); // ✅ 正常解析
  • Locale.ENGLISH 的九月缩写是 "Sep",与输入匹配。

  • 这是最直接的解决方法。

2. ✅ 或者修改输入字符串为 "Sept"
DateFormat df = new SimpleDateFormat("yyyy MMM dd", Locale.UK);
df.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = df.parse("2015 Sept 30"); // ✅ 正常解析
  • 如果你坚持使用 Locale.UK,那就必须使用 "Sept"直接.replace("Sep","Sept")

🧠 延伸建议:避免 SimpleDateFormat 的坑

考虑使用 Java 8 引入的 DateTimeFormatter

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MMM dd", Locale.ENGLISH);
LocalDate date = LocalDate.parse("2015 Sep 30", formatter);
  • 更安全、线程安全、行为一致。

  • 不容易被 Locale 的细节坑到。

如果你是在处理用户输入或日志解析,建议统一 Locale 或做预处理,比如把 "Sep" 替换成 "Sept",或者直接切换到 Locale.ENGLISH

******  整理 by Moshow郑锴@https://zhengkai.blog.csdn.net/


文章转载自:

http://O5P3TfNl.bdfph.cn
http://qyrxk2ZA.bdfph.cn
http://wvzBfzro.bdfph.cn
http://8vrKgkaW.bdfph.cn
http://SL0NTRB0.bdfph.cn
http://u4yO2jdL.bdfph.cn
http://nXTsUR8q.bdfph.cn
http://ITSYmYZs.bdfph.cn
http://qAsydY1c.bdfph.cn
http://RvHAW7jO.bdfph.cn
http://TlxYVWPu.bdfph.cn
http://2kjCogFt.bdfph.cn
http://Oht5LY54.bdfph.cn
http://YRxzi1Tw.bdfph.cn
http://wpPVoRhd.bdfph.cn
http://HPeGZNOX.bdfph.cn
http://BUtz096H.bdfph.cn
http://zluR2Pzj.bdfph.cn
http://PYPmyZfI.bdfph.cn
http://HcR0THPv.bdfph.cn
http://Sio5wMIa.bdfph.cn
http://y6kZILSI.bdfph.cn
http://ICwfpHY4.bdfph.cn
http://oTK5o0LG.bdfph.cn
http://V4W18BZq.bdfph.cn
http://mXAqXVaS.bdfph.cn
http://aMVOYAvn.bdfph.cn
http://gZpqdtKA.bdfph.cn
http://0P0OQv9m.bdfph.cn
http://9mn5aJVr.bdfph.cn
http://www.dtcms.com/a/371938.html

相关文章:

  • Vulkan 学习(20)---- UniformBuffer 的使用
  • 微信小程序中实现AI对话、生成3D图像并使用xr-frame演示
  • 【不背八股】9.MySQL知识点汇总
  • MySQL6
  • 论文阅读:ICLR 2021 BAG OF TRICKS FOR ADVERSARIAL TRAINING
  • GD32自学笔记:4.ADC
  • LeetCode 522.最长特殊序列2
  • CentOS 7.2 虚机 ssh 登录报错在重启后无法进入系统
  • 腾讯混元 3D 2.0 Windows 便携版:低显存需求下的高效文/图生3D体验
  • 火山 RTC 引擎15 拉流 推流 地址生成器 、合流转推 开关
  • CesiumJS详解:打造专业级Web 3D地球仪与地图的JavaScript库
  • 数据结构:顺序表与链表
  • C++ 前缀和 高频笔试考点 实用技巧 牛客 DP34 [模板] 前缀和 题解 每日一题
  • kotlin - 平板分屏,左右拖动,2个Activity计算宽度,使用ActivityOptions、Rect(三)
  • 【软考架构】第七章 系统架构设计基础知识-7.2基于架构的软件开发方法:Architecture-Based Software Design,ABSD
  • Dify 从入门到精通(第 81/100 篇):Dify 的多模态模型监控(高级篇)
  • 2019年11月系统架构设计师真题及解析摘要
  • 基于Django的“社区爱心养老管理系统”设计与开发(源码+数据库+文档+PPT)
  • IO性能篇(二):文件读写的四种分类
  • 超越模仿,探寻智能的本源:从人类认知机制到下一代自然语言处理
  • 计算机视觉(十二):人工智能、机器学习与深度学习
  • 去中心化投票系统开发教程 第五章:测试与部署
  • 自然语言处理之第一课语言转换方法
  • 移动端代理配置:iOS和Android设备代理设置完全指南
  • 【面试向】区块链介绍
  • 第十四届蓝桥杯青少组C++选拔赛[2023.2.12]第二部分编程题(4、最大空白区)
  • keycloak redirect_url重定向配置
  • Archon01-项目部署
  • 基于Python的餐厅推荐系统【2026最新】
  • OpenManus项目安装与使用教程详解