使用java.text.SimpleDateFormat解析可能的单位数月/日/小时 [英] Parse date with possible single-digit month/day/hour using java.text.SimpleDateFormat

查看:193
本文介绍了使用java.text.SimpleDateFormat解析可能的单位数月/日/小时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用Talend Open Studio(一个开源代码生成ETL工具)的项目中,我收到错误的解析传入日期字符串,如3/14/1967 0:00:00(数字月份)。

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).

挖掘到 代码,我可以看到它正在使用 java.text.SimpleDateFormat 。所以我期望我需要使用的日期模式字符串是dM-yyyy H:mm:ss ...但它不断给我错误,如不可抛弃的日期:3/14/1967 0: 00:00。

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".

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

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!)

推荐答案

你确定你实际上需要使用 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.

这篇关于使用java.text.SimpleDateFormat解析可能的单位数月/日/小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆