gpt4 book ai didi

java - DateTimeFormatter 创建模式

转载 作者:行者123 更新时间:2023-12-02 09:43:53 28 4
gpt4 key购买 nike

我有这个日期:2008-01-10T11:00:00-05:00 (日期、T(分隔符)、时间、偏移量)

我有这个:DateTimeFormatter.ofPattern("yyyy-MM-ddSEPARATORHH-mm-ssOFFSET")

我用这个table创建我的模式。

但是,我找不到如何注释SEPARATOR (T) 和偏移。

对于 OFFSET 存在这样的: x zone-offset offset-x +0000; -08; -0830; -08:30; -083015; -08:30:15; ,但不知道如何使用x获取-08:30

最佳答案

这里有一个小例子,展示了如何解析您的字符串并接收偏移量:

public static void main(String[] args) {
// this is what you have, a datetime String with an offset
String dateTime = "2008-01-10T11:00:00-05:00";
// create a temporal object that considers offsets in time
OffsetDateTime offsetDateTime = OffsetDateTime.parse(dateTime);
// just print them in two different formattings
System.out.println(offsetDateTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));
System.out.println(offsetDateTime.format(DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss")));
// get the offset from the object
ZoneOffset zonedOffset = offsetDateTime.getOffset();
// get its display name (a String representation)
String zoneOffsetString = zonedOffset.getDisplayName(TextStyle.FULL_STANDALONE, Locale.getDefault());
// and print the result
System.out.println("The offset you want to get is " + zoneOffsetString);
}

Please pay attention to the code comments, they explain what is done. Printing the OffsetDateTime two times in the middle of the code is just done in order to show how you can deal with a single datetime object along with different formatters.

关于java - DateTimeFormatter 创建模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56834389/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com