exsltdatetime.java

来自「java jdk 1.4的源码」· Java 代码 · 共 958 行 · 第 1/3 页

JAVA
958
字号
     * See above.     */    public static XBoolean leapYear()    {      Calendar cal = Calendar.getInstance();      int yr = (int)cal.get(Calendar.YEAR);      return new XBoolean(yr % 400 == 0 || (yr % 100 != 0 && yr % 4 == 0));          }               /**     * The date:month-name function returns the full name of the month of a date.      * 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 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--)      * If the date/time string is not in one of these formats, then an empty string ('')      * is returned.      * The result is an English month name: one of 'January', 'February', 'March',      * 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November'      * or 'December'.      */    public static XString monthName(String datetimeIn)      throws ParseException    {      String[] edz = getEraDatetimeZone(datetimeIn);      String datetime = edz[1];      if (datetime == null)         return new XString("");            String[] formatsIn = {dt, d, gym, gm};      String formatOut = "MMMM";      return new XString (getNameOrAbbrev(datetimeIn, formatsIn, formatOut));        }        /**     * See above.     */    public static XString monthName()    {      Calendar cal = Calendar.getInstance();      String format = "MMMM";      return new XString(getNameOrAbbrev(format));      }            /**     * The date:month-abbreviation function returns the abbreviation of the month of      * a date. 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 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--)      * If the date/time string is not in one of these formats, then an empty string ('')      * is returned.      * The result is a three-letter English month abbreviation: one of 'Jan', 'Feb', 'Mar',      * 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov' or 'Dec'.      * An implementation of this extension function in the EXSLT date namespace must conform      * to the behaviour described in this document.      */    public static XString monthAbbreviation(String datetimeIn)      throws ParseException    {      String[] edz = getEraDatetimeZone(datetimeIn);      String datetime = edz[1];      if (datetime == null)         return new XString("");            String[] formatsIn = {dt, d, gym, gm};      String formatOut = "MMM";      return new XString (getNameOrAbbrev(datetimeIn, formatsIn, formatOut));    }        /**     * See above.     */    public static XString monthAbbreviation()    {      String format = "MMM";      return new XString(getNameOrAbbrev(format));      }            /**     * The date:day-name function returns the full name of the day of the week      * of a date.  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 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)      * If the date/time string is not in one of these formats, then the empty string ('')      * is returned.      * The result is an English day name: one of 'Sunday', 'Monday', 'Tuesday', 'Wednesday',      * 'Thursday' or 'Friday'.      * An implementation of this extension function in the EXSLT date namespace must conform      * to the behaviour described in this document.      */    public static XString dayName(String datetimeIn)      throws ParseException    {      String[] edz = getEraDatetimeZone(datetimeIn);      String datetime = edz[1];      if (datetime == null)         return new XString("");                  String[] formatsIn = {dt, d};      String formatOut = "EEEE";      return new XString (getNameOrAbbrev(datetimeIn, formatsIn, formatOut));        }        /**     * See above.     */    public static XString dayName()    {      String format = "EEEE";      return new XString(getNameOrAbbrev(format));            }            /**     * The date:day-abbreviation function returns the abbreviation of the day      * of the week of a date. 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 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)      * If the date/time string is not in one of these formats, then the empty string      * ('') is returned.      * The result is a three-letter English day abbreviation: one of 'Sun', 'Mon', 'Tue',      * 'Wed', 'Thu' or 'Fri'.      * An implementation of this extension function in the EXSLT date namespace must conform      * to the behaviour described in this document.      */    public static XString dayAbbreviation(String datetimeIn)      throws ParseException    {      String[] edz = getEraDatetimeZone(datetimeIn);      String datetime = edz[1];      if (datetime == null)         return new XString("");                        String[] formatsIn = {dt, d};      String formatOut = "EEE";      return new XString (getNameOrAbbrev(datetimeIn, formatsIn, formatOut));    }        /**     * See above.     */    public static XString dayAbbreviation()    {      String format = "EEE";      return new XString(getNameOrAbbrev(format));                  }        /**     * Returns an array with the 3 components that a datetime input string      * may contain: - (for BC era), datetime, and zone. If the zone is not     * valid, return null for that component.     */    private static String[] getEraDatetimeZone(String in)    {      String leader = "";      String datetime = in;      String zone = "";      if (in.charAt(0)=='-')      {        leader = "-"; //  '+' is implicit , not allowed        datetime = in.substring(1);      }      int z = getZoneStart(datetime);      if (z > 0)      {        zone = datetime.substring(z);        datetime = datetime.substring(0, z);      }      else if (z == -2)        zone = null;      //System.out.println("'" + leader + "' " + datetime + " " + zone);      return new String[]{leader, datetime, zone};      }            /**     * Get the start of zone information if the input ends     * with 'Z' or +/-hh:mm. If a zone string is not     * found, return -1; if the zone string is invalid,     * return -2.     */    private static int getZoneStart (String datetime)    {      if (datetime.indexOf("Z") == datetime.length()-1)        return datetime.indexOf("Z");      else if (               (datetime.lastIndexOf("-") == datetime.length()-6 &&                datetime.charAt(datetime.length()-3) == ':')                               ||                 (datetime.indexOf("+") == datetime.length() -6)              )      {        try        {          SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");          dateFormat.setLenient(false);          Date d = dateFormat.parse(datetime.substring(datetime.length() -5));          return datetime.length()-6;        }        catch (ParseException pe)        {          System.out.println("ParseException " + pe.getErrorOffset());          return -2; // Invalid.        }      }        return -1; // No zone information.    }        /**     * Attempt to parse an input string with the allowed formats, returning     * null if none of the formats work. Input formats are passed in longest to shortest,     * so if any parse operation fails with a parse error in the string, can     * immediately return null.     */    private static Date testFormats (String in, String[] formats)      throws ParseException    {      for (int i = 0; i <formats.length; i++)      {        try        {          SimpleDateFormat dateFormat = new SimpleDateFormat(formats[i]);          dateFormat.setLenient(false);                    return dateFormat.parse(in);        }        catch (ParseException pe)        {          if (pe.getErrorOffset() < in.length())            return null;        }      }      return null;    }            /**     * Parse the input string and return the corresponding calendar field     * number.     */    private static double getNumber(String in, String[] formats, int calField)      throws ParseException    {      Calendar cal = Calendar.getInstance();      cal.setLenient(false);      // Try the allowed formats, from longest to shortest.      Date date = testFormats(in, formats);      if (date == null) return Double.NaN;      cal.setTime(date);      return cal.get(calField);    }             /**     *  Get the full name or abbreviation of the month or day.     */    private static String getNameOrAbbrev(String in,                                          String[] formatsIn,                                         String formatOut)      throws ParseException    {      for (int i = 0; i <formatsIn.length; i++) // from longest to shortest.      {        try        {          SimpleDateFormat dateFormat = new SimpleDateFormat(formatsIn[i]);          dateFormat.setLenient(false);          Date dt = dateFormat.parse(in);                    dateFormat.applyPattern(formatOut);          return dateFormat.format(dt);        }        catch (ParseException pe)        {          // If ParseException occurred during input string, input is invalid.          // If the ParseException occurred at the end of the input string,          // another format may work.          if (pe.getErrorOffset() < in.length())            return "";        }      }      return "";    }    /**     * Get the full name or abbreviation for the current month or day      * (no input string).     */    private static String getNameOrAbbrev(String format)    {      Calendar cal = Calendar.getInstance();      SimpleDateFormat dateFormat = new SimpleDateFormat(format);      return dateFormat.format(cal.getTime());    }}

⌨️ 快捷键说明

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