java - 使用注解将“yyyy-MM-dd'T'HH:mm:ss”解析为 Kotlin 中的 LocalDateTime
问题描述
我想LocalDateTime
使用注释将带有模板“yyyy-MM-dd'T'HH:mm:ss”的字符串解析为。
我试图解析下面的代码,但它抛出了一个错误。我应该怎么做才能修复这个错误?
代码:
SearchREQ(
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss")
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
var departureDate: LocalDateTime,
)
错误:
Type definition error: [simple type, class java.time.LocalDateTime];
nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException:
Cannot construct instance of java.time.LocalDateTime (no Creators,
like default construct, exist): no String-argument constructor/factory
method to deserialize from String value ('2018-08-20T06:00:00')
at [Source: (PushbackInputStream); line: 5, column: 22]
(through reference chain: gtd.connector.model.req.SearchREQ[\"departureDate\"])
感谢您的回答
解决方案
LocalDateTime
没有String
构造函数。一个可能的解决方法是:
class SearchREQ(
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss")
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
var departureDate: Date
) {
val departureDateTime get() = LocalDateTime.ofInstant(departureDate.toInstant(), TimeZone.getTimeZone("UTC").toZoneId())!!
}
您还可以为LocalDateTime
.
推荐阅读
- c++ - 使用 CRTP 创建特征矩阵
- c++ - 如何修复此错误“错误:没有匹配函数调用 'getline(std::ifstream&, st::_cx11::string&,
- node.js - 是否有解释如何设置应用程序环境的 Node.js 文档?
- react-native - 如何将函数传递给 React Native 中的其他组件?
- c# - OData 和 Cosmos DB
- c++ - C ++字符串文件重定向差异?
- swift - 如何从字典中删除所有 Int 值?
- swift - 表达式类型“@lvalue CGPoint”在没有更多上下文的情况下模棱两可
- javascript - 如何使用变量更改javascript中的样式?
- c - 代码似乎无法正确打印连接的字符串