📄 exsltdatetime.java
字号:
/* * Copyright 1999-2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *//* * $Id: ExsltDatetime.java,v 1.2.4.1 2005/09/10 18:50:49 jeffsuttor Exp $ */package com.sun.org.apache.xalan.internal.lib;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.Locale;import java.util.TimeZone;import com.sun.org.apache.xpath.internal.objects.XBoolean;import com.sun.org.apache.xpath.internal.objects.XNumber;import com.sun.org.apache.xpath.internal.objects.XObject;/** * 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> * @xsl.usage general */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"; static final String EMPTY_STR = ""; /** * 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 String 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 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 String 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 EMPTY_STR; String[] formatsIn = {dt, d}; String formatOut = d; Date date = testFormats(datetime, formatsIn); if (date == null) return EMPTY_STR; SimpleDateFormat dateFormat = new SimpleDateFormat(formatOut); dateFormat.setLenient(false); String dateOut = dateFormat.format(date); if (dateOut.length() == 0) return EMPTY_STR; else return (leader + dateOut + zone); } /** * See above. */ public static String date() { String datetime = dateTime().toString(); String date = datetime.substring(0, datetime.indexOf("T")); String zone = datetime.substring(getZoneStart(datetime)); return (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 String time(String timeIn) throws ParseException { String[] edz = getEraDatetimeZone(timeIn); String time = edz[1]; String zone = edz[2]; if (time == null || zone == null) return EMPTY_STR; String[] formatsIn = {dt, d, t}; String formatOut = t; Date date = testFormats(time, formatsIn); if (date == null) return EMPTY_STR; SimpleDateFormat dateFormat = new SimpleDateFormat(formatOut); String out = dateFormat.format(date); return (out + zone); } /** * See above. */ public static String time() { String datetime = dateTime().toString(); String time = datetime.substring(datetime.indexOf("T")+1); // The datetime() function returns the zone on the datetime string. If we // append it, we get the zone substring duplicated. // Fix for JIRA 2013 // String zone = datetime.substring(getZoneStart(datetime)); // return (time + zone); return (time); } /** * 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 double 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 Double.NaN; String[] formats = {dt, d, gym, gy}; double yr = getNumber(datetime, formats, Calendar.YEAR); if (ad || yr == Double.NaN) return yr; else return -yr; } /** * See above. */ public static double year() { Calendar cal = Calendar.getInstance(); return cal.get(Calendar.YEAR); } /** * The date:month-in-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) * xs:gMonth (--MM--) * xs:gMonthDay (--MM-DD) * If the date/time string is not in one of these formats, then NaN is returned. */ public static double monthInYear(String datetimeIn) throws ParseException { String[] edz = getEraDatetimeZone(datetimeIn); String datetime = edz[1]; if (datetime == null) return Double.NaN; String[] formats = {dt, d, gym, gm, gmd}; return getNumber(datetime, formats, Calendar.MONTH) + 1; } /** * See above. */ public static double monthInYear() { Calendar cal = Calendar.getInstance(); return cal.get(Calendar.MONTH) + 1; } /** * The date:week-in-year function returns the week of the year as a number. If no argument * is given, then the current local date/time, as returned by date:date-time is used as the * default argument. For the purposes of numbering, counting follows ISO 8601: week 1 in a year * is the week containing the first Thursday of the year, with new weeks beginning on a Monday. * The date/time string specified as the argument is 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) * If the date/time string is not in one of these formats, then NaN is returned. */ public static double weekInYear(String datetimeIn) throws ParseException { String[] edz = getEraDatetimeZone(datetimeIn); String datetime = edz[1]; if (datetime == null) return Double.NaN; String[] formats = {dt, d}; return getNumber(datetime, formats, Calendar.WEEK_OF_YEAR); } /** * See above. */ public static double weekInYear() { Calendar cal = Calendar.getInstance(); return cal.get(Calendar.WEEK_OF_YEAR); } /** * The date:day-in-year function returns the day of a date in a year * as a number. If no argument is given, then the current local * date/time, as returned by date:date-time is used the default argument. * The date/time string specified as the argument is 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) * If the date/time string is not in one of these formats, then NaN is returned. */ public static double dayInYear(String datetimeIn) throws ParseException { String[] edz = getEraDatetimeZone(datetimeIn); String datetime = edz[1]; if (datetime == null) return Double.NaN; String[] formats = {dt, d}; return getNumber(datetime, formats, Calendar.DAY_OF_YEAR); } /** * See above. */ public static double dayInYear() { Calendar cal = Calendar.getInstance(); return cal.get(Calendar.DAY_OF_YEAR); } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -