JAXB binds all three of the schema types xsd:datexsd:time and xsd:dateTime to XMLGregorianCalendar. This class is in the package javax.xml.datatype. (Do not confuse this with java.util.GregorianCalendar.) There is a convenient set of methods for getting at the various components such as year or day or minute. But creating any of these values isn't quite so simple because XMLGregorianCalendar is an abstract class. We'll illustrate this with a simple example for marshalling date and time.

The XML schema snippet shown below defines an element containing sub-elements with xsd:date and xsd:time.

<xsd:complexType name="DateTimeType">
<xsd:sequence>
<xsd:element name="Date" type="xsd:date"/>
<xsd:element name="Time" type="xsd:time"/>
</xsd:sequence>
</xsd:complexType>

The generated class contains the usual getters and setters:

public class DateTimeType {

    protected XMLGregorianCalendar date;
protected XMLGregorianCalendar time; public XMLGregorianCalendar getDate() {
return date;
} public void setDate(XMLGregorianCalendar value) {
this.date = value;
} public XMLGregorianCalendar getTime() {
return time;
} public void setTime(XMLGregorianCalendar value) {
this.time = value;
}
}

However, some work remains to be done before we can call either setter. It's the class javax.xml.datatype.DatatypeFactory that provides the methods with which we can create the javax.xml.datatype.XMLGregorianCalendar objects.

// Create a DateTimeType element for the current time and date.
ObjectFactory of = new ObjectFactory();
DateTimeType meta = of.createDateTimeType();
GregorianCalendar now = new GregorianCalendar(); // Obtain a DatatypeFactory instance.
DatatypeFactory df = DatatypeFactory.newInstance(); // Create an XMLGregorianCalendar with the current date.
XMLGregorianCalendar gcDate =
df.newXMLGregorianCalendarDate(
now.get( Calendar.YEAR ),
now.get( Calendar.MONTH ),
now.get( Calendar.DAY_OF_MONTH ),
DatatypeConstants.FIELD_UNDEFINED ); // Create an XMLGregorianCalendar with the current time.
XMLGregorianCalendar gcTime =
df.newXMLGregorianCalendarTime(
now.get( Calendar.HOUR_OF_DAY ),
now.get( Calendar.MINUTE ),
now.get( Calendar.SECOND ),
null, // no fraction
DatatypeConstants.FIELD_UNDEFINED ); // Insert sub-elements into the DateTimeType element.
meta.setDate( gcDate );
meta.setTime( gcTime );

You may have noticed the null argument in the method constructing an XMLGregorianCalendar with the time. This indicates that we don't care about fractions of seconds. It is, however, not possible to omit seconds entirely.

The XML element produced by this code will look like this:

<DateTime>
<Date>2008-07-23</Date>
<Time>18:42:24</Time>
</DateTime>

You should notice that the date and time representations follow ISO 8601.

JAXB - XML Schema Types, Date and Time的更多相关文章

  1. JAXB - XML Schema Types, Defining Subtypes

    Although object orientation isn't a key feature of XML or the XML Schema language, it's still possib ...

  2. JAXB - XML Schema Types, Binary Data

    Data that has no "natural" representation with printable characters must, for inclusion in ...

  3. JAXB - XML Schema Types, Defining an Enumeration

    If you want a data type that enumerates discrete values you should use a restriction of the schema t ...

  4. JAXB - XML Schema Types, Defining Types for XML Elements With Content

    Content: A Value The content of an XML element may be some value, or one or more subordinate element ...

  5. JAXB - XML Schema Types, Defining Types for XML Elements Without Content

    Types for XML elements are constructed using xsd:complexType, even if they do not have content. The ...

  6. XML Schema and XMLspy notes

    Introduction An xml documents consists of elements, attributes and text. There are two structures in ...

  7. XSD(XML Schema Definition)用法实例介绍以及C#使用xsd文件验证XML格式

    XML Schema 语言也称作 XML Schema 定义(XML Schema Definition,XSD),作用是定义 XML 文档的合法构建模块,类似 DTD,但更加强大. 作用有: ①定义 ...

  8. 【转】XSD (xml Schema Definition)

    来自:http://www.cnblogs.com/newsouls/archive/2011/10/28/2227765.html Xml Schema的用途 1.  定义一个Xml文档中都有什么元 ...

  9. [BTS] System.Xml.Schema.XmlSchemaException: The complexType has already been declared when generate IDoc schema.

    I use wcf-sap adapter for generate the schema of IDoc that named "YHREMPMASTER". but throw ...

随机推荐

  1. 关于Aggregate 的一点用法

    比如 我们要 将数组或者对象中的某列或某属性 的值取出,然后 用 逗号隔开. 1.通常我们可能会用for 或foreach 来循环,然后将取出的值并添加至StringBuilder 2.用Aggreg ...

  2. android NDK 实用学习(一)-获取java端类及其类变量

    近期为android 端项目包装一些c++代码,故学习ndk相关知识,现总结如下: 1,java与c++类型参照图: 2,此测试中使用的java类: package com.dasea.test.co ...

  3. HDFS的shell操作

    bin/hadoop命令操作: namenode -format 格式化文件系统 fs(缩写:FileSystem) 运行一个文件系统的用户客户端 bin/hadoop fs常用命令操作: -ls h ...

  4. Struts2.x jsp页面无法使用jsp:forward跳转到action

    问题:使用<jsp:forward page="test"></jsp:forward>语句无法跳转到test所对应的action. 解决办法:在web.x ...

  5. QQMusic绿钻兄,你可安好?我需要晴天。

    不好意思,年纪这样大了,还依靠吐槽来保持呆毛的正能量,实在对不住,先说对不起. QQMusic是我最喜欢的腾讯增值服务,正版内容,海量歌手,高清下载.实在是音乐软件中高大上的典范,除了歌手排名中前十中 ...

  6. Android反编译基础(apktoos)--广工图书馆APK

    更多精彩内容 :http://www.chenchuangfeng.com QQ:375061590 ------------------------------------------------- ...

  7. Action开发、通配符、路径问题和struts中常量用法

    1.action开发 开发的几种方式 (1).继承自ActionSupport,(如果用struts的数据效验功,能必须必须使用此功能,因为ActionSupport实现了数据效验的接口) publi ...

  8. PowerShell中的数学计算

    Double类型和float都属于浮点类型,精度不高.而Decimal属于高精度

  9. HDU 1718 Rank counting sort解法

    本题是利用counting sort的思想去解题. 注意本题,好像利用直接排序,然后查找rank是会直接被判WA的.奇怪的推断系统. 由于分数值的范围是0到100,很小,而student 号码又很大, ...

  10. 【32】确定你的public继承塑模出Is-A关系

    1.public继承表示Is-A关系,也就是满足里氏代换.与之相对应的,private继承表示根据某物实现出,不满足里氏代换.子类对象初始化父类引用,编译通不过. 2.考虑下面的需求,企鹅继承鸟,Bi ...