用于时区偏移的java.time DateTimeFormatter模式 [英] java.time DateTimeFormatter pattern for timezone offset
问题描述
我正在尝试解析: 2014-05-02-10.45.05.993280-5:00
其中 -5:00
是UTC的偏移量。使用java.time Java 8中的DateTimeFormatter
。
I am trying to parse: 2014-05-02-10.45.05.993280-5:00
where the -5:00
is the offset from UTC. Using a java.time DateTimeFormatter
in Java 8.
对于第一位,我有以下内容: yyyy-MM- dd-HH.mm.ss.SSSSSS
但是,我无法弄清楚解析偏移量的模式应该是什么。
For the first bit I have the following: yyyy-MM-dd-HH.mm.ss.SSSSSS
however, I can't figure out what the pattern should be to parse the offset also.
如果我有4位数的偏移量(-05:00),我可以使用: yyyy-MM-dd-HH.mm.ss.SSSSSSxxx
,但这不是工作3位数。
If I had the offset with 4 digits (-05:00) I could use: yyyy-MM-dd-HH.mm.ss.SSSSSSxxx
, but this doesn't work for 3 digits.
任何想法?
推荐答案
使用资金字母X代替x,因此XXX。区别在于大X可以将输入字母Z识别为UTC-Offset +00:00而小字母字母X不能识别。
Use capital letter X instead of x, hence XXX. The difference is that big X can recognize the input letter "Z" as UTC-Offset +00:00 while small pattern letter X cannot.
建议模式:
yyyy-MM-dd-HH.mm.ss.SSSSSSXXX
请注意以下 JDK-bug :
java.time.format.DateTimeFormatter无法使用单个$ b解析偏移量$ b数字小时
java.time.format.DateTimeFormatter cannot parse an offset with single digit hour
更新:
我现在已经在错误日志中测试了所描述的解决方法。
I have now tested the described workaround in the bug-log.
String input = "2014-05-02-10.45.05.993280-5:00";
DateTimeFormatter f = new DateTimeFormatterBuilder().appendPattern("yyyy-MM-dd-HH.mm.ss.SSSSSS").parseLenient().appendOffset("+HH:MM", "Z").toFormatter();
System.out.println(f.parse(input, ZonedDateTime::from));
但它会引发异常:
线程main中的异常java.time.format.DateTimeParseException:
Text'2014-05-02-10.45.05.993280-5:00'不能在java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1947)的索引26
处解析
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1849)
在HelloWorld.main(HelloWorld.java:16)
Exception in thread "main" java.time.format.DateTimeParseException: Text '2014-05-02-10.45.05.993280-5:00' could not be parsed at index 26 at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1947) at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1849) at HelloWorld.main(HelloWorld.java:16)
因此,宽松的解析也无济于事。 所以现在只剩下三个选项:
So lenient parsing does not help either. So there are now only three options left for you:
-
使用bug报告器建议的解决方法: [...]解决方法是分别解析日期/时间,使用手动编码解析器进行偏移,并将LocalDateTime与手解析偏移量相结合。不是一件容易的事。
Use workaround suggested by bug reporter: [...] workaround is to parse the date/time separately, use a hand coded parser for the offset and combine the LocalDateTime with the hand parsed offset. Not an easy work around.
尝试自己专门的字符串预处理。如果你有一个固定的格式,那么你可以尝试在第26位插入零位数(如果总输入长度是一个数字太小)。
Try your own specialized string preprocessing. If you have a fixed format then you can try to insert the zero-digit at position 26 (if the total input length is one digit too small).
或者您使用可以执行此操作的外部库。如果您愿意添加额外的依赖项,我的库Time4J(v4.0)可以这样做。请参阅此代码:
Or you use an external library which can do this. My library Time4J (v4.0) can do that if you are willing to add an extra dependency. See this code:
String input =2014-05-02-10.45.05.993280 -5:00\" ;
ZonalDateTime zdt =
ZonalDateTime.parse(
输入,
Moment.localFormatter(yyyy-MM-dd-HH.mm.ss.SSSSSSXXX,PatternType.CLDR)) ;
System.out.println(zdt); // 2014-05-02T10:45:05,993280UTC-05:00
ZonedDateTime result = zdt.toTemporalAccessor();
更新:根据JDK-bug-status,已经修复了Java-9的bug,但是Java-8的后端似乎不可用。
Update: According to JDK-bug-status, the bug has been fixed for Java-9, but a backport for Java-8 does not seem to be available though.
这篇关于用于时区偏移的java.time DateTimeFormatter模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!