• JAXB


    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.

  • 相关阅读:
    FJoi2017 1月21日模拟赛 comparison(平衡树+thita重构)
    juruo的刷题&博文祭
    [bzoj4247][挂饰] (动规+排序)
    FJoi2017 1月20日模拟赛 直线斯坦纳树(暴力+最小生成树+骗分+人工构造+随机乱搞)
    FJoi2017 1月20日模拟赛 交错和(等差数列+rmq)
    FJoi2017 1月20日模拟赛 恐狼后卫(口糊动规)
    【spoj 5971】lcmsum
    【bzoj 4025 改编版】graph
    【CF 718C】fibonacci
    【CF 482E】ELCA
  • 原文地址:https://www.cnblogs.com/huey/p/5504863.html
Copyright © 2020-2023  润新知