⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exsltdatetime.java

📁 java1.6众多例子参考
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
     * 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 String dayAbbreviation(String datetimeIn)      throws ParseException    {      String[] edz = getEraDatetimeZone(datetimeIn);      String datetime = edz[1];      if (datetime == null)         return EMPTY_STR;                        String[] formatsIn = {dt, d};      String formatOut = "EEE";      return getNameOrAbbrev(datetimeIn, formatsIn, formatOut);    }        /**     * See above.     */    public static String dayAbbreviation()    {      String format = "EEE";      return 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)=='-' && !in.startsWith("--"))      {        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.length()-1;      else if (datetime.length() >=6       		&& datetime.charAt(datetime.length()-3) == ':'      		&& (datetime.charAt(datetime.length()-6) == '+'       		    || datetime.charAt(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.     */    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)        {        }      }      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], Locale.ENGLISH);          dateFormat.setLenient(false);          Date dt = dateFormat.parse(in);                    dateFormat.applyPattern(formatOut);          return dateFormat.format(dt);        }        catch (ParseException pe)        {        }      }      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, Locale.ENGLISH);      return dateFormat.format(cal.getTime());    }    /**     * The date:format-date function formats a date/time according to a pattern.     * <p>     * The first argument to date:format-date specifies the date/time to be      * formatted. It must be right or left-truncated date/time strings 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:      * <ul>     * <li>xs:dateTime (CCYY-MM-DDThh:mm:ss)      * <li>xs:date (CCYY-MM-DD)      * <li>xs:time (hh:mm:ss)      * <li>xs:gYearMonth (CCYY-MM)      * <li>xs:gYear (CCYY)      * <li>xs:gMonthDay (--MM-DD)      * <li>xs:gMonth (--MM--)      * <li>xs:gDay (---DD)     * </ul>     * The second argument is a string that gives the format pattern used to      * format the date. The format pattern must be in the syntax specified by      * the JDK 1.1 SimpleDateFormat class. The format pattern string is      * interpreted as described for the JDK 1.1 SimpleDateFormat class.      * <p>     * If the date/time format is right-truncated (i.e. in a format other than      * xs:time, or xs:dateTime) then any missing components are assumed to be as      * follows: if no month is specified, it is given a month of 01; if no day      * is specified, it is given a day of 01; if no time is specified, it is      * given a time of 00:00:00.      * <p>     * If the date/time format is left-truncated (i.e. xs:time, xs:gMonthDay,      * xs:gMonth or xs:gDay) and the format pattern has a token that uses a      * component that is missing from the date/time format used, then that token      * is replaced with an empty string ('') within the result.     *      * The author is Helg Bredow (helg.bredow@kalido.com)     */    public static String formatDate(String dateTime, String pattern)    {        final String yearSymbols = "Gy";        final String monthSymbols = "M";        final String daySymbols = "dDEFwW";        TimeZone timeZone;        String zone;        // Get the timezone information if it was supplied and modify the         // dateTime so that SimpleDateFormat will understand it.        if (dateTime.endsWith("Z") || dateTime.endsWith("z"))        {            timeZone = TimeZone.getTimeZone("GMT");            dateTime = dateTime.substring(0, dateTime.length()-1) + "GMT";            zone = "z";        }        else if ((dateTime.length() >= 6)                  && (dateTime.charAt(dateTime.length()-3) == ':')                  && ((dateTime.charAt(dateTime.length()-6) == '+')                     || (dateTime.charAt(dateTime.length()-6) == '-')))        {            String offset = dateTime.substring(dateTime.length()-6);                        if ("+00:00".equals(offset) || "-00:00".equals(offset))            {                timeZone = TimeZone.getTimeZone("GMT");            }            else            {                timeZone = TimeZone.getTimeZone("GMT" + offset);            }            zone = "z";            // Need to adjust it since SimpleDateFormat requires GMT+hh:mm but            // we have +hh:mm.            dateTime = dateTime.substring(0, dateTime.length()-6) + "GMT" + offset;        }        else        {            // Assume local time.            timeZone = TimeZone.getDefault();            zone = "";            // Leave off the timezone since SimpleDateFormat will assume local            // time if time zone is not included.        }        String[] formats = {dt + zone, d, gym, gy};                // Try the time format first. We need to do this to prevent         // SimpleDateFormat from interpreting a time as a year. i.e we just need        // to check if it's a time before we check it's a year.        try        {            SimpleDateFormat inFormat = new SimpleDateFormat(t + zone);            inFormat.setLenient(false);            Date d= inFormat.parse(dateTime);            SimpleDateFormat outFormat = new SimpleDateFormat(strip                (yearSymbols + monthSymbols + daySymbols, pattern));            outFormat.setTimeZone(timeZone);            return outFormat.format(d);        }        catch (ParseException pe)        {        }                // Try the right truncated formats.        for (int i = 0; i < formats.length; i++)        {            try            {                SimpleDateFormat inFormat = new SimpleDateFormat(formats[i]);                inFormat.setLenient(false);                Date d = inFormat.parse(dateTime);                SimpleDateFormat outFormat = new SimpleDateFormat(pattern);                outFormat.setTimeZone(timeZone);                return outFormat.format(d);            }            catch (ParseException pe)            {            }        }                // Now try the left truncated ones. The Java format() function doesn't        // return the correct strings in this case. We strip any pattern         // symbols that shouldn't be output so that they are not defaulted to         // inappropriate values in the output.        try        {            SimpleDateFormat inFormat = new SimpleDateFormat(gmd);            inFormat.setLenient(false);                      Date d = inFormat.parse(dateTime);            SimpleDateFormat outFormat = new SimpleDateFormat(strip(yearSymbols, pattern));            outFormat.setTimeZone(timeZone);            return outFormat.format(d);        }        catch (ParseException pe)        {        }        try        {            SimpleDateFormat inFormat = new SimpleDateFormat(gm);            inFormat.setLenient(false);            Date d = inFormat.parse(dateTime);            SimpleDateFormat outFormat = new SimpleDateFormat(strip(yearSymbols, pattern));            outFormat.setTimeZone(timeZone);            return outFormat.format(d);        }        catch (ParseException pe)        {        }        try        {            SimpleDateFormat inFormat = new SimpleDateFormat(gd);            inFormat.setLenient(false);            Date d = inFormat.parse(dateTime);            SimpleDateFormat outFormat = new SimpleDateFormat(strip(yearSymbols + monthSymbols, pattern));            outFormat.setTimeZone(timeZone);            return outFormat.format(d);        }        catch (ParseException pe)        {        }        return EMPTY_STR;    }        /**     * Strips occurrences of the given character from a date format pattern.     * @param symbols list of symbols to strip.     * @param pattern     * @return     */    private static String strip(String symbols, String pattern)    {        int quoteSemaphore = 0;        int i = 0;        StringBuffer result = new StringBuffer(pattern.length());        while (i < pattern.length())        {            char ch = pattern.charAt(i);            if (ch == '\'')            {                // Assume it's an openening quote so simply copy the quoted                 // text to the result. There is nothing to strip here.                int endQuote = pattern.indexOf('\'', i + 1);                if (endQuote == -1)                {                    endQuote = pattern.length();                }                result.append(pattern.substring(i, endQuote));                i = endQuote++;            }            else if (symbols.indexOf(ch) > -1)            {                // The char needs to be stripped.                i++;            }            else            {                result.append(ch);                i++;            }        }        return result.toString();    }}

⌨️ 快捷键说明

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