通过提示用户日期格式来解析日期?

发布于 2025-01-08 04:24:33 字数 500 浏览 5 评论 0原文

我的应用程序读取用户提供的 csv 文件。此 csv 文件包含日期和/或时间字段,其格式为用户选择的任何方式。喜欢: 1999/10/28 或者 2000-01-01,3PM-02-59

我想为用户提供在此字段中指示日期和时间格式的可能性。所以,我有一个输入窗口,上面写着: “指定日期/时间字段的格式” “示例:对于 1999/10/28 格式,输入 yyyy/mm/dd” “其他示例:对于 2000-01-01,3PM-02-59,输入 yyyy-dd-mm,hh-mm-ss”

我会将其输入作为字符串检索,然后执行一些复杂且烦人的操作来派生常规该字符串的表达式,然后我将在 csv 解析器中应用该表达式来识别“日期和时间”字段中的年、月、日、小时等。

在开始之前我的问题:是否有一个图书馆已经做了这种事情?我是在重新发明轮子吗?我是否缺少更明显的方法?

多谢!

PS:是的,这都是关于用户提供自己奇怪的日期和时间格式的。无需建议,使用统一格式会更容易! :-)

My application reads a csv file provided by the user. This csv file contains a field for date and / or time, formatted in any way the user chose it. Like:
1999/10/28
or
2000-01-01,3PM-02-59

I want to offer the user the possibility to indicate the formatting of the date and time in this field. So, I'd have an input window saying:
"Specify the formatting of your date/time field"
"Example: for 1999/10/28 format, input yyyy/mm/dd"
"Other example: for 2000-01-01,3PM-02-59,input yyyy-dd-mm,hh-mm-ss"

I would retrieve their input as a string, then do some complicated and annoying operation to derive a regular expression from this string, which I would then apply in the csv parser to identify years, months, days, hours, etc. in the "date and time" field.

My question before embarking in this: is there a library which already does this kind of thing? Am I reinventing the wheel? Is there a much more obvious approach I'm missing?

Thanks a lot!

PS: yes, it is all about users providing their own strange date and time formats. No need to suggest that it would be easier with a a uniform format! :-)

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

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

发布评论

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

评论(2

谁把谁当真 2025-01-15 04:24:33

在 Joda Time 你可以使用多个解析器,你可以检查这个SO答案
使用 Joda 日期和时间Time API解析多种格式

in Joda Time you can use multiple parser, you can check this SO answer
Using Joda Date & Time API to parse multiple formats

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