SimpleDateFormat 中的可选部分
我正在读取可能有或没有时区调整的日期字符串:yyyyMMddHHmmssz
或 yyyyMMddHHmmss
。当字符串缺少时区时,我会将其视为 GMT。我没有看到任何在 SimpleDateFormat
中创建可选部分的方法,但也许我错过了一些东西。有没有办法使用 SimpleDateFormat
来做到这一点,或者我应该编写一个新的具体 DateFormat
来处理这个问题?
I'm reading in date strings that could be with or without a time zone adjustment: yyyyMMddHHmmssz
or yyyyMMddHHmmss
. When a string is missing a zone, I'll treat it as GMT. I'm not seeing any way to create optional sections in a SimpleDateFormat
, but maybe I'm missing something. Is there a way to do this with a SimpleDateFormat
, or should I just write a new concrete DateFormat
to handle this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您可以使用 Joda Datetime,它支持格式化程序中的可选部分,例如“yyyy-MM-dd [hh:mm:ss]”
If you can use Joda Datetime it supports optional parts in formatter, for example, "yyyy-MM-dd [hh:mm:ss]"
我将使用
try-catch
操作循环遍历潜在的DateFormat
对象列表,以在第一次成功解析时打破循环。I would loop over the list of potential
DateFormat
objects using atry-catch
operation to break the loop on the first successful parse.您可以创建两个不同的
SimpleDateFormats
就像You can create two different
SimpleDateFormats
like我前段时间通过扩展 SimpleDateFormat 解决了类似的问题。下面是一个粗略的实现,展示了我的解决方案的想法。它可能不完全完整/优化。
要点是,您需要检查输入字符串并以某种方式“猜测”TZ 字段是否丢失,如果缺少则添加它,然后让
SimpleDateFormat#parse(String, ParsePosition)
完成其余的工作。上面的实现没有根据SimpleDateFormat#parse(String, ParsePosition)
中的 javadoc 中的约定更新 ParsePosition该类具有单个默认构造函数,因为只允许一种格式。
方法
MySimpleDateFormat#parse(String, ParsePosition)
由SimpleDateFormat#parse(String)
调用,因此足以涵盖这两种情况。运行 main() 这是输出(如预期)
I have solved a similar problem some time ago by extending SimpleDateFormat. Below an crude implementation to show the idea of my solution. It may not be fully complete/optimised.
The gist is that you need to check the input string and "guess" somehow that the TZ field is missing, add it if so and then let the
SimpleDateFormat#parse(String, ParsePosition)
do the rest. The implementation above isn't updating ParsePosition according to the contract in javadoc inSimpleDateFormat#parse(String, ParsePosition)
The class has a single default ctor as there's only one format allowed.
The method
MySimpleDateFormat#parse(String, ParsePosition)
is invoked bySimpleDateFormat#parse(String)
so it's sufficient to cover both cases.Running the main() this is the output (as expected)
JSR-310 随 Java 8 一起提供,它提供了对解析时间值的增强支持,其中组件现在可以是可选的。您不仅可以使区域可选,还可以使时间组件可选并返回给定字符串的正确时间单位。
考虑以下测试用例。
这里的 DateTimeFormatter 模式
"yyyy-MM-dd[[ ]['T']HH:mm[:ss][XXX]]"
允许方括号内的选项,也可以嵌套。模式也可以从 DateTimeFormatterBuilder,上面的模式在这里进行了演示:这将转换为如下所示的表达式:
可选值可以嵌套,并且如果仍然打开,也会在末尾自动关闭。但请注意,无法在可选部分上提供异或,因此上述格式实际上可以很好地解析以下值:
请注意,我们可以重用现有格式化程序作为当前格式的一部分,这一功能非常巧妙。
JSR-310 has been delivered with Java 8 which provides enhanced support for parsing temporal values where components may now be optional. Not only can you make the zone optional, but you can also make the time component optional and return the correct temporal unit for the given string.
Consider the following test cases.
Here the DateTimeFormatter pattern of
"yyyy-MM-dd[[ ]['T']HH:mm[:ss][XXX]]"
allows optionals within the square parentheses which can also be nested. Patterns can also be constructed from a DateTimeFormatterBuilder, which the above pattern is demonstrated here:This would translate to an expression which looks like the following:
Optional values can be nested and are also auto closed at the end if still open. Note however that there is no way to provide an exclusive OR on optional parts, thus the above format would actually parse the following value quite fine:
Note the really neat capability that we can reuse existing formatter's as parts of our current format.
我知道这是一篇旧文章,但只是为了记录...
Apache DateUtils 类可以帮助您。
链接到 Maven 存储库中的 Apache Commons Lang 库,您可能想检查最新版本是什么:
http://mvnrepository.com/artifact/org.apache.commons/commons-lang3
v3.4
摇篮 v3.4
I know this is an old post but just for the record...
Apache DateUtils class can help you with that.
Link to the Apache Commons Lang library in Maven repository, you might want to check what's the latest version:
http://mvnrepository.com/artifact/org.apache.commons/commons-lang3
Maven v3.4
Gradle v3.4
我将创建两种 SimpleDateFormat,一种带有时区,一种没有时区。您可以查看字符串的长度来确定使用哪一个。
听起来您需要一个 DateFormat 来委托两个不同的 SDF。
I would create two SimpleDateFormat, one with a time zone and one without. You can look at the length of the String to determine which one to use.
Sounds like you need a DateFormat which delegates to two different SDF.