使用 java.text.SimpleDateFormat 解析具有可能的单位数月/日/小时的日期

发布于 2024-09-27 15:53:46 字数 826 浏览 0 评论 0原文

在使用 Talend Open Studio(一种开源代码生成 ETL 工具)的项目中,我在解析传入日期字符串时遇到错误,例如“3/14/1967 0:00:00”(注意单位数月份< /em>)。

深入研究 代码,我可以看到它正在使用 java.text.SimpleDateFormat。因此,我期望需要使用的日期模式字符串是 "dM-yyyy H:mm:ss"...但它一直给我错误,如“无法解析的日期:3/14/1967 0: 00:00”。

我假设 SimpleDateFormat可以处理单位或两位数问题。 我需要不同的日期模式吗?(当然,我可以在 Java 尝试将字符串读取为日期之前进行预处理来调整值,但这不是必需的!)

On a project with Talend Open Studio (an Open Source code-generating ETL tool), I am getting errors parsing incoming date strings like "3/14/1967 0:00:00" (note the single-digit month).

Digging into the code, I can see it is using java.text.SimpleDateFormat. So the date pattern string I expect I need to use is "d-M-yyyy H:mm:ss"... but it keeps giving me errors like "Unparseable date: 3/14/1967 0:00:00".

I assume that SimpleDateFormat can deal with the single-or-double digit problem. Do I need a different date pattern? (Sure, I could do pre-processing to tweak the values before Java attempts to read the strings as Dates, but it shouldn't be necessary!)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

唠甜嗑 2024-10-04 15:53:46

您确定您实际上需要使用dM-yyyy而不是M/d/yyyy吗?我明白为什么“3/14/1967”会有问题...您预计那是哪一天?请注意,您不仅将“d”和“M”弄反了,而且还指定了“-”而不是“/”作为分隔符。

Are you sure you actually need to use d-M-yyyy rather than M/d/yyyy? I can see why it would have a problem with "3/14/1967"... what date did you expect that to be? Note that not only have you got "d" and "M" the wrong way round, but you've also specified "-" instead of "/" as the separator.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文