自动日期/时间解析器,无需指定格式 [英] Automatic Date/Time parser without specifying format

查看:254
本文介绍了自动日期/时间解析器,无需指定格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在搜索一个可以将字符串解析成POJO而不指定格式的java库。我已经研究了 POjava 。有没有其他的图书馆做类似的事情?

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");

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

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.

更新:

我说Joda不能解决我的目的,Joda希望以ISO8601格式或您指定的任何格式(yyyyMMdd)解析该字符串。我将无法对此格式进行硬编码,因为我需要处理多种格式。

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.

我有一个解决方案,以消除美国或欧洲日期格式的歧义,即mm / dd / yy或dd / mm / yy。假设我可以访问日期的时区,我可以确定它是美国还是欧洲格式?有人可以告诉我这样做吗?谷歌,但没有发现。

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.

推荐答案

我找到了我的问题的答案。我使用了这个特定的图书馆 POjava 。此页面说明如何格式化日期+时间字符串,而不指定任何格式。但是,为了使图书馆能正常工作,您必须指定日期顺序,如Day,然后是月份或月份,然后是Day。

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.

这篇关于自动日期/时间解析器,无需指定格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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