开发者

How do I annotate a JAXB property to use xsd:time rather than xsd:datetime?

I have a JAXB class like this:

public class Game {
    private Date startTime;

    @XmlElement
    public Date getStartTime() {
        return startTime;
    }

    public void setSta开发者_StackOverflow社区rtTime(Date startTime) {
        this.startTime = startTime;
    }
}

which results in an .xsd where startTime has type xsd:datetime. I want it to be xsd:time. xsd:time maps to XmlGregorianCalendar, but the reverse mapping maps to xsd:anySimpleType which isn't very helpful.

I've tried various arguments to @XmlElement(type=...) to no avail. Any pointers would be greatly appreciated.

If it makes a difference, this is a type used by JAX-WS.


If you are generating the schema from the Java classes here is what you should change:

public class Game {
    private XMLGregorianCalendar startTime;

    @XmlElement
    @XmlSchemaType(name = "time")
    public XMLGregorianCalendar getStartTimeForSchema() {
      return startTime;
    }

    public void setStartTimeForSchema(XMLGregorianCalendar startTime) {
      this.startTime = startTime;
    }

    @XmlTransient
    public Date getStartTime() {
      return startTime.toGregorianCalendar().getTime();
    }

    @XmlTransient
    public void setStartTime(Date startTime) {
    GregorianCalendar gc = (GregorianCalendar) GregorianCalendar.getInstance();
      gc.setTime(startTime);
      DatatypeFactory dataTypeFactory = null;
      try {
        dataTypeFactory = DatatypeFactory.newInstance();
      } catch (DatatypeConfigurationException ex) {
        // log
      }
      this.startTime = dataTypeFactory.newXMLGregorianCalendar(gc);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜