paf*_*de2 1 java datetime timezone-offset zoneddatetime
我想将字符串转换为 OffsetDateTime 数据类型。字符串具有以下形状:
2017-11-27T19:06:03
Run Code Online (Sandbox Code Playgroud)
我尝试了两种方法:
方法一
public static OffsetDateTime convertString(String timestamp) {
java.time.format.DateTimeFormatter formatter = new java.time.format.DateTimeFormatterBuilder()
.parseCaseInsensitive()
.append(java.time.format.DateTimeFormatter.ISO_LOCAL_DATE)
.appendLiteral('T')
.appendValue(HOUR_OF_DAY, 2)
.appendLiteral(':')
.appendValue(MINUTE_OF_HOUR, 2)
.optionalStart()
.appendLiteral(':')
.appendValue(SECOND_OF_MINUTE, 2)
.toFormatter();
return OffsetDateTime.parse(timestamp, formatter);
}
Run Code Online (Sandbox Code Playgroud)
方法二:
public static OffsetDateTime convertString(String timestamp) {
java.time.format.DateTimeFormatter parser = java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
java.time.LocalDateTime dt = java.time.LocalDateTime.parse(timestamp, parser);
ZonedDateTime zdt = ZonedDateTime.of(dt, java.time.ZoneId.of("UTC"));
return OffsetDateTime.from(zdt);
}
Run Code Online (Sandbox Code Playgroud)
第一种方法不起作用,因为它抱怨以下内容:
java.time.format.DateTimeParseException:无法解析文本“2017-11-27T19:02:42”:无法从 TemporalAccessor 获取 OffsetDateTime:{},ISO 解析为 java 类型的 2017-11-27T19:02:42 .time.format.Parsed
据我所知,它来自字符串没有 ZoneId 的事实。如何在格式化程序上覆盖 ZoneId 以忽略它?
第二种方法来自这个问题并且有效,但它需要 2 个额外的转换,我想避免这些额外的转换。
任何帮助将不胜感激。
要从OffsetDateTime没有偏移量或时区的字符串中获取,您需要确定偏移量或获取偏移量的方法。最明显的方法是,如果您知道要在哪个时区解释日期时间。例如:
OffsetDateTime dateTime = LocalDateTime.parse(timestamp)
.atZone(ZoneId.of("Asia/Chongqing"))
.toOffsetDateTime();
System.out.println(dateTime);
Run Code Online (Sandbox Code Playgroud)
使用您问题中的字符串输出:
2017-11-27T19:06:03+08:00
不要害怕从LocalDateTimetoZonedDateTime和 to的两个转换OffsetDateTime。随着java.time它们不仅容易做,但也清晰易读。
如果您知道偏移量,只需使用它,那么您只需要一次转换(我在示例中使用了一个无意义的偏移量,因此您必须选择自己的):
OffsetDateTime dateTime = LocalDateTime.parse(timestamp)
.atOffset(ZoneOffset.ofTotalSeconds(-36953));
System.out.println(dateTime);
Run Code Online (Sandbox Code Playgroud)
输出:
2017-11-27T19:06:03-10:15:53
您可以指定所需的偏移量,例如ZoneOffset.of("+08:00")或ZoneOffset.ofHours(8)。
但是要回答你的问题(现在是时候了)。
如何在格式化程序上覆盖 ZoneId 以忽略它?
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
.parseDefaulting(ChronoField.OFFSET_SECONDS, -36953)
.toFormatter();
OffsetDateTime dateTime = OffsetDateTime.parse(timestamp, formatter);
System.out.println(dateTime);
Run Code Online (Sandbox Code Playgroud)
输出与前一个相同(再次,您需要选择一个对您的情况有意义的偏移量)。