自动日期/时间分析器,无需指定格式(Automatic Date/Time parser without specifying format)

系统教程 行业动态 更新时间:2024-06-14 16:57:18
自动日期/时间分析器,无需指定格式(Automatic Date/Time parser without specifying format)

我正在寻找一个java库,它可以在不指定格式的情况下将字符串解析为POJO。 我研究过POjava 。 有没有其他图书馆可以做类似的事情?

DateTime dateTime = DateTimeParser.parse("21/02/13");

//If unclear use the cultural information passed
DateTime dateTime = DateTimeParser.parse("01/02/13", new Locale("en-us"));

//Should also work with time zones
DateTime dateTime = DateTimeParser.parse("2011/12/13T14:15:16+01:00");
 

我发现以下链接具有相同的问题Java的智能日期/时间解析器 ,但不是非常有用的答案。 Joda和JChronic都不做我想要的。 纠正我,如果我错了。

更新:

我说Joda没有解决我的目的的原因是,Joda希望字符串以ISO8601格式或您指定的任何格式解析,如“yyyyMMdd”。 由于我需要处理多种格式,因此我无法对此格式进行硬编码。

为了消除与美国或欧洲日期格式有关的模糊性,例如mm / dd / yy或dd / mm / yy,我有一个解决方案。 假设我可以访问日期的时区,我可以确定它是美国还是欧洲格式? 有人能告诉我如何做到这一点吗? 谷歌搜索但没有找到任何东西。

I am searching for a java library that can parse a string into a POJO without specifying the format. I have researched POjava. Is there anyother library which does similar thing?

DateTime dateTime = DateTimeParser.parse("21/02/13");

//If unclear use the cultural information passed
DateTime dateTime = DateTimeParser.parse("01/02/13", new Locale("en-us"));

//Should also work with time zones
DateTime dateTime = DateTimeParser.parse("2011/12/13T14:15:16+01:00");
 

I found the following links with the same problem Intelligent date / time parser for Java, but not very useful answers. Neither Joda or JChronic does what I wanted. Correct me if I am wrong.

Update:

The reason I say Joda does not solve my purpose is, Joda expects the string to be parsed in ISO8601 format or any format you specify like "yyyyMMdd". I will not be able to hardcode this format as I need to handle several formats.

I have a hack around solution for eliminating the ambiguity with respect to American or European date formats, i.e. mm/dd/yy or dd/mm/yy. Assuming I have access to timezone of the date, can I determine if it is American or European format? Can someone tell me way to do this? Googled but found nothing.

最满意答案

问题是有一些格式不能被猜到。

一个简单的例子是01/02/2013 。 这是2月1日还是1月2日? 甚至更糟糕的是: 01/02/09 ?

两种格式都存在。 (谢谢你,英国和美国!)

所以任何格式猜测者都不得不依赖于这些格式的运气,或故意为这些格式失败。

python模块dateutil.parser可以作为尽力而为解析器的示例。 对不起,我不知道一个Java等价物。 但你可能想看看乔达时间

http://labix.org/python-dateutil#head-b95ce2094d189a89f80f5ae52a05b4ab7b41af47

它实际上有参数dayfirst和yearfirst 。

然后有一个perl模块:

https://metacpan.org/pod/Time::ParseDate

您可能能够使用该模块的优先顺序列表。 盲目地尝试一些模式并不是很快(优化的词法分析器会更快),但它可能对你来说足够好,除非你猜测数百万条记录的格式。

I found the answer to my problem. I used this particular library POjava. This page explains how you can format the date+time string without specifying any format. However, for the library to work properly, you got to specify the date ordering like Day followed by Month or Month followed by Day.

更多推荐

本文发布于:2023-04-12 20:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/54ab913486e2cc3e6c56cdfa71a2438d.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:分析器   日期   格式   时间   Automatic

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!