首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >jaxb解组时间戳

jaxb解组时间戳
EN

Stack Overflow用户
提问于 2010-03-26 05:13:23
回答 4查看 63.2K关注 0票数 59

我无法让JAXB在Resteasy JAX-RS服务器应用程序中解组时间戳。

我的类看起来像这样:

@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name = "foo")
public final class Foo {
    // Other fields omitted

    @XmlElement(name = "timestamp", required = true)
    protected Date timestamp;

    public Foo() {}

    public Date getTimestamp() {
        return timestamp;
    }

    public String getTimestampAsString() {
        return (timestamp != null) ? new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(timestamp) : null;
    }

    public void setTimestamp(final Date timestamp) {
        this.timestamp = timestamp;
    }

    public void setTimestamp(final String timestampAsString) {
        try {
            this.timestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(timestampAsString);
        } catch (ParseException ex) {
            this.timestamp = null;
        }
    }
}

有什么想法吗?

谢谢。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2010-07-06 04:23:36

JAXB可以处理java.util.Date类。然而,它期望的格式是:

"yyyy-MM-dd'T'HH:mm:ss“而不是"yyyy-MM-dd HH:mm:ss”

如果您想要使用日期格式,我建议您使用XmlAdapter,它将如下所示:

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class DateAdapter extends XmlAdapter<String, Date> {

    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public String marshal(Date v) throws Exception {
        return dateFormat.format(v);
    }

    @Override
    public Date unmarshal(String v) throws Exception {
        return dateFormat.parse(v);
    }

}

然后,您可以在时间戳属性上指定此适配器:

import java.util.Date;

import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlAccessorType(XmlAccessType.NONE) 
@XmlRootElement(name = "foo") 
public final class Foo { 
    // Other fields omitted 

    @XmlElement(name = "timestamp", required = true) 
    @XmlJavaTypeAdapter(DateAdapter.class)
    protected Date timestamp; 

    public Foo() {} 

    public Date getTimestamp() { 
        return timestamp; 
    } 

    public void setTimestamp(final Date timestamp) { 
        this.timestamp = timestamp; 
    } 

}
票数 107
EN

Stack Overflow用户

发布于 2010-03-26 05:16:38

JAXB不能直接编组Date对象,因为它们没有足够的信息来明确表示。JAXB为此引入了XmlGregorianCalendar类,但是直接使用它是非常不愉快的。

我建议将您的timestamp字段更改为XmlGregorianCalendar,并在可能的情况下更改各种方法以更新此字段,同时保留已有的公共接口。

如果希望保留XML域,则需要实现自己的XmlAdapter类来告诉Date如何将Date转换为XML。

票数 11
EN

Stack Overflow用户

发布于 2014-04-30 16:22:39

为了让XML marshaller生成格式化为YYYY-MM-DD的xsd: XmlAdapter,而不定义日期,我使用此方法构建了javax.xml.datatype.XMLGregorianCalendar的实例:

public XMLGregorianCalendar buildXmlDate(Date date) throws DatatypeConfigurationException {
    return date==null ? null : DatatypeFactory.newInstance().newXMLGregorianCalendar(new SimpleDateFormat("yyyy-MM-dd").format(date));
}

根据结果,我初始化了JAXB编译器(在Eclipse中)生成的类的XMLGregorianCalendar字段:

  Date now = new Date();
  ...
  report.setMYDATE(buildXmlDateTime(now));
  ...
  JAXBContext context = JAXBContext.newInstance(ReportType.class);
  Marshaller m = context.createMarshaller();
  m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
  m.marshal(new ObjectFactory().createREPORT(report), writer);

并获得预期格式的标签:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<REPORT>
   ...
   <MY_DATE>2014-04-30</MY_DATE>
   ...
</REPORT>
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2519432

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档