Java取消编组LocalDateTime [英] java unmarshall LocalDateTime
问题描述
这是我的适配器类:
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;
}
由于某种原因,我无法编译它.它表明问题出在 return 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>
我正在关注此示例.
推荐答案
Probably you're using LocalDateTime
from Java 8. This class has not any constructor for string.
在您关注的示例中, JodaTime
.
In the example which you're following LocalDateTime
is from JodaTime
.
因此,您可以通过以下方式做到这一点:
So, you can do this in to ways:
-
导入
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);
}
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
中
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
这篇关于Java取消编组LocalDateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!