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. Mac安装wget的两种方法

    ​ 第一种.传统的安装包 A - 从ftp://ftp.gnu.org/gnu/wget/下载到最新的wget安装包到本地 B - 然后通过终端tar -zxvf命令解压到我们某个目录 C - 然后依 ...

  2. 系统时间不一致导致memcached的session不共享

    测试服务器需要做负载均衡,采用的是Nginx+Tomcat. 负载均衡配置成功之后,采用memcached配置session同步.总共4台服务器,三台服务器很顺利的配置成功,最后一台服务器死活不能共享 ...

  3. 问题-关于 in []使用过程中报错" Constant expression violates subrange bounds"

    问题现象:在DELPHI中使用户in [] 时参数大于255后,报错,错误如下:Constant expression violates subrange bounds E1012常量表达式超出子界 ...

  4. python 获取对象信息

    当我们拿到一个对象的引用时,如何知道这个对象是什么类型.有哪些方法呢? 使用type() 首先,我们来判断对象类型,使用type()函数: 基本类型都可以用type()判断: >>> ...

  5. msp430的两本书 电子版【worldsing笔记】

    msp430的两本书,pdf版 MSP430系列单片机接口_技术及系统设计实例 MSP430系列16位超低功耗单片机原理与应用 点击此处下载 ourdev_528863.pdf(文件大小:4.21M) ...

  6. [五]SpringMvc学习-Restful风格实现

    1.Restful风格的资源URL 无后缀资源的访问(csdn用法) 2.SpringMvc对Rest风格的支持 2.1将 /*.do改为/ 2.2 3.@PathVariable获取Url变量 @R ...

  7. 学习和理解C#的委托

    去年自学C#用的教程是入门级的<学通C#的24堂课>,教材里面也没有提到委托和事件,工作中也没怎么用到.后来一次在网上看了一些大牛的博客,读完之后感觉懵懵懂懂,似懂非懂,过了两三天之后,却 ...

  8. LINUX下的简单线程池

    前言 任何一种设计方式的引入都会带来额外的开支,是否使用,取决于能带来多大的好处和能带来多大的坏处,好处与坏处包括程序的性能.代码的可读性.代码的可维护性.程序的开发效率等. 线程池适用场合:任务比较 ...

  9. linux安装oracle

    目 录 一.硬件要求二.软件三.系统安装注意四.安装Oracle前的系统准备工作五.安装Oracle,并进行相关设置六.升级Oracle到patchset 10.2.0.4七.使用rlwrap调用sq ...

  10. Swif基本语法以及与OC比较三

         (未 经 博 主 同 意,不 得 转 载 !)   ------------------------华丽分割线----------------------- // // main.swift ...