我正在使用 LocalDateTime
来表示时间。
在 Debug模式下,我注意到时间格式不是 ISO 8601,这让我很难直接发送到其他 api。
请求类:
public class AgreementRequest {
@NonNull
private String name;
@NonNull
private String description;
@JsonProperty("start_date")
@DateTimeFormat(iso = DATE_TIME)
@NonNull
private String startDate;
}
初始化:
request = AgreementRequest.builder()
.name("Premium subscription")
.description("Montly subscription")
.startDate(LocalDateTime.now().plusDays(1).toString()).build();
LocalDateTime.toString
也不在 ISO 8601 中,ISO8601 中的字母 z
没有了。
我禁用了 objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
,这是问题所在吗?
最佳答案
The
LocalDateTime.toString
is not either in ISO 8601, the letterZ
in ISO8601 is gone.
LocalDateTime
日期的描述,如用于生日,结合挂钟上看到的本地时间。如果没有偏移量或时区等附加信息,它不能代表时间线上的一个瞬间。
Z
是零 UTC 偏移的区域指示符,这就是为什么在调用 toString()
时看不到它的原因来自LocalDateTime
实例。使用 ZonedDateTime
或 OffsetDateTime
如果您需要时区或偏移量。
序列化(反)JSR-310类型与 jackson ,考虑 JavaTimeModule
.查看documentation了解详情。
Should I use
Instant
instead ofLocalDateTime
? If so, what should I change in my PostgreSQL DB?
检查什么是PostgreSQL JDBC驱动documentation说:
The PostgreSQL™ JDBC driver implements native support for the Java 8 Date and Time API (JSR-310) using JDBC 4.2.
PostgreSQL™ | Java SE 8 --------------------------------+---------------- DATE | LocalDate TIME [ WITHOUT TIMEZONE ] | LocalTime TIMESTAMP [ WITHOUT TIMEZONE ] | LocalDateTime TIMESTAMP WITH TIMEZONE | OffsetDateTime
[...] Note that
ZonedDateTime
,Instant
andOffsetTime
/TIME [ WITHOUT TIMEZONE ]
are not supported. Also note that allOffsetDateTime
will instances will have be in UTC (have offset 0).This is because the backend stores them as UTC.
关于java - 默认情况下 LocalDateTime 不是 ISO8601,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52169272/