itV*_*ico 4 java xml datetime jaxb unmarshalling
这是我的适配器类:
public class LocalDateTimeAdapter extends XmlAdapter<String, LocalDateTime> {
@Override
public LocalDateTime unmarshal(String v) throws Exception {
return new LocalDateTime(v);
}
@Override
public String marshal(LocalDateTime v) throws Exception {
return v.toString();
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个我想存储日期的对象类:
@XmlAccessorType(XmlAccessType.FIELD)
public class Object {
@XmlJavaTypeAdapter(LocalDateTimeAdapter.class)
private LocalDateTime time;
public LocalDateTime getTime() {
return time;
}
Run Code Online (Sandbox Code Playgroud)
由于某种原因,我无法编译它。表明问题出在return new LocalDateTime(v);。这是我得到的错误:
Error:(9, 16) java: constructor LocalDateTime in class java.time.LocalDateTime cannot be applied to given types;
required: java.time.LocalDate,java.time.LocalTime
found: java.lang.String
reason: actual and formal argument lists differ in length
Run Code Online (Sandbox Code Playgroud)
和 xml 部分:
<time type="dateTime">2000-01-01T19:45:00Z</time>
Run Code Online (Sandbox Code Playgroud)
我正在遵循这个例子。
可能您使用的LocalDateTime是 Java 8。此类没有任何字符串构造函数。
在您关注的示例中,LocalDateTime来自JodaTime.
因此,您可以通过以下方式做到这一点:
导入org.joda.time.LocalDateTime(您将需要 JodaTime 依赖项)而不是java.time.LocalDateTime;
或将unmarshal方法更改为如下所示:
@Override
public LocalDateTime unmarshal(String v) throws Exception {
return LocalDateTime.parse(v);
}
Run Code Online (Sandbox Code Playgroud)您可能需要告知日期时间格式,因为默认格式是2011-12-03T10:15:30这样的:
@Override
public LocalDateTime unmarshal(String v) throws Exception {
return LocalDateTime.parse(v, DateTimeFormatter.ISO_INSTANT);
}
Run Code Online (Sandbox Code Playgroud)
此外,java.time.LocalDateTime toString将输出以下 ISO-8601 格式之一:
| 归档时间: |
|
| 查看次数: |
4940 次 |
| 最近记录: |