英文:
What is the correct formatting pattern for this String format?
问题
我尝试使用threeten将一个字符串转换为ZonedDateTime时出现了DateTimeParseException。我不确定这个字符串格式的正确格式化模式是什么?2014-04-16T00:00+02:00[Europe/Berlin]。有人能告诉我正确的模式是什么吗?
另外顺便提一下:有没有某个页面或资源可以让我查找这样的信息,而不必自己重新构建呢?
谢谢!
英文:
I am getting a DateTimeParseException when trying to convert a String to a ZonedDateTime with threeten. I am not sure what the correct formatting pattern is for this String format?
2014-04-16T00:00+02:00[Europe/Berlin]. Can someone tell me how the correct pattern is?
On a sitenote: Is there some page or some resource somewhere where I can look such things up without having to reconstruct it by myself?
Thanks!
答案1
得分: 3
不需要格式化器: 您的格式是 ZonedDateTime
的默认格式。ZonedDateTime
在默认情况下会解析和打印此格式,也就是说,无需任何显式的格式化器。
String s = "2014-04-16T00:00+02:00[Europe/Berlin]";
ZonedDateTime zdt = ZonedDateTime.parse(s);
System.out.println("Parsed into " + zdt);
输出:
Parsed into 2014-04-16T00:00+02:00[Europe/Berlin]
这个格式扩展了 ISO 8601 格式。ISO 8601 格式仅为 2014-04-16T00:00+02:00
,包括 UTC 偏移量但不包括时区。java.time
的开发人员将其扩展以包含时区 ID。
如果您需要一个格式化器: 如果您有特殊原因需要一个格式化器,也许您需要将其传递给一个方法,或者您只是希望明确指定您期望的格式,那么内置了一个:DateTimeFormatter.ISO_ZONED_DATE_TIME
。因此,您仍然不需要编写任何格式模式字符串。
在哪里找到这些信息? 这些信息可以在 java.time
类的文档中找到。请参阅下面的文档链接。
您自己的代码: 感谢您在此答案下的评论中提供您自己的代码。对于其他读者,我在这里重复一下,以便阅读。
fun parseZonedDateTimeToString(date: ZonedDateTime): String {
return DateTimeFormatter.ISO_ZONED_DATE_TIME.format(date)
}
fun parseStringToZonedDateTime(dateString: String): ZonedDateTime {
return ZonedDateTime.parse(dateString, DateTimeFormatter.ISO_ZONED_DATE_TIME)
}
链接
- Wikipedia 文章:ISO 8601
- 文档链接:
- 使用一个参数的
ZonedDateTime.parse()
,指定“例如2007-12-03T10:15:30+01:00[Europe/Paris]
”的文本字符串 ZonedDateTime.toString()
承诺返回“一个字符串,例如2007-12-03T10:15:30+01:00[Europe/Paris]
”DateTimeFormatter
包括内置的格式化器以及格式化模式字符串中使用的模式字母DateTimeFormatter.ISO_ZONED_DATE_TIME
,“类似 ISO 的日期时间格式化器,格式化或解析带有偏移和时区的日期时间,例如 '2011-12-03T10:15:30+01:00[Europe/Paris]'。”- 由于上面的链接是针对 Java 10 版本的类文档,与后移版本的文档并不总是相同,所以这里是 ThreeTen backport 1.4.2 API 的文档,在其中您也将找到上述所有项目。
- 使用一个参数的
英文:
No formatter needed: Your format is the default format for a ZonedDateTime
. ZonedDateTime
both parses and prints this format as its default, that is, without any explicit formatter.
String s = "2014-04-16T00:00+02:00[Europe/Berlin]";
ZonedDateTime zdt = ZonedDateTime.parse(s);
System.out.println("Parsed into " + zdt);
Output:
> Parsed into 2014-04-16T00:00+02:00[Europe/Berlin]
The format is extended from ISO 8601 format. ISO 8601 would be 2014-04-16T00:00+02:00
only, so includes the UTC offset but not the time zone. The developers of java.time extended it to include the time zone ID.
If you want a formatter: If you have a special reason for wanting a formatter, maybe you need to pass one to a method or you just wish to make it explicit which format you expect, one is built in: DateTimeFormatter.ISO_ZONED_DATE_TIME
. So you still don’t need to write any format pattern string.
Where to find this information? It’s in the documentation of the classes of java.time. See the documentation links below.
Your own code: Thank you for providing your own code in the comment under this answer. For other readers I am repeating it here, formatted for readability.
fun parseZonedDateTimeToString(date: ZonedDateTime): String {
return DateTimeFormatter.ISO_ZONED_DATE_TIME.format(date)
}
fun parseStringToZonedDateTime(dateString: String): ZonedDateTime {
return ZonedDateTime.parse(dateString, DateTimeFormatter.ISO_ZONED_DATE_TIME)
}
Links
- Wikipedia article: ISO 8601
- Documentation links:
- The one-arg
ZonedDateTime.parse()
specifying “a text string such as2007-12-03T10:15:30+01:00[Europe/Paris]
” ZonedDateTime.toString()
promising “aString
, such as2007-12-03T10:15:30+01:00[Europe/Paris]
”DateTimeFormatter
with the built-in formatters as well as the pattern letters used in format pattern stringsDateTimeFormatter.ISO_ZONED_DATE_TIME
, “The ISO-like date-time formatter that formats or parses a date-time with offset and zone, such as '2011-12-03T10:15:30+01:00[Europe/Paris]'.”- Since the links above are to the documentation of the Java 10 versions of the classes, which is not always identical to the documentation of the backport, here is the documentation of ThreeTen backport 1.4.2 API, under which you will find all of the above items too.
- The one-arg
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论