更新
总结:
- 您有一个在某处使用日期样式的架构,并且您无法更改该架构
 
- 你有一些XML数据使用该架构,并指定一些带有时区的日期(所以它的格式)
yyyy-MM-ddXXX 
- 您想要从该文件中的日期表示形式中删除该部分(日期本身不运送任何时区,日期只是一个数字)
XXX 
所以这可能是一个示例架构:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    <element name="foo">
        <complexType>
            <sequence>
                <element name="bar" type="date" minOccurs="1" maxOccurs="1"/>
            </sequence>
        </complexType>
    </element>
</schema>
这可能是一个示例数据:
<?xml version="1.0" encoding="UTF-8"?>
<foo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="foo.xsd">
    <bar>2014-01-01+06:00</bar>
</foo>
这是 JAXB 注释的类
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Foo implements Serializable
{
    private static final long serialVersionUID = 1L;
    @XmlElement(name = "bar")
    @XmlJavaTypeAdapter(DateAdapter.class)
    @XmlSchemaType(name = "date")
    private Date bar;
    // getters/setters
}
这是日期适配器
public class DateAdapter extends XmlAdapter<String, Date>
{
    @Override
    public String marshal(Date date)
    {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        df.setTimeZone(TimeZone.getTimeZone("GMT"));
        return df.format(date);
    }
    @Override
    public Date unmarshal(String date) throws ParseException
    {
        DateFormat df = new SimpleDateFormat("yyyy-MM-ddXXX");
        return df.parse(date);
    }
}
这是主要的,针对架构进行验证:
public static void main(String[] args) throws JAXBException, SAXException
{
    JAXBContext context = JAXBContext.newInstance(Foo.class);
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = sf.newSchema(Foo.class.getResource("/foo.xsd"));
    Unmarshaller unmarshaller = context.createUnmarshaller();
    unmarshaller.setSchema(schema);
    Foo foo = (Foo) unmarshaller.unmarshal(Foo.class.getResource("/foo.xml"));
    System.out.println("unmarshalled: " + foo.getBar());
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, "foo.xsd");
    marshaller.setSchema(schema);
    marshaller.marshal(foo, System.out);
}
这是输出,时区已被删除,日期表示已明显更改
unmarshalled: Tue Dec 31 19:00:00 CET 2013
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<foo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="foo.xsd">
    <bar>2013-12-31</bar>
</foo>
也许这个日期表示变化不是你所期望的,但这不是JAXB关注的问题,所代表的日期没有改变。
我忘记了反向生成Foo的绑定:
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" jaxb:extensionBindingPrefixes="xjc" jaxb:version="2.0">
    <jaxb:globalBindings>
        <xjc:javaType name="java.util.Date" xmlType="xsd:date" adapter="aaa.DateAdapter" />
    </jaxb:globalBindings>
</jaxb:bindings>
更新结束
抱歉,评论太长了...
我无法理解:
- 你为什么要用?
XmlGregorianCalendar 
- 为什么你应该/(/)到完全相同的数据结构?
marshalunmarshalserializedeserialize 
- 为什么要删除时区?
 
和
- 我使用直接和简单
java.util.Date
 
- 
marshal/unmarshal应始终涉及 s(至少对于 XML)String 
- 我真的没有看到一个很好的理由来任意删除日期表示的一部分。也许你想以绝对的方式序列化它。
 
然而
public class DateAdapter extends XmlAdapter<String, Date>
{
    @Override
    public String marshal(Date date)
    {
        DateFormat df = DateFormat.getDateTimeInstance();
        df.setTimeZone(TimeZone.getTimeZone("GMT"));
        return df.format(date);
    }
    @Override
    public Date unmarshal(String date) throws ParseException
    {
        DateFormat df = DateFormat.getDateTimeInstance();
        df.setTimeZone(TimeZone.getTimeZone("GMT"));
        return df.parse(date);
    }
    public static void main(String[] args) throws ParseException
    {
        DateAdapter adapter = new DateAdapter();
        String str = adapter.marshal(new Date());
        System.out.println(str); // 16-dic-2013 10.02.09  --> to gmt
        Date date = adapter.unmarshal(str);
        System.out.println(date); // Mon Dec 16 11:02:09 CET 2013  --> correct, i'm gmt+1
    }
}