我正在添加这个问题,因为我是Java和Android的新手,我搜索了几个小时试图解决这个问题.答案来自相关答案的组合,所以我想我会记录我为其他可能正在努力的人学到的东西.见答案.
对于一些背景知识,我的经验主要是PHP的Web开发和一点Ruby.我唯一的操作系统是Linux(Ubuntu Studio),我(不情愿地)在Android Studio 2.1.2中开发我的第一个Android应用程序.我的Java设置如下:
>java -version
> openjdk version "1.8.0_91"
> OpenJDK Runtime Environment (build 1.8.0_91-8u91-b14-3ubuntu1~15.10.1-b14)
> OpenJDK 64-Bit Server VM (build 25.91-b14, mixed mode)
Run Code Online (Sandbox Code Playgroud) datetime android android-gradle-plugin threetenbp threetenabp
我们尝试使用时区偏移量解析以下ISO 8601 DateTime字符串:
final String input = "2022-03-17T23:00:00.000+0000";
OffsetDateTime.parse(input);
LocalDateTime.parse(input, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
Run Code Online (Sandbox Code Playgroud)
由于时区偏移中的冒号,两种方法都失败(这OffsetDateTime也是有意义的DateTimeFormatter.ISO_OFFSET_DATE_TIME).
java.time.format.DateTimeParseException:无法在索引23处解析文本'2022-03-17T23:00:00.000 + 0000'
但根据维基百科,时区偏移有4种有效格式:
<time>Z
<time>±hh:mm
<time>±hhmm
<time>±hh
Run Code Online (Sandbox Code Playgroud)
其他框架/语言可以解析这个字符串而没有任何问题,例如Javascript Date()或Jacksons ISO8601Utils(他们在这里讨论这个问题)
现在我们可以DateTimeFormatter使用复杂的RegEx 编写自己的,但在我看来,java.time库应该能够默认解析这个有效的ISO 8601字符串,因为它是有效的.
现在我们使用Jacksons ISO8601DateFormat,但我们更愿意使用官方date.time库来使用.你有什么方法可以解决这个问题?
我有以下测试。
@Test
void withoutColon_fails() {
ZonedDateTime.parse("2019-01-24T12:10:58.036820+0400");
}
@Test
void withColon_ok() {
ZonedDateTime.parse("2019-01-24T12:10:58.036820+04:00");
}
Run Code Online (Sandbox Code Playgroud)
我试图解析的日期时间之间的唯一区别是,在第一种情况下,时区指定的小时和分钟之间没有冒号,而在第二种情况下有一个冒号。
第一个失败:
java.time.format.DateTimeParseException: Text '2019-01-24T12:10:58.036820+0400' could not be parsed, unparsed text found at index 29
at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2049)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
at java.base/java.time.ZonedDateTime.parse(ZonedDateTime.java:598)
at java.base/java.time.ZonedDateTime.parse(ZonedDateTime.java:583)
Run Code Online (Sandbox Code Playgroud)
根据 javadoc, parse(), through DateTimeFormatter.ISO_ZONED_DATE_TIME, 最终使用DateTimeFormatter.ISO_OFFSET_DATE_TIME, 反过来,我们可以读到它是
ISO 日期时间格式化程序,用于格式化或解析具有偏移量的日期时间,例如“2011-12-03T10:15:30+01:00”。
进一步遵循 javadoc 链接,我们会看到ZoneOffset.getId()它表示它接受:
Z - for UTC (ISO-8601)
+hh:mm or -hh:mm - if the seconds are zero (ISO-8601)
+hh:mm:ss or -hh:mm:ss - if the seconds are non-zero (not ISO-8601) …Run Code Online (Sandbox Code Playgroud) 欣赏有很多类似的帖子,但我找不到具体的帮助.
我正在尝试将此字符串转换为Java中的Date
2017-05-16 06:24:36-0700
但每次使用此代码都会失败
Date Login = new SimpleDateFormat("dd/MM/yy HH:mm:ss").parse("2017-05-16 06:24:36-0700");
Run Code Online (Sandbox Code Playgroud)
现在我假设它是由于最后的时区信息 - 我只是无法弄清楚如何设置格式.我试过这个,但没有运气
SimpleDateFormat("dd/MM/yy HH:mm:ssZ")
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我需要将字符串 2020-04-15T23:00:00+0000 转换为 ZonedDateTime。我尝试了以下但他们没有工作:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss Z");
System.out.println(ZonedDateTime.parse("2020-04-15T23:00:00+0000", formatter));
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss 00:00");
System.out.println(ZonedDateTime.parse("2020-04-15T23:00:00+0000", formatter));
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss 0000");
System.out.println(ZonedDateTime.parse("2020-04-15T23:00:00+0000", formatter));
Run Code Online (Sandbox Code Playgroud)
我也使用了 ISO_LOCAL_DATE_TIME 但这没有用。有任何想法吗?
更新:
我在下面试过:
OffsetDateTime dateTime1 = OffsetDateTime.parse("2020-04-15T23:00:00+0000",
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ"));
System.out.println(dateTime1.toZonedDateTime());
Run Code Online (Sandbox Code Playgroud)
我得到的输出为 2020-04-15T23:00Z,但我需要它为 2020-04-15T23:00:00Z,最后的秒数正在修剪。