Joda-Time 中的自由日期/时间解析
是否可以创建一个 DateTimeParser
来解析:
- 带有时间的日期 给定
- 没有时间的日期(假设时间是一天的开始)
- 没有日期的时间(假设日期是今天)
或者我需要吗有三个独立的解析器,并尝试用每个解析器解析字符串?
换句话说,是否可以在解析器中定义可选字段?
Is it possible to create a single DateTimeParser
which would parse:
- dates with time given
- dates without time (assuming time to be start of day)
- times without dates (assuming date to be today)
or do I need to have three separate parsers, and try parsing strings with each one?
In other words, is it possible to define optional fields in a parser?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
org.joda.time.format.ISODateTimeFormat
有一个静态方法dateOptionalTimeParser()
,它基于DateTimeFomatterBuilder
类使用下面的代码,该方法有一个appendOptional
方法 - 您可能能够根据您的要求调整该方法...链接:http://joda-time.sourceforge.net/apidocs/org /joda/time/format/DateTimeFormatterBuilder.htmlorg.joda.time.format.ISODateTimeFormat
has a static methoddateOptionalTimeParser()
that uses the code below, based on theDateTimeFomatterBuilder
class, which has anappendOptional
method - you would probably be able to adapt the method to your requirement... Link: http://joda-time.sourceforge.net/apidocs/org/joda/time/format/DateTimeFormatterBuilder.html