java unmarshall LocalDateTime错误 [英] java unmarshall LocalDateTime error
问题描述
这是我的适配器类:
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();
}
}
这是一个对象类,我想存储日期:
and this is an object-class where i want to store the date:
@XmlAccessorType(XmlAccessType.FIELD)
public class Object {
@XmlJavaTypeAdapter(LocalDateTimeAdapter.class)
private LocalDateTime time;
public LocalDateTime getTime() {
return time;
}
由于某种原因,我无法编译它。它显示问题出在返回new LocalDateTime(v);
。这是我得到的错误:
For some reason, i can't compile it. It shows that the problem is at return new LocalDateTime(v);
. And this is the error I get:
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>
我正在关注这个示例。
推荐答案
可能你正在使用 <$来自Java 8的c $ c> LocalDateTime 。该类没有任何构造函数用于字符串。
Probably you're using LocalDateTime
from Java 8. This class has not any constructor for string.
在你的例子中重温 LocalDateTime
来自 JodaTime
。
In the example which you're following LocalDateTime
is from JodaTime
.
所以,你可以这样做:
-
导入
org.joda.time.LocalDateTime
(您将需要JodaTime依赖项)而不是java.time.LocalDateTime
;
Import
org.joda.time.LocalDateTime
(you will need JodaTime dependency) instead ofjava.time.LocalDateTime
;
或将 unmarshal
方法改为某些人像这样:
or change unmarshal
method to something like this:
@Override
public LocalDateTime unmarshal(String v) throws Exception {
return LocalDateTime.parse(v);
}
您可能需要通知日期时间格式,因为默认格式为 2011-12-03T10:15:30
,也许这样:
You may need to inform the date time format, as the default is a format to 2011-12-03T10:15:30
, maybe this:
@Override
public LocalDateTime unmarshal(String v) throws Exception {
return LocalDateTime.parse(v, DateTimeFormatter.ISO_INSTANT);
}
此外,在 java.time.LocalDateTime
toString
将输出以下ISO-8601格式之一:
Also, in java.time.LocalDateTime
toString
will output one of the following ISO-8601 formats:
- uuuu-MM -dd'T'HH:mm
- uuuu-MM-dd'T'HH:mm:ss
- uuuu-MM-dd'T 'HH:mm:ss.SSS
- uuuu-MM-dd'T'HH:mm:ss.SSSSSS
- uuuu-MM-dd 'T'HH:mm:ss.SSSSSSSSS
- uuuu-MM-dd'T'HH:mm
- uuuu-MM-dd'T'HH:mm:ss
- uuuu-MM-dd'T'HH:mm:ss.SSS
- uuuu-MM-dd'T'HH:mm:ss.SSSSSS
- uuuu-MM-dd'T'HH:mm:ss.SSSSSSSSS
这篇关于java unmarshall LocalDateTime错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!