这是我的适配器类:
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();
}
}这是一个对象类,我想在其中存储日期:
@XmlAccessorType(XmlAccessType.FIELD)
public class Object {
@XmlJavaTypeAdapter(LocalDateTimeAdapter.class)
private LocalDateTime time;
public LocalDateTime getTime() {
return time;
}因为某种原因,我无法编译它。这表明问题出在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而xml部分:
<time type="dateTime">2000-01-01T19:45:00Z</time>我正在学习这的例子。
发布于 2015-04-03 00:33:51
您可能使用的是Java8中的LocalDateTime,这个类没有任何字符串构造函数。
在您所跟踪的示例中,LocalDateTime来自JodaTime。
所以,你可以这样做:
org.joda.time.LocalDateTime (您需要JodaTime依赖项)而不是java.time.LocalDateTime;unmarshal方法更改为如下所示:
@覆盖公共LocalDateTime解封送件(字符串v)引发异常{返回LocalDateTime.parse(v);}您可能需要通知日期时间格式,因为默认格式是2011-12-03T10:15:30的格式,可能如下:
@Override
public LocalDateTime unmarshal(String v) throws Exception {
return LocalDateTime.parse(v, DateTimeFormatter.ISO_INSTANT);
}此外,在java.time.LocalDateTime中,toString将输出下列ISO-8601格式之一:
https://stackoverflow.com/questions/29424551
复制相似问题