我正在上这样的课

@XmlRootElement(name = "executionStats")
public class ScriptExecutionStatistics {

private DateTime start;
private DateTime end;

public ScriptExecutionStatistics() { }

public ScriptExecutionStatistics(DateTime start, DateTime end) {
    this.start = start;
    this.end = end;
}

@XmlAttribute
public DateTime getStart() {
    return start;
}

public void setStart(DateTime start) {
    this.start = start;
}

@XmlAttribute
public DateTime getEnd() {
    return end;
}

public void setEnd(DateTime end) {
    this.end = end;
}

}

我想在xml中序列化它,所以它看起来像这样
<scriptExecutionStatistics start="17.08.2015 18:17:00" end="17.08.2015 18:18:00" />

我知道,如果我想将joda time与jaxb绑定,则需要一个适配器,但是这让我感到困惑,我真的没有。所以我开始
public class DateTimeAdapter extends XmlAdapter<ScriptExecutionStatistics, DateTime>{
private static DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");

@Override
public DateTime unmarshal(ScriptExecutionStatistics vt) throws Exception {
    throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public ScriptExecutionStatistics marshal(DateTime bt) throws Exception {
    ScriptExecutionStatistics stats = new ScriptExecutionStatistics();

}

}

您能帮我完成它吗?
提前致谢

最佳答案

DateTimeAdapter应该将DateTimeString编组/解编,例如

public class DateTimeAdapter extends XmlAdapter<String, DateTime>{
   private static DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");

   @Override
   public DateTime unmarshal(String vt) throws Exception {
       return dtf.parseDateTime(vt);
   }

   @Override
   public String marshal(DateTime bt) throws Exception {
       return dtf.print(bt);

   }
}

另外,您还应该在ScriptExecutionStatistics中使用@XmlJavaTypeAdapter注释来注释适当的字段/获取器
@XmlJavaTypeAdapter(DateTimeAdapter.class)
@XmlAttribute
public DateTime getStart() {
    return start;
}

10-08 06:49