xmlgregoriancalendarimpl.java

来自「JAVA 所有包」· Java 代码 · 共 1,796 行 · 第 1/5 页

JAVA
1,796
字号
        // compute format string for this lexical representation.        String format = null;        String lexRep = lexicalRepresentation;        final int NOT_FOUND = -1;        int lexRepLength = lexRep.length();        // current parser needs a format string,        // use following heuristics to figure out what xml schema date/time        // datatype this lexical string could represent.        // Fix 4971612: invalid SCCS macro substitution in data string,        //   no %{alpha}% to avoid SCCS maco substitution        if (lexRep.indexOf('T') != NOT_FOUND) {            // found Date Time separater, must be xsd:DateTime            format = "%Y-%M-%DT%h:%m:%s" + "%z";        } else if (lexRepLength >= 3 && lexRep.charAt(2) == ':') {            // found ":", must be xsd:Time            format = "%h:%m:%s" + "%z";        } else if (lexRep.startsWith("--")) {            // check for gDay || gMonth || gMonthDay            if (lexRepLength >= 3 && lexRep.charAt(2) == '-') {                // gDay, ---DD(z?)                format = "---%D" + "%z";            } else if (lexRepLength == 4     // --MM                    || lexRepLength == 5     // --MMZ                    || lexRepLength == 10) { // --MMSHH:MM                // gMonth, --MM(z?),                 // per XML Schema Errata, used to be --MM--(z?)                format = "--%M" + "%z";            } else {                // gMonthDay, --MM-DD(z?), (or invalid lexicalRepresentation)                // length should be:                //  7: --MM-DD                //  8: --MM-DDZ                // 13: --MM-DDSHH:MM                format = "--%M-%D" + "%z";            }        } else {            // check for Date || GYear | GYearMonth            int countSeparator = 0;            // start at index 1 to skip potential negative sign for year.            int timezoneOffset = lexRep.indexOf(':');            if (timezoneOffset != NOT_FOUND) {                // found timezone, strip it off for distinguishing                // between Date, GYear and GYearMonth so possible                // negative sign in timezone is not mistaken as                // a separator.                lexRepLength -= 6;            }            for (int i = 1; i < lexRepLength; i++) {                if (lexRep.charAt(i) == '-') {                    countSeparator++;                }            }            if (countSeparator == 0) {                // GYear                format = "%Y" + "%z";            } else if (countSeparator == 1) {                // GYearMonth                format = "%Y-%M" + "%z";            } else {                // Date or invalid lexicalRepresentation                // Fix 4971612: invalid SCCS macro substitution in data string                format = "%Y-%M-%D" + "%z";            }        }        Parser p = new Parser(format, lexRep);        p.parse();        // check for validity        if (!isValid()) {            throw new IllegalArgumentException(                    DatatypeMessageFormatter.formatMessage(null, "InvalidXGCRepresentation", new Object[]{lexicalRepresentation})                    //"\"" + lexicalRepresentation + "\" is not a valid representation of an XML Gregorian Calendar value."            );        }    }    /**     * <p>Create an instance with all date/time datatype fields set to     * {@link DatatypeConstants#FIELD_UNDEFINED} or null respectively.</p>     */    public XMLGregorianCalendarImpl() {        // field initializers already do the correct initialization.    }    /**     * <p>Private constructor allowing for complete value spaces allowed by     * W3C XML Schema 1.0 recommendation for xsd:dateTime and related     * builtin datatypes. Note that <code>year</code> parameter supports     * arbitrarily large numbers and fractionalSecond has infinite     * precision.</p>     *     * @param year of <code>XMLGregorianCalendar</code> to be created.     * @param month of <code>XMLGregorianCalendar</code> to be created.     * @param day of <code>XMLGregorianCalendar</code> to be created.     * @param hour of <code>XMLGregorianCalendar</code> to be created.     * @param minute of <code>XMLGregorianCalendar</code> to be created.     * @param second of <code>XMLGregorianCalendar</code> to be created.     * @param fractionalSecond of <code>XMLGregorianCalendar</code> to be created.     * @param timezone of <code>XMLGregorianCalendar</code> to be created.     *     */    protected XMLGregorianCalendarImpl(        BigInteger year,        int month,        int day,        int hour,        int minute,        int second,        BigDecimal fractionalSecond,        int timezone) {		setYear(year);        setMonth(month);        setDay(day);        setTime(hour, minute, second, fractionalSecond);		setTimezone(timezone);		// check for validity		if (!isValid()) {            throw new IllegalArgumentException(                DatatypeMessageFormatter.formatMessage(null,                    "InvalidXGCValue-fractional",                    new Object[] { year, new Integer(month), new Integer(day),                    new Integer(hour), new Integer(minute), new Integer(second),                    fractionalSecond, new Integer(timezone)})			);			/**                String yearString = "null";                if (year != null) {                    yearString = year.toString();                }                String fractionalSecondString = "null";                if (fractionalSecond != null) {                    fractionalSecondString = fractionalSecond.toString();                }                throw new IllegalArgumentException(                    "year = " + yearString                    + ", month = " + month                    + ", day = " + day                    + ", hour = " + hour                    + ", minute = " + minute                    + ", second = " + second                    + ", fractionalSecond = " + fractionalSecondString                    + ", timezone = " + timezone                    + ", is not a valid representation of an XML Gregorian Calendar value."                );                */		}    }    /**     * <p>Private constructor of value spaces that a     * <code>java.util.GregorianCalendar</code> instance would need to convert to an     * <code>XMLGregorianCalendar</code> instance.</p>     *     * <p><code>XMLGregorianCalendar eon</code> and     * <code>fractionalSecond</code> are set to <code>null</code></p>     *     * @param year of <code>XMLGregorianCalendar</code> to be created.     * @param month of <code>XMLGregorianCalendar</code> to be created.     * @param day of <code>XMLGregorianCalendar</code> to be created.     * @param hour of <code>XMLGregorianCalendar</code> to be created.     * @param minute of <code>XMLGregorianCalendar</code> to be created.     * @param second of <code>XMLGregorianCalendar</code> to be created.     * @param millisecond of <code>XMLGregorianCalendar</code> to be created.     * @param timezone of <code>XMLGregorianCalendar</code> to be created.     */    private XMLGregorianCalendarImpl(        int year,        int month,        int day,        int hour,        int minute,        int second,		int millisecond,        int timezone) {		setYear(year);        setMonth(month);        setDay(day);        setTime(hour, minute, second);		setTimezone(timezone);		setMillisecond(millisecond);		if (!isValid()) {            throw new IllegalArgumentException(                DatatypeMessageFormatter.formatMessage(null,                "InvalidXGCValue-milli",                new Object[] { new Integer(year), new Integer(month), new Integer(day),                new Integer(hour), new Integer(minute), new Integer(second),                new Integer(millisecond), new Integer(timezone)})			);                /*                throw new IllegalArgumentException(                    "year = " + year                    + ", month = " + month                    + ", day = " + day                    + ", hour = " + hour                    + ", minute = " + minute                    + ", second = " + second                    + ", millisecond = " + millisecond                    + ", timezone = " + timezone                    + ", is not a valid representation of an XML Gregorian Calendar value."                    );                 */		}    }	/**	 * <p>Convert a <code>java.util.GregorianCalendar</code> to XML Schema 1.0	 * representation.</p>	 *	 * <table border="2" rules="all" cellpadding="2">	 *   <thead>	 *     <tr>	 *       <th align="center" colspan="2">	 *          Field by Field Conversion from	 *          <code>java.util.GregorianCalendar</code> to this class	 *       </th>	 *     </tr>	 *   </thead>	 *   <tbody>	 *     <tr>	 *        <th><code>javax.xml.datatype.XMLGregorianCalendar</code> field</th>	 *        <th><code>java.util.GregorianCalendar</code> field</th>	 *     </tr>	 *     <tr>	 *       <th>{@link #setYear(int)}</th>	 *       <th><code>ERA == GregorianCalendar.BC ? -YEAR : YEAR</code></th>	 *     </tr>	 *     <tr>	 *       <th>{@link #setMonth(int)}</th>	 *       <th><code>MONTH + 1</code></th>	 *     </tr>	 *     <tr>	 *       <th>{@link #setDay(int)}</th>	 *       <th><code>DAY_OF_MONTH</code></th>	 *     </tr>	 *     <tr>	 *       <th>{@link #setTime(int,int,int, BigDecimal)}</th>	 *       <th><code>HOUR_OF_DAY, MINUTE, SECOND, MILLISECOND</code></th>	 *     </tr>	 *     <tr>	 *       <th>{@link #setTimezone(int)}<i>*</i></th>	 *       <th><code>(ZONE_OFFSET + DST_OFFSET) / (60*1000)</code><br/>	 *       <i>(in minutes)</i>	 *       </th>	 *     </tr>	 *   </tbody>	 * </table>	 * <p><i>*</i>conversion loss of information. It is not possible to represent	 * a <code>java.util.GregorianCalendar</code> daylight savings timezone id in the	 * XML Schema 1.0 date/time datatype representation.</p>	 *	 * <p>To compute the return value's <code>TimeZone</code> field,	 * <ul>	 * <li>when <code>this.getTimezone() != DatatypeConstants.FIELD_UNDEFINED</code>,	 * create a <code>java.util.TimeZone</code> with a custom timezone id	 * using the <code>this.getTimezone()</code>.</li>	 * <li>else use the <code>GregorianCalendar</code> default timezone value	 * for the host is defined as specified by	 * <code>java.util.TimeZone.getDefault()</code>.</li></p>	 *	 * @param cal <code>java.util.GregorianCalendar</code> used to create <code>XMLGregorianCalendar</code>	 */    public XMLGregorianCalendarImpl(GregorianCalendar cal) {        int year = cal.get(Calendar.YEAR);        if (cal.get(Calendar.ERA) == GregorianCalendar.BC) {            year = -year;        }        this.setYear(year);        // Calendar.MONTH is zero based, XSD Date datatype's month field starts        // with JANUARY as 1.        this.setMonth(cal.get(Calendar.MONTH) + 1);        this.setDay(cal.get(Calendar.DAY_OF_MONTH));        this.setTime(                cal.get(Calendar.HOUR_OF_DAY),                cal.get(Calendar.MINUTE),                cal.get(Calendar.SECOND),                cal.get(Calendar.MILLISECOND));        // Calendar ZONE_OFFSET and DST_OFFSET fields are in milliseconds.        int offsetInMinutes = (cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET)) / (60 * 1000);        this.setTimezone(offsetInMinutes);    }    // Factories    /**     * <p>Create a Java representation of XML Schema builtin datatype <code>dateTime</code>.     * All possible fields are specified for this factory method.</p>     *     * @param year represents both high-order eons and low-order year.     * @param month of <code>dateTime</code>     * @param day of <code>dateTime</code>     * @param hours of <code>dateTime</code>     * @param minutes of <code>dateTime</code>     * @param seconds of <code>dateTime</code>     * @param fractionalSecond value of null indicates optional field is absent.     * @param timezone offset in minutes. {@link DatatypeConstants#FIELD_UNDEFINED} indicates optional field is not set.     *     * @return <code>XMLGregorianCalendar</code> created from parameter values.     *     * @see DatatypeConstants#FIELD_UNDEFINED     *     * @throws IllegalArgumentException if any parameter is outside value     * constraints for the field as specified in     * <a href="#datetimefieldmapping">date/time field mapping table</a>.     */    public static XMLGregorianCalendar createDateTime(        BigInteger year,        int month,        int day,        int hours,        int minutes,        int seconds,        BigDecimal fractionalSecond,        int timezone) {        return new XMLGregorianCalendarImpl(            year,            month,            day,            hours,            minutes,            seconds,            fractionalSecond,            timezone);    }    /**     * <p>Create a Java instance of XML Schema builtin datatype dateTime.</p>     *     * @param year represents both high-order eons and low-order year.     * @param month of <code>dateTime</code>     * @param day of <code>dateTime</code>     * @param hour of <code>dateTime</code>     * @param minute of <code>dateTime</code>     * @param second of <code>dateTime</code>     *     * @return <code>XMLGregorianCalendar</code> created from parameter values.     *     * @throws IllegalArgumentException if any parameter is outside value constraints for the field as specified in

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?