This is my adapter class:
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;
}
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
and the xml part:
<time type="dateTime">2000-01-01T19:45:00Z</time>
I'm following this example.
Probably you're using LocalDateTime
from Java 8. This class has not any constructor for string.
In the example which you're following LocalDateTime
is from JodaTime
.
So, you can do this in to ways:
Import
org.joda.time.LocalDateTime
(you will need JodaTime dependency) instead ofjava.time.LocalDateTime
;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);
}
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
来源:https://stackoverflow.com/questions/29424551/java-unmarshall-localdatetime-error