exsltdatetime.java
来自「java jdk 1.4的源码」· Java 代码 · 共 958 行 · 第 1/3 页
JAVA
958 行
/* * The Apache Software License, Version 1.1 * * * Copyright (c) 1999 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Xalan" and "Apache Software Foundation" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation and was * originally based on software copyright (c) 1999, Lotus * Development Corporation., http://www.lotus.com. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */package org.apache.xalan.lib;import java.util.Date;import java.util.TimeZone;import java.util.Calendar;import java.text.SimpleDateFormat;import java.text.DateFormat;import java.text.ParseException;import org.apache.xpath.objects.XString;import org.apache.xpath.objects.XNumber;import org.apache.xpath.objects.XBoolean;import org.apache.xpath.objects.XObject;/** * <meta name="usage" content="general"/> * This class contains EXSLT dates and times extension functions. * It is accessed by specifying a namespace URI as follows: * <pre> * xmlns:datetime="http://exslt.org/dates-and-times" * </pre> * * The documentation for each function has been copied from the relevant * EXSLT Implementer page. * * @see <a href="http://www.exslt.org/">EXSLT</a> */public class ExsltDatetime{ // Datetime formats (era and zone handled separately). static final String dt = "yyyy-MM-dd'T'HH:mm:ss"; static final String d = "yyyy-MM-dd"; static final String gym = "yyyy-MM"; static final String gy = "yyyy"; static final String gmd = "MM-dd"; static final String gm = "MM"; static final String gd = "dd"; static final String t = "HH:mm:ss"; /** * The date:date-time function returns the current date and time as a date/time string. * The date/time string that's returned must be a string in the format defined as the * lexical representation of xs:dateTime in * <a href="http://www.w3.org/TR/xmlschema-2/#dateTime">[3.2.7 dateTime]</a> of * <a href="http://www.w3.org/TR/xmlschema-2/">[XML Schema Part 2: Datatypes]</a>. * The date/time format is basically CCYY-MM-DDThh:mm:ss, although implementers should consult * <a href="http://www.w3.org/TR/xmlschema-2/">[XML Schema Part 2: Datatypes]</a> and * <a href="http://www.iso.ch/markete/8601.pdf">[ISO 8601]</a> for details. * The date/time string format must include a time zone, either a Z to indicate Coordinated * Universal Time or a + or - followed by the difference between the difference from UTC * represented as hh:mm. */ public static XString dateTime() { Calendar cal = Calendar.getInstance(); Date datetime = cal.getTime(); // Format for date and time. SimpleDateFormat dateFormat = new SimpleDateFormat(dt); StringBuffer buff = new StringBuffer(dateFormat.format(datetime)); // Must also include offset from UTF. // Get the offset (in milliseconds). int offset = cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET); // If there is no offset, we have "Coordinated // Universal Time." if (offset == 0) buff.append("Z"); else { // Convert milliseconds to hours and minutes int hrs = offset/(60*60*1000); // In a few cases, the time zone may be +/-hh:30. int min = offset%(60*60*1000); char posneg = hrs < 0? '-': '+'; buff.append(posneg + formatDigits(hrs) + ':' + formatDigits(min)); } return new XString(buff.toString()); } /** * Represent the hours and minutes with two-digit strings. * @param q hrs or minutes. * @return two-digit String representation of hrs or minutes. */ private static String formatDigits(int q) { String dd = String.valueOf(Math.abs(q)); return dd.length() == 1 ? '0' + dd : dd; } /** * The date:date function returns the date specified in the date/time string given * as the argument. If no argument is given, then the current local date/time, as * returned by date:date-time is used as a default argument. * The date/time string that's returned must be a string in the format defined as the * lexical representation of xs:dateTime in * <a href="http://www.w3.org/TR/xmlschema-2/#dateTime">[3.2.7 dateTime]</a> of * <a href="http://www.w3.org/TR/xmlschema-2/">[XML Schema Part 2: Datatypes]</a>. * If the argument is not in either of these formats, date:date returns an empty string (''). * The date/time format is basically CCYY-MM-DDThh:mm:ss, although implementers should consult * <a href="http://www.w3.org/TR/xmlschema-2/">[XML Schema Part 2: Datatypes]</a> and * <a href="http://www.iso.ch/markete/8601.pdf">[ISO 8601]</a> for details. * The date is returned as a string with a lexical representation as defined for xs:date in * [3.2.9 date] of [XML Schema Part 2: Datatypes]. The date format is basically CCYY-MM-DD, * although implementers should consult [XML Schema Part 2: Datatypes] and [ISO 8601] for details. * If no argument is given or the argument date/time specifies a time zone, then the date string * format must include a time zone, either a Z to indicate Coordinated Universal Time or a + or - * followed by the difference between the difference from UTC represented as hh:mm. If an argument * is specified and it does not specify a time zone, then the date string format must not include * a time zone. */ public static XString date(String datetimeIn) throws ParseException { String[] edz = getEraDatetimeZone(datetimeIn); String leader = edz[0]; String datetime = edz[1]; String zone = edz[2]; if (datetime == null || zone == null) return new XString(""); String[] formatsIn = {dt, d}; String formatOut = d; Date date = testFormats(datetime, formatsIn); if (date == null) return new XString(""); SimpleDateFormat dateFormat = new SimpleDateFormat(formatOut); dateFormat.setLenient(false); String dateOut = dateFormat.format(date); if (dateOut.length() == 0) return new XString(""); else return new XString(leader + dateOut + zone); } /** * See above. */ public static XString date() { String datetime = dateTime().toString(); String date = datetime.substring(0, datetime.indexOf("T")); String zone = datetime.substring(getZoneStart(datetime)); return new XString(date + zone); } /** * The date:time function returns the time specified in the date/time string given * as the argument. If no argument is given, then the current local date/time, as * returned by date:date-time is used as a default argument. * The date/time string that's returned must be a string in the format defined as the * lexical representation of xs:dateTime in * <a href="http://www.w3.org/TR/xmlschema-2/#dateTime">[3.2.7 dateTime]</a> of * <a href="http://www.w3.org/TR/xmlschema-2/">[XML Schema Part 2: Datatypes]</a>. * If the argument string is not in this format, date:time returns an empty string (''). * The date/time format is basically CCYY-MM-DDThh:mm:ss, although implementers should consult * <a href="http://www.w3.org/TR/xmlschema-2/">[XML Schema Part 2: Datatypes]</a> and * <a href="http://www.iso.ch/markete/8601.pdf">[ISO 8601]</a> for details. * The date is returned as a string with a lexical representation as defined for xs:time in * <a href="http://www.w3.org/TR/xmlschema-2/#time">[3.2.8 time]</a> of [XML Schema Part 2: Datatypes]. * The time format is basically hh:mm:ss, although implementers should consult [XML Schema Part 2: * Datatypes] and [ISO 8601] for details. * If no argument is given or the argument date/time specifies a time zone, then the time string * format must include a time zone, either a Z to indicate Coordinated Universal Time or a + or - * followed by the difference between the difference from UTC represented as hh:mm. If an argument * is specified and it does not specify a time zone, then the time string format must not include * a time zone. */ public static XString time(String timeIn) throws ParseException { String[] edz = getEraDatetimeZone(timeIn); String time = edz[1]; String zone = edz[2]; if (time == null || zone == null) return new XString(""); String[] formatsIn = {dt, d}; String formatOut = t; Date date = testFormats(time, formatsIn); if (date == null) return new XString(""); SimpleDateFormat dateFormat = new SimpleDateFormat(formatOut); String out = dateFormat.format(date); return new XString(out + zone); } /** * See above. */ public static XString time() { String datetime = dateTime().toString(); String time = datetime.substring(datetime.indexOf("T")+1); String zone = datetime.substring(getZoneStart(datetime)); return new XString(time + zone); } /** * The date:year function returns the year of a date as a number. If no * argument is given, then the current local date/time, as returned by * date:date-time is used as a default argument. * The date/time string specified as the first argument must be a right-truncated * string in the format defined as the lexical representation of xs:dateTime in one * of the formats defined in * <a href="http://www.w3.org/TR/xmlschema-2/">[XML Schema Part 2: Datatypes]</a>. * The permitted formats are as follows: * xs:dateTime (CCYY-MM-DDThh:mm:ss) * xs:date (CCYY-MM-DD) * xs:gYearMonth (CCYY-MM) * xs:gYear (CCYY) * If the date/time string is not in one of these formats, then NaN is returned. */ public static XNumber year(String datetimeIn) throws ParseException { String[] edz = getEraDatetimeZone(datetimeIn); boolean ad = edz[0].length() == 0; // AD (Common Era -- empty leader) String datetime = edz[1]; if (datetime == null) return new XNumber(Double.NaN); String[] formats = {dt, d, gym, gy}; double yr = getNumber(datetime, formats, Calendar.YEAR); if (ad || yr == Double.NaN) return new XNumber(yr); else return new XNumber(-yr); } /** * See above. */ public static XNumber year() { Calendar cal = Calendar.getInstance(); return new XNumber(cal.get(Calendar.YEAR)); } /** * The date:year function returns the month of a date as a number. If no argument * is given, then the current local date/time, as returned by date:date-time is used * as a default argument. * The date/time string specified as the first argument is a left or right-truncated * string in the format defined as the lexical representation of xs:dateTime in one of * the formats defined in * <a href="http://www.w3.org/TR/xmlschema-2/">[XML Schema Part 2: Datatypes]</a>. * The permitted formats are as follows: * xs:dateTime (CCYY-MM-DDThh:mm:ss) * xs:date (CCYY-MM-DD) * xs:gYearMonth (CCYY-MM) * If the date/time string is not in one of these formats, then NaN is returned. */ public static XNumber monthInYear(String datetimeIn) throws ParseException { String[] edz = getEraDatetimeZone(datetimeIn); String datetime = edz[1]; if (datetime == null) return new XNumber(Double.NaN); String[] formats = {dt, d, gym};
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?