什么是此字符串格式的正确格式模式?

huangapple go评论75阅读模式
英文:

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)
} 

链接

英文:

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 as 2007-12-03T10:15:30+01:00[Europe/Paris]
    • ZonedDateTime.toString() promising “a String, such as 2007-12-03T10:15:30+01:00[Europe/Paris]
    • DateTimeFormatter with the built-in formatters as well as the pattern letters used in format pattern strings
    • DateTimeFormatter.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.

huangapple
  • 本文由 发表于 2020年4月4日 19:41:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/61027547.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定