jaxp/src/com/sun/org/apache/xalan/internal/lib/ExsltDatetime.java
author duke
Wed, 05 Jul 2017 19:04:46 +0200
changeset 18896 98b0babd0565
parent 12458 d601e4bba306
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     2
 * reserved comment block
7f561c08de6b Initial load
duke
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
     5
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     6
 * Copyright 1999-2004 The Apache Software Foundation.
7f561c08de6b Initial load
duke
parents:
diff changeset
     7
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
     8
 * Licensed under the Apache License, Version 2.0 (the "License");
7f561c08de6b Initial load
duke
parents:
diff changeset
     9
 * you may not use this file except in compliance with the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    10
 * You may obtain a copy of the License at
7f561c08de6b Initial load
duke
parents:
diff changeset
    11
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
 *     http://www.apache.org/licenses/LICENSE-2.0
7f561c08de6b Initial load
duke
parents:
diff changeset
    13
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 * Unless required by applicable law or agreed to in writing, software
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
 * distributed under the License is distributed on an "AS IS" BASIS,
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
 * See the License for the specific language governing permissions and
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
 * limitations under the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
    21
 * $Id: ExsltDatetime.java,v 1.2.4.1 2005/09/10 18:50:49 jeffsuttor Exp $
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
package com.sun.org.apache.xalan.internal.lib;
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
import java.text.ParseException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
import java.text.SimpleDateFormat;
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
import java.util.Calendar;
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
import java.util.Date;
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
import java.util.Locale;
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
import java.util.TimeZone;
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
import com.sun.org.apache.xpath.internal.objects.XBoolean;
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
import com.sun.org.apache.xpath.internal.objects.XNumber;
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
import com.sun.org.apache.xpath.internal.objects.XObject;
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
 * This class contains EXSLT dates and times extension functions.
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
 * It is accessed by specifying a namespace URI as follows:
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
 * <pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
 *    xmlns:datetime="http://exslt.org/dates-and-times"
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
 * </pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
 * The documentation for each function has been copied from the relevant
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
 * EXSLT Implementer page.
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
 * @see <a href="http://www.exslt.org/">EXSLT</a>
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
 * @xsl.usage general
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
public class ExsltDatetime
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
{
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
    // Datetime formats (era and zone handled separately).
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
    static final String dt = "yyyy-MM-dd'T'HH:mm:ss";
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
    static final String d = "yyyy-MM-dd";
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
    static final String gym = "yyyy-MM";
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
    static final String gy = "yyyy";
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
    static final String gmd = "--MM-dd";
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
    static final String gm = "--MM--";
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
    static final String gd = "---dd";
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
    static final String t = "HH:mm:ss";
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
    static final String EMPTY_STR = "";
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
     * The date:date-time function returns the current date and time as a date/time string.
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
     * The date/time string that's returned must be a string in the format defined as the
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
     * lexical representation of xs:dateTime in
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
     * <a href="http://www.w3.org/TR/xmlschema-2/#dateTime">[3.2.7 dateTime]</a> of
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
     * <a href="http://www.w3.org/TR/xmlschema-2/">[XML Schema Part 2: Datatypes]</a>.
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
     * The date/time format is basically CCYY-MM-DDThh:mm:ss, although implementers should consult
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
     * <a href="http://www.w3.org/TR/xmlschema-2/">[XML Schema Part 2: Datatypes]</a> and
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
     * <a href="http://www.iso.ch/markete/8601.pdf">[ISO 8601]</a> for details.
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
     * The date/time string format must include a time zone, either a Z to indicate Coordinated
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
     * Universal Time or a + or - followed by the difference between the difference from UTC
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
     * represented as hh:mm.
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
    public static String dateTime()
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
      Calendar cal = Calendar.getInstance();
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
      Date datetime = cal.getTime();
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
      // Format for date and time.
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
      SimpleDateFormat dateFormat = new SimpleDateFormat(dt);
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
      StringBuffer buff = new StringBuffer(dateFormat.format(datetime));
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
      // Must also include offset from UTF.
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
      // Get the offset (in milliseconds).
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
      int offset = cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET);
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
      // If there is no offset, we have "Coordinated
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
      // Universal Time."
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
      if (offset == 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
        buff.append("Z");
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
      else
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
        // Convert milliseconds to hours and minutes
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
        int hrs = offset/(60*60*1000);
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
        // In a few cases, the time zone may be +/-hh:30.
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
        int min = offset%(60*60*1000);
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
        char posneg = hrs < 0? '-': '+';
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   100
        buff.append(posneg).append(formatDigits(hrs)).append(':').append(formatDigits(min));
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
      return buff.toString();
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
     * Represent the hours and minutes with two-digit strings.
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
     * @param q hrs or minutes.
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
     * @return two-digit String representation of hrs or minutes.
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
    private static String formatDigits(int q)
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
      String dd = String.valueOf(Math.abs(q));
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
      return dd.length() == 1 ? '0' + dd : dd;
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
     * The date:date function returns the date specified in the date/time string given
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
     * as the argument. If no argument is given, then the current local date/time, as
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
     * returned by date:date-time is used as a default argument.
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
     * The date/time string that's returned must be a string in the format defined as the
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
     * lexical representation of xs:dateTime in
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
     * <a href="http://www.w3.org/TR/xmlschema-2/#dateTime">[3.2.7 dateTime]</a> of
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
     * <a href="http://www.w3.org/TR/xmlschema-2/">[XML Schema Part 2: Datatypes]</a>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
     * If the argument is not in either of these formats, date:date returns an empty string ('').
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
     * The date/time format is basically CCYY-MM-DDThh:mm:ss, although implementers should consult
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
     * <a href="http://www.w3.org/TR/xmlschema-2/">[XML Schema Part 2: Datatypes]</a> and
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
     * <a href="http://www.iso.ch/markete/8601.pdf">[ISO 8601]</a> for details.
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
     * The date is returned as a string with a lexical representation as defined for xs:date in
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
     * [3.2.9 date] of [XML Schema Part 2: Datatypes]. The date format is basically CCYY-MM-DD,
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
     * although implementers should consult [XML Schema Part 2: Datatypes] and [ISO 8601] for details.
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
     * If no argument is given or the argument date/time specifies a time zone, then the date string
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
     * format must include a time zone, either a Z to indicate Coordinated Universal Time or a + or -
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
     * followed by the difference between the difference from UTC represented as hh:mm. If an argument
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
     * is specified and it does not specify a time zone, then the date string format must not include
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
     * a time zone.
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
    public static String date(String datetimeIn)
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
      throws ParseException
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
      String[] edz = getEraDatetimeZone(datetimeIn);
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
      String leader = edz[0];
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
      String datetime = edz[1];
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
      String zone = edz[2];
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
      if (datetime == null || zone == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
        return EMPTY_STR;
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
      String[] formatsIn = {dt, d};
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
      String formatOut = d;
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
      Date date = testFormats(datetime, formatsIn);
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
      if (date == null) return EMPTY_STR;
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
      SimpleDateFormat dateFormat = new SimpleDateFormat(formatOut);
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
      dateFormat.setLenient(false);
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
      String dateOut = dateFormat.format(date);
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
      if (dateOut.length() == 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
          return EMPTY_STR;
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
      else
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
        return (leader + dateOut + zone);
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
     * See above.
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
    public static String date()
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
      String datetime = dateTime().toString();
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
      String date = datetime.substring(0, datetime.indexOf("T"));
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
      String zone = datetime.substring(getZoneStart(datetime));
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
      return (date + zone);
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
     * The date:time function returns the time specified in the date/time string given
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
     * as the argument. If no argument is given, then the current local date/time, as
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
     * returned by date:date-time is used as a default argument.
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
     * The date/time string that's returned must be a string in the format defined as the
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
     * lexical representation of xs:dateTime in
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
     * <a href="http://www.w3.org/TR/xmlschema-2/#dateTime">[3.2.7 dateTime]</a> of
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
     * <a href="http://www.w3.org/TR/xmlschema-2/">[XML Schema Part 2: Datatypes]</a>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
     * If the argument string is not in this format, date:time returns an empty string ('').
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
     * The date/time format is basically CCYY-MM-DDThh:mm:ss, although implementers should consult
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
     * <a href="http://www.w3.org/TR/xmlschema-2/">[XML Schema Part 2: Datatypes]</a> and
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
     * <a href="http://www.iso.ch/markete/8601.pdf">[ISO 8601]</a> for details.
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
     * The date is returned as a string with a lexical representation as defined for xs:time in
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
     * <a href="http://www.w3.org/TR/xmlschema-2/#time">[3.2.8 time]</a> of [XML Schema Part 2: Datatypes].
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
     * The time format is basically hh:mm:ss, although implementers should consult [XML Schema Part 2:
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
     * Datatypes] and [ISO 8601] for details.
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
     * If no argument is given or the argument date/time specifies a time zone, then the time string
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
     * format must include a time zone, either a Z to indicate Coordinated Universal Time or a + or -
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
     * followed by the difference between the difference from UTC represented as hh:mm. If an argument
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
     * is specified and it does not specify a time zone, then the time string format must not include
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
     * a time zone.
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
    public static String time(String timeIn)
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
      throws ParseException
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
      String[] edz = getEraDatetimeZone(timeIn);
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
      String time = edz[1];
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
      String zone = edz[2];
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
      if (time == null || zone == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
        return EMPTY_STR;
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
      String[] formatsIn = {dt, d, t};
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
      String formatOut =  t;
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
      Date date = testFormats(time, formatsIn);
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
      if (date == null) return EMPTY_STR;
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
      SimpleDateFormat dateFormat = new SimpleDateFormat(formatOut);
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
      String out = dateFormat.format(date);
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
      return (out + zone);
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
     * See above.
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
    public static String time()
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
      String datetime = dateTime().toString();
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
      String time = datetime.substring(datetime.indexOf("T")+1);
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
          // The datetime() function returns the zone on the datetime string.  If we
7f561c08de6b Initial load
duke
parents:
diff changeset
   222
          // append it, we get the zone substring duplicated.
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
          // Fix for JIRA 2013
7f561c08de6b Initial load
duke
parents:
diff changeset
   224
7f561c08de6b Initial load
duke
parents:
diff changeset
   225
      // String zone = datetime.substring(getZoneStart(datetime));
7f561c08de6b Initial load
duke
parents:
diff changeset
   226
      // return (time + zone);
7f561c08de6b Initial load
duke
parents:
diff changeset
   227
      return (time);
7f561c08de6b Initial load
duke
parents:
diff changeset
   228
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   229
7f561c08de6b Initial load
duke
parents:
diff changeset
   230
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   231
     * The date:year function returns the year of a date as a number. If no
7f561c08de6b Initial load
duke
parents:
diff changeset
   232
     * argument is given, then the current local date/time, as returned by
7f561c08de6b Initial load
duke
parents:
diff changeset
   233
     * date:date-time is used as a default argument.
7f561c08de6b Initial load
duke
parents:
diff changeset
   234
     * The date/time string specified as the first argument must be a right-truncated
7f561c08de6b Initial load
duke
parents:
diff changeset
   235
     * string in the format defined as the lexical representation of xs:dateTime in one
7f561c08de6b Initial load
duke
parents:
diff changeset
   236
     * of the formats defined in
7f561c08de6b Initial load
duke
parents:
diff changeset
   237
     * <a href="http://www.w3.org/TR/xmlschema-2/">[XML Schema Part 2: Datatypes]</a>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   238
     * The permitted formats are as follows:
7f561c08de6b Initial load
duke
parents:
diff changeset
   239
     *   xs:dateTime (CCYY-MM-DDThh:mm:ss)
7f561c08de6b Initial load
duke
parents:
diff changeset
   240
     *   xs:date (CCYY-MM-DD)
7f561c08de6b Initial load
duke
parents:
diff changeset
   241
     *   xs:gYearMonth (CCYY-MM)
7f561c08de6b Initial load
duke
parents:
diff changeset
   242
     *   xs:gYear (CCYY)
7f561c08de6b Initial load
duke
parents:
diff changeset
   243
     * If the date/time string is not in one of these formats, then NaN is returned.
7f561c08de6b Initial load
duke
parents:
diff changeset
   244
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   245
    public static double year(String datetimeIn)
7f561c08de6b Initial load
duke
parents:
diff changeset
   246
      throws ParseException
7f561c08de6b Initial load
duke
parents:
diff changeset
   247
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   248
      String[] edz = getEraDatetimeZone(datetimeIn);
7f561c08de6b Initial load
duke
parents:
diff changeset
   249
      boolean ad = edz[0].length() == 0; // AD (Common Era -- empty leader)
7f561c08de6b Initial load
duke
parents:
diff changeset
   250
      String datetime = edz[1];
7f561c08de6b Initial load
duke
parents:
diff changeset
   251
      if (datetime == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   252
        return Double.NaN;
7f561c08de6b Initial load
duke
parents:
diff changeset
   253
7f561c08de6b Initial load
duke
parents:
diff changeset
   254
      String[] formats = {dt, d, gym, gy};
7f561c08de6b Initial load
duke
parents:
diff changeset
   255
      double yr = getNumber(datetime, formats, Calendar.YEAR);
7f561c08de6b Initial load
duke
parents:
diff changeset
   256
      if (ad || yr == Double.NaN)
7f561c08de6b Initial load
duke
parents:
diff changeset
   257
        return yr;
7f561c08de6b Initial load
duke
parents:
diff changeset
   258
      else
7f561c08de6b Initial load
duke
parents:
diff changeset
   259
        return -yr;
7f561c08de6b Initial load
duke
parents:
diff changeset
   260
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   261
7f561c08de6b Initial load
duke
parents:
diff changeset
   262
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   263
     * See above.
7f561c08de6b Initial load
duke
parents:
diff changeset
   264
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   265
    public static double year()
7f561c08de6b Initial load
duke
parents:
diff changeset
   266
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   267
      Calendar cal = Calendar.getInstance();
7f561c08de6b Initial load
duke
parents:
diff changeset
   268
      return cal.get(Calendar.YEAR);
7f561c08de6b Initial load
duke
parents:
diff changeset
   269
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   270
7f561c08de6b Initial load
duke
parents:
diff changeset
   271
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   272
     * The date:month-in-year function returns the month of a date as a number. If no argument
7f561c08de6b Initial load
duke
parents:
diff changeset
   273
     * is given, then the current local date/time, as returned by date:date-time is used
7f561c08de6b Initial load
duke
parents:
diff changeset
   274
     * as a default argument.
7f561c08de6b Initial load
duke
parents:
diff changeset
   275
     * The date/time string specified as the first argument is a left or right-truncated
7f561c08de6b Initial load
duke
parents:
diff changeset
   276
     * string in the format defined as the lexical representation of xs:dateTime in one of
7f561c08de6b Initial load
duke
parents:
diff changeset
   277
     * the formats defined in
7f561c08de6b Initial load
duke
parents:
diff changeset
   278
     * <a href="http://www.w3.org/TR/xmlschema-2/">[XML Schema Part 2: Datatypes]</a>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   279
     * The permitted formats are as follows:
7f561c08de6b Initial load
duke
parents:
diff changeset
   280
     *    xs:dateTime (CCYY-MM-DDThh:mm:ss)
7f561c08de6b Initial load
duke
parents:
diff changeset
   281
     *    xs:date (CCYY-MM-DD)
7f561c08de6b Initial load
duke
parents:
diff changeset
   282
     *    xs:gYearMonth (CCYY-MM)
7f561c08de6b Initial load
duke
parents:
diff changeset
   283
     *    xs:gMonth (--MM--)
7f561c08de6b Initial load
duke
parents:
diff changeset
   284
     *    xs:gMonthDay (--MM-DD)
7f561c08de6b Initial load
duke
parents:
diff changeset
   285
     * If the date/time string is not in one of these formats, then NaN is returned.
7f561c08de6b Initial load
duke
parents:
diff changeset
   286
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   287
    public static double monthInYear(String datetimeIn)
7f561c08de6b Initial load
duke
parents:
diff changeset
   288
      throws ParseException
7f561c08de6b Initial load
duke
parents:
diff changeset
   289
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   290
      String[] edz = getEraDatetimeZone(datetimeIn);
7f561c08de6b Initial load
duke
parents:
diff changeset
   291
      String datetime = edz[1];
7f561c08de6b Initial load
duke
parents:
diff changeset
   292
      if (datetime == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   293
        return Double.NaN;
7f561c08de6b Initial load
duke
parents:
diff changeset
   294
7f561c08de6b Initial load
duke
parents:
diff changeset
   295
      String[] formats = {dt, d, gym, gm, gmd};
7f561c08de6b Initial load
duke
parents:
diff changeset
   296
      return getNumber(datetime, formats, Calendar.MONTH) + 1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   297
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   298
7f561c08de6b Initial load
duke
parents:
diff changeset
   299
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   300
     * See above.
7f561c08de6b Initial load
duke
parents:
diff changeset
   301
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   302
    public static double monthInYear()
7f561c08de6b Initial load
duke
parents:
diff changeset
   303
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   304
      Calendar cal = Calendar.getInstance();
7f561c08de6b Initial load
duke
parents:
diff changeset
   305
      return cal.get(Calendar.MONTH) + 1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   306
   }
7f561c08de6b Initial load
duke
parents:
diff changeset
   307
7f561c08de6b Initial load
duke
parents:
diff changeset
   308
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   309
     * The date:week-in-year function returns the week of the year as a number. If no argument
7f561c08de6b Initial load
duke
parents:
diff changeset
   310
     * is given, then the current local date/time, as returned by date:date-time is used as the
7f561c08de6b Initial load
duke
parents:
diff changeset
   311
     * default argument. For the purposes of numbering, counting follows ISO 8601: week 1 in a year
7f561c08de6b Initial load
duke
parents:
diff changeset
   312
     * is the week containing the first Thursday of the year, with new weeks beginning on a Monday.
7f561c08de6b Initial load
duke
parents:
diff changeset
   313
     * The date/time string specified as the argument is a right-truncated string in the format
7f561c08de6b Initial load
duke
parents:
diff changeset
   314
     * defined as the lexical representation of xs:dateTime in one of the formats defined in
7f561c08de6b Initial load
duke
parents:
diff changeset
   315
     * <a href="http://www.w3.org/TR/xmlschema-2/">[XML Schema Part 2: Datatypes]</a>. The
7f561c08de6b Initial load
duke
parents:
diff changeset
   316
     * permitted formats are as follows:
7f561c08de6b Initial load
duke
parents:
diff changeset
   317
     *    xs:dateTime (CCYY-MM-DDThh:mm:ss)
7f561c08de6b Initial load
duke
parents:
diff changeset
   318
     *    xs:date (CCYY-MM-DD)
7f561c08de6b Initial load
duke
parents:
diff changeset
   319
     * If the date/time string is not in one of these formats, then NaN is returned.
7f561c08de6b Initial load
duke
parents:
diff changeset
   320
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   321
    public static double weekInYear(String datetimeIn)
7f561c08de6b Initial load
duke
parents:
diff changeset
   322
      throws ParseException
7f561c08de6b Initial load
duke
parents:
diff changeset
   323
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   324
      String[] edz = getEraDatetimeZone(datetimeIn);
7f561c08de6b Initial load
duke
parents:
diff changeset
   325
      String datetime = edz[1];
7f561c08de6b Initial load
duke
parents:
diff changeset
   326
      if (datetime == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   327
        return Double.NaN;
7f561c08de6b Initial load
duke
parents:
diff changeset
   328
7f561c08de6b Initial load
duke
parents:
diff changeset
   329
      String[] formats = {dt, d};
7f561c08de6b Initial load
duke
parents:
diff changeset
   330
      return getNumber(datetime, formats, Calendar.WEEK_OF_YEAR);
7f561c08de6b Initial load
duke
parents:
diff changeset
   331
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   332
7f561c08de6b Initial load
duke
parents:
diff changeset
   333
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   334
     * See above.
7f561c08de6b Initial load
duke
parents:
diff changeset
   335
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   336
    public static double weekInYear()
7f561c08de6b Initial load
duke
parents:
diff changeset
   337
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   338
       Calendar cal = Calendar.getInstance();
7f561c08de6b Initial load
duke
parents:
diff changeset
   339
      return cal.get(Calendar.WEEK_OF_YEAR);
7f561c08de6b Initial load
duke
parents:
diff changeset
   340
   }
7f561c08de6b Initial load
duke
parents:
diff changeset
   341
7f561c08de6b Initial load
duke
parents:
diff changeset
   342
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   343
     * The date:day-in-year function returns the day of a date in a year
7f561c08de6b Initial load
duke
parents:
diff changeset
   344
     * as a number. If no argument is given, then the current local
7f561c08de6b Initial load
duke
parents:
diff changeset
   345
     * date/time, as returned by date:date-time is used the default argument.
7f561c08de6b Initial load
duke
parents:
diff changeset
   346
     * The date/time string specified as the argument is a right-truncated
7f561c08de6b Initial load
duke
parents:
diff changeset
   347
     * string in the format defined as the lexical representation of xs:dateTime
7f561c08de6b Initial load
duke
parents:
diff changeset
   348
     * in one of the formats defined in
7f561c08de6b Initial load
duke
parents:
diff changeset
   349
     * <a href="http://www.w3.org/TR/xmlschema-2/">[XML Schema Part 2: Datatypes]</a>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   350
     * The permitted formats are as follows:
7f561c08de6b Initial load
duke
parents:
diff changeset
   351
     *     xs:dateTime (CCYY-MM-DDThh:mm:ss)
7f561c08de6b Initial load
duke
parents:
diff changeset
   352
     *     xs:date (CCYY-MM-DD)
7f561c08de6b Initial load
duke
parents:
diff changeset
   353
     * If the date/time string is not in one of these formats, then NaN is returned.
7f561c08de6b Initial load
duke
parents:
diff changeset
   354
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   355
    public static double dayInYear(String datetimeIn)
7f561c08de6b Initial load
duke
parents:
diff changeset
   356
      throws ParseException
7f561c08de6b Initial load
duke
parents:
diff changeset
   357
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   358
      String[] edz = getEraDatetimeZone(datetimeIn);
7f561c08de6b Initial load
duke
parents:
diff changeset
   359
      String datetime = edz[1];
7f561c08de6b Initial load
duke
parents:
diff changeset
   360
      if (datetime == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   361
        return Double.NaN;
7f561c08de6b Initial load
duke
parents:
diff changeset
   362
7f561c08de6b Initial load
duke
parents:
diff changeset
   363
      String[] formats = {dt, d};
7f561c08de6b Initial load
duke
parents:
diff changeset
   364
      return getNumber(datetime, formats, Calendar.DAY_OF_YEAR);
7f561c08de6b Initial load
duke
parents:
diff changeset
   365
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   366
7f561c08de6b Initial load
duke
parents:
diff changeset
   367
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   368
     * See above.
7f561c08de6b Initial load
duke
parents:
diff changeset
   369
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   370
    public static double dayInYear()
7f561c08de6b Initial load
duke
parents:
diff changeset
   371
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   372
       Calendar cal = Calendar.getInstance();
7f561c08de6b Initial load
duke
parents:
diff changeset
   373
      return cal.get(Calendar.DAY_OF_YEAR);
7f561c08de6b Initial load
duke
parents:
diff changeset
   374
   }
7f561c08de6b Initial load
duke
parents:
diff changeset
   375
7f561c08de6b Initial load
duke
parents:
diff changeset
   376
7f561c08de6b Initial load
duke
parents:
diff changeset
   377
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   378
     * The date:day-in-month function returns the day of a date as a number.
7f561c08de6b Initial load
duke
parents:
diff changeset
   379
     * If no argument is given, then the current local date/time, as returned
7f561c08de6b Initial load
duke
parents:
diff changeset
   380
     * by date:date-time is used the default argument.
7f561c08de6b Initial load
duke
parents:
diff changeset
   381
     * The date/time string specified as the argument is a left or right-truncated
7f561c08de6b Initial load
duke
parents:
diff changeset
   382
     * string in the format defined as the lexical representation of xs:dateTime
7f561c08de6b Initial load
duke
parents:
diff changeset
   383
     * in one of the formats defined in
7f561c08de6b Initial load
duke
parents:
diff changeset
   384
     * <a href="http://www.w3.org/TR/xmlschema-2/">[XML Schema Part 2: Datatypes]</a>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   385
     * The permitted formats are as follows:
7f561c08de6b Initial load
duke
parents:
diff changeset
   386
     *      xs:dateTime (CCYY-MM-DDThh:mm:ss)
7f561c08de6b Initial load
duke
parents:
diff changeset
   387
     *      xs:date (CCYY-MM-DD)
7f561c08de6b Initial load
duke
parents:
diff changeset
   388
     *      xs:gMonthDay (--MM-DD)
7f561c08de6b Initial load
duke
parents:
diff changeset
   389
     *      xs:gDay (---DD)
7f561c08de6b Initial load
duke
parents:
diff changeset
   390
     * If the date/time string is not in one of these formats, then NaN is returned.
7f561c08de6b Initial load
duke
parents:
diff changeset
   391
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   392
    public static double dayInMonth(String datetimeIn)
7f561c08de6b Initial load
duke
parents:
diff changeset
   393
      throws ParseException
7f561c08de6b Initial load
duke
parents:
diff changeset
   394
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   395
      String[] edz = getEraDatetimeZone(datetimeIn);
7f561c08de6b Initial load
duke
parents:
diff changeset
   396
      String datetime = edz[1];
7f561c08de6b Initial load
duke
parents:
diff changeset
   397
      String[] formats = {dt, d, gmd, gd};
7f561c08de6b Initial load
duke
parents:
diff changeset
   398
      double day = getNumber(datetime, formats, Calendar.DAY_OF_MONTH);
7f561c08de6b Initial load
duke
parents:
diff changeset
   399
      return day;
7f561c08de6b Initial load
duke
parents:
diff changeset
   400
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   401
7f561c08de6b Initial load
duke
parents:
diff changeset
   402
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   403
     * See above.
7f561c08de6b Initial load
duke
parents:
diff changeset
   404
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   405
    public static double dayInMonth()
7f561c08de6b Initial load
duke
parents:
diff changeset
   406
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   407
      Calendar cal = Calendar.getInstance();
7f561c08de6b Initial load
duke
parents:
diff changeset
   408
      return cal.get(Calendar.DAY_OF_MONTH);
7f561c08de6b Initial load
duke
parents:
diff changeset
   409
   }
7f561c08de6b Initial load
duke
parents:
diff changeset
   410
7f561c08de6b Initial load
duke
parents:
diff changeset
   411
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   412
     * The date:day-of-week-in-month function returns the day-of-the-week
7f561c08de6b Initial load
duke
parents:
diff changeset
   413
     * in a month of a date as a number (e.g. 3 for the 3rd Tuesday in May).
7f561c08de6b Initial load
duke
parents:
diff changeset
   414
     * If no argument is given, then the current local date/time, as returned
7f561c08de6b Initial load
duke
parents:
diff changeset
   415
     * by date:date-time is used the default argument.
7f561c08de6b Initial load
duke
parents:
diff changeset
   416
     * The date/time string specified as the argument is a right-truncated string
7f561c08de6b Initial load
duke
parents:
diff changeset
   417
     * in the format defined as the lexical representation of xs:dateTime in one
7f561c08de6b Initial load
duke
parents:
diff changeset
   418
     * of the formats defined in
7f561c08de6b Initial load
duke
parents:
diff changeset
   419
     * <a href="http://www.w3.org/TR/xmlschema-2/">[XML Schema Part 2: Datatypes]</a>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   420
     * The permitted formats are as follows:
7f561c08de6b Initial load
duke
parents:
diff changeset
   421
     *      xs:dateTime (CCYY-MM-DDThh:mm:ss)
7f561c08de6b Initial load
duke
parents:
diff changeset
   422
     *      xs:date (CCYY-MM-DD)
7f561c08de6b Initial load
duke
parents:
diff changeset
   423
     * If the date/time string is not in one of these formats, then NaN is returned.
7f561c08de6b Initial load
duke
parents:
diff changeset
   424
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   425
    public static double dayOfWeekInMonth(String datetimeIn)
7f561c08de6b Initial load
duke
parents:
diff changeset
   426
      throws ParseException
7f561c08de6b Initial load
duke
parents:
diff changeset
   427
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   428
      String[] edz = getEraDatetimeZone(datetimeIn);
7f561c08de6b Initial load
duke
parents:
diff changeset
   429
      String datetime = edz[1];
7f561c08de6b Initial load
duke
parents:
diff changeset
   430
      if (datetime == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   431
        return Double.NaN;
7f561c08de6b Initial load
duke
parents:
diff changeset
   432
7f561c08de6b Initial load
duke
parents:
diff changeset
   433
      String[] formats =  {dt, d};
7f561c08de6b Initial load
duke
parents:
diff changeset
   434
      return getNumber(datetime, formats, Calendar.DAY_OF_WEEK_IN_MONTH);
7f561c08de6b Initial load
duke
parents:
diff changeset
   435
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   436
7f561c08de6b Initial load
duke
parents:
diff changeset
   437
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   438
     * See above.
7f561c08de6b Initial load
duke
parents:
diff changeset
   439
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   440
    public static double dayOfWeekInMonth()
7f561c08de6b Initial load
duke
parents:
diff changeset
   441
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   442
       Calendar cal = Calendar.getInstance();
7f561c08de6b Initial load
duke
parents:
diff changeset
   443
      return cal.get(Calendar.DAY_OF_WEEK_IN_MONTH);
7f561c08de6b Initial load
duke
parents:
diff changeset
   444
   }
7f561c08de6b Initial load
duke
parents:
diff changeset
   445
7f561c08de6b Initial load
duke
parents:
diff changeset
   446
7f561c08de6b Initial load
duke
parents:
diff changeset
   447
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   448
     * The date:day-in-week function returns the day of the week given in a
7f561c08de6b Initial load
duke
parents:
diff changeset
   449
     * date as a number. If no argument is given, then the current local date/time,
7f561c08de6b Initial load
duke
parents:
diff changeset
   450
     * as returned by date:date-time is used the default argument.
7f561c08de6b Initial load
duke
parents:
diff changeset
   451
     * The date/time string specified as the argument is a right-truncated string
7f561c08de6b Initial load
duke
parents:
diff changeset
   452
     * in the format defined as the lexical representation of xs:dateTime in one
7f561c08de6b Initial load
duke
parents:
diff changeset
   453
     * of the formats defined in
7f561c08de6b Initial load
duke
parents:
diff changeset
   454
     * <a href="http://www.w3.org/TR/xmlschema-2/">[XML Schema Part 2: Datatypes]</a>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   455
     * The permitted formats are as follows:
7f561c08de6b Initial load
duke
parents:
diff changeset
   456
     *      xs:dateTime (CCYY-MM-DDThh:mm:ss)
7f561c08de6b Initial load
duke
parents:
diff changeset
   457
     *      xs:date (CCYY-MM-DD)
7f561c08de6b Initial load
duke
parents:
diff changeset
   458
     * If the date/time string is not in one of these formats, then NaN is returned.
7f561c08de6b Initial load
duke
parents:
diff changeset
   459
                            The numbering of days of the week starts at 1 for Sunday, 2 for Monday and so on up to 7 for Saturday.
7f561c08de6b Initial load
duke
parents:
diff changeset
   460
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   461
    public static double dayInWeek(String datetimeIn)
7f561c08de6b Initial load
duke
parents:
diff changeset
   462
      throws ParseException
7f561c08de6b Initial load
duke
parents:
diff changeset
   463
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   464
      String[] edz = getEraDatetimeZone(datetimeIn);
7f561c08de6b Initial load
duke
parents:
diff changeset
   465
      String datetime = edz[1];
7f561c08de6b Initial load
duke
parents:
diff changeset
   466
      if (datetime == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   467
        return Double.NaN;
7f561c08de6b Initial load
duke
parents:
diff changeset
   468
7f561c08de6b Initial load
duke
parents:
diff changeset
   469
      String[] formats = {dt, d};
7f561c08de6b Initial load
duke
parents:
diff changeset
   470
      return getNumber(datetime, formats, Calendar.DAY_OF_WEEK);
7f561c08de6b Initial load
duke
parents:
diff changeset
   471
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   472
7f561c08de6b Initial load
duke
parents:
diff changeset
   473
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   474
     * See above.
7f561c08de6b Initial load
duke
parents:
diff changeset
   475
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   476
    public static double dayInWeek()
7f561c08de6b Initial load
duke
parents:
diff changeset
   477
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   478
       Calendar cal = Calendar.getInstance();
7f561c08de6b Initial load
duke
parents:
diff changeset
   479
      return cal.get(Calendar.DAY_OF_WEEK);
7f561c08de6b Initial load
duke
parents:
diff changeset
   480
   }
7f561c08de6b Initial load
duke
parents:
diff changeset
   481
7f561c08de6b Initial load
duke
parents:
diff changeset
   482
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   483
     * The date:hour-in-day function returns the hour of the day as a number.
7f561c08de6b Initial load
duke
parents:
diff changeset
   484
     * If no argument is given, then the current local date/time, as returned
7f561c08de6b Initial load
duke
parents:
diff changeset
   485
     * by date:date-time is used the default argument.
7f561c08de6b Initial load
duke
parents:
diff changeset
   486
     * The date/time string specified as the argument is a right-truncated
7f561c08de6b Initial load
duke
parents:
diff changeset
   487
     * string  in the format defined as the lexical representation of xs:dateTime
7f561c08de6b Initial load
duke
parents:
diff changeset
   488
     * in one of the formats defined in
7f561c08de6b Initial load
duke
parents:
diff changeset
   489
     * <a href="http://www.w3.org/TR/xmlschema-2/">[XML Schema Part 2: Datatypes]</a>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   490
     * The permitted formats are as follows:
7f561c08de6b Initial load
duke
parents:
diff changeset
   491
     *     xs:dateTime (CCYY-MM-DDThh:mm:ss)
7f561c08de6b Initial load
duke
parents:
diff changeset
   492
     *     xs:time (hh:mm:ss)
7f561c08de6b Initial load
duke
parents:
diff changeset
   493
     * If the date/time string is not in one of these formats, then NaN is returned.
7f561c08de6b Initial load
duke
parents:
diff changeset
   494
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   495
    public static double hourInDay(String datetimeIn)
7f561c08de6b Initial load
duke
parents:
diff changeset
   496
      throws ParseException
7f561c08de6b Initial load
duke
parents:
diff changeset
   497
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   498
      String[] edz = getEraDatetimeZone(datetimeIn);
7f561c08de6b Initial load
duke
parents:
diff changeset
   499
      String datetime = edz[1];
7f561c08de6b Initial load
duke
parents:
diff changeset
   500
      if (datetime == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   501
        return Double.NaN;
7f561c08de6b Initial load
duke
parents:
diff changeset
   502
7f561c08de6b Initial load
duke
parents:
diff changeset
   503
      String[] formats = {dt, t};
7f561c08de6b Initial load
duke
parents:
diff changeset
   504
      return getNumber(datetime, formats, Calendar.HOUR_OF_DAY);
7f561c08de6b Initial load
duke
parents:
diff changeset
   505
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   506
7f561c08de6b Initial load
duke
parents:
diff changeset
   507
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   508
     * See above.
7f561c08de6b Initial load
duke
parents:
diff changeset
   509
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   510
    public static double hourInDay()
7f561c08de6b Initial load
duke
parents:
diff changeset
   511
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   512
       Calendar cal = Calendar.getInstance();
7f561c08de6b Initial load
duke
parents:
diff changeset
   513
      return cal.get(Calendar.HOUR_OF_DAY);
7f561c08de6b Initial load
duke
parents:
diff changeset
   514
   }
7f561c08de6b Initial load
duke
parents:
diff changeset
   515
7f561c08de6b Initial load
duke
parents:
diff changeset
   516
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   517
     * The date:minute-in-hour function returns the minute of the hour
7f561c08de6b Initial load
duke
parents:
diff changeset
   518
     * as a number. If no argument is given, then the current local
7f561c08de6b Initial load
duke
parents:
diff changeset
   519
     * date/time, as returned by date:date-time is used the default argument.
7f561c08de6b Initial load
duke
parents:
diff changeset
   520
     * The date/time string specified as the argument is a right-truncated
7f561c08de6b Initial load
duke
parents:
diff changeset
   521
     * string in the format defined as the lexical representation of xs:dateTime
7f561c08de6b Initial load
duke
parents:
diff changeset
   522
     * in one of the formats defined in
7f561c08de6b Initial load
duke
parents:
diff changeset
   523
     * <a href="http://www.w3.org/TR/xmlschema-2/">[XML Schema Part 2: Datatypes]</a>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   524
     * The permitted formats are as follows:
7f561c08de6b Initial load
duke
parents:
diff changeset
   525
     *      xs:dateTime (CCYY-MM-DDThh:mm:ss)
7f561c08de6b Initial load
duke
parents:
diff changeset
   526
     *      xs:time (hh:mm:ss)
7f561c08de6b Initial load
duke
parents:
diff changeset
   527
     * If the date/time string is not in one of these formats, then NaN is returned.
7f561c08de6b Initial load
duke
parents:
diff changeset
   528
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   529
    public static double minuteInHour(String datetimeIn)
7f561c08de6b Initial load
duke
parents:
diff changeset
   530
      throws ParseException
7f561c08de6b Initial load
duke
parents:
diff changeset
   531
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   532
      String[] edz = getEraDatetimeZone(datetimeIn);
7f561c08de6b Initial load
duke
parents:
diff changeset
   533
      String datetime = edz[1];
7f561c08de6b Initial load
duke
parents:
diff changeset
   534
      if (datetime == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   535
        return Double.NaN;
7f561c08de6b Initial load
duke
parents:
diff changeset
   536
7f561c08de6b Initial load
duke
parents:
diff changeset
   537
      String[] formats = {dt,t};
7f561c08de6b Initial load
duke
parents:
diff changeset
   538
      return getNumber(datetime, formats, Calendar.MINUTE);
7f561c08de6b Initial load
duke
parents:
diff changeset
   539
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   540
7f561c08de6b Initial load
duke
parents:
diff changeset
   541
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   542
     * See above.
7f561c08de6b Initial load
duke
parents:
diff changeset
   543
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   544
   public static double minuteInHour()
7f561c08de6b Initial load
duke
parents:
diff changeset
   545
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   546
       Calendar cal = Calendar.getInstance();
7f561c08de6b Initial load
duke
parents:
diff changeset
   547
      return cal.get(Calendar.MINUTE);
7f561c08de6b Initial load
duke
parents:
diff changeset
   548
   }
7f561c08de6b Initial load
duke
parents:
diff changeset
   549
7f561c08de6b Initial load
duke
parents:
diff changeset
   550
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   551
     * The date:second-in-minute function returns the second of the minute
7f561c08de6b Initial load
duke
parents:
diff changeset
   552
     * as a number. If no argument is given, then the current local
7f561c08de6b Initial load
duke
parents:
diff changeset
   553
     * date/time, as returned by date:date-time is used the default argument.
7f561c08de6b Initial load
duke
parents:
diff changeset
   554
     * The date/time string specified as the argument is a right-truncated
7f561c08de6b Initial load
duke
parents:
diff changeset
   555
     * string in the format defined as the lexical representation of xs:dateTime
7f561c08de6b Initial load
duke
parents:
diff changeset
   556
     * in one of the formats defined in
7f561c08de6b Initial load
duke
parents:
diff changeset
   557
     * <a href="http://www.w3.org/TR/xmlschema-2/">[XML Schema Part 2: Datatypes]</a>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   558
     * The permitted formats are as follows:
7f561c08de6b Initial load
duke
parents:
diff changeset
   559
     *      xs:dateTime (CCYY-MM-DDThh:mm:ss)
7f561c08de6b Initial load
duke
parents:
diff changeset
   560
     *      xs:time (hh:mm:ss)
7f561c08de6b Initial load
duke
parents:
diff changeset
   561
     * If the date/time string is not in one of these formats, then NaN is returned.
7f561c08de6b Initial load
duke
parents:
diff changeset
   562
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   563
    public static double secondInMinute(String datetimeIn)
7f561c08de6b Initial load
duke
parents:
diff changeset
   564
      throws ParseException
7f561c08de6b Initial load
duke
parents:
diff changeset
   565
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   566
      String[] edz = getEraDatetimeZone(datetimeIn);
7f561c08de6b Initial load
duke
parents:
diff changeset
   567
      String datetime = edz[1];
7f561c08de6b Initial load
duke
parents:
diff changeset
   568
      if (datetime == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   569
        return Double.NaN;
7f561c08de6b Initial load
duke
parents:
diff changeset
   570
7f561c08de6b Initial load
duke
parents:
diff changeset
   571
      String[] formats = {dt, t};
7f561c08de6b Initial load
duke
parents:
diff changeset
   572
      return getNumber(datetime, formats, Calendar.SECOND);
7f561c08de6b Initial load
duke
parents:
diff changeset
   573
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   574
7f561c08de6b Initial load
duke
parents:
diff changeset
   575
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   576
     * See above.
7f561c08de6b Initial load
duke
parents:
diff changeset
   577
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   578
    public static double secondInMinute()
7f561c08de6b Initial load
duke
parents:
diff changeset
   579
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   580
       Calendar cal = Calendar.getInstance();
7f561c08de6b Initial load
duke
parents:
diff changeset
   581
      return cal.get(Calendar.SECOND);
7f561c08de6b Initial load
duke
parents:
diff changeset
   582
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   583
7f561c08de6b Initial load
duke
parents:
diff changeset
   584
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   585
     * The date:leap-year function returns true if the year given in a date
7f561c08de6b Initial load
duke
parents:
diff changeset
   586
     * is a leap year. If no argument is given, then the current local
7f561c08de6b Initial load
duke
parents:
diff changeset
   587
     * date/time, as returned by date:date-time is used as a default argument.
7f561c08de6b Initial load
duke
parents:
diff changeset
   588
     * The date/time string specified as the first argument must be a
7f561c08de6b Initial load
duke
parents:
diff changeset
   589
     * right-truncated string in the format defined as the lexical representation
7f561c08de6b Initial load
duke
parents:
diff changeset
   590
     * of xs:dateTime in one of the formats defined in
7f561c08de6b Initial load
duke
parents:
diff changeset
   591
     * <a href="http://www.w3.org/TR/xmlschema-2/">[XML Schema Part 2: Datatypes]</a>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   592
     * The permitted formats are as follows:
7f561c08de6b Initial load
duke
parents:
diff changeset
   593
     *    xs:dateTime (CCYY-MM-DDThh:mm:ss)
7f561c08de6b Initial load
duke
parents:
diff changeset
   594
     *    xs:date (CCYY-MM-DD)
7f561c08de6b Initial load
duke
parents:
diff changeset
   595
     *    xs:gYearMonth (CCYY-MM)
7f561c08de6b Initial load
duke
parents:
diff changeset
   596
     *    xs:gYear (CCYY)
7f561c08de6b Initial load
duke
parents:
diff changeset
   597
     * If the date/time string is not in one of these formats, then NaN is returned.
7f561c08de6b Initial load
duke
parents:
diff changeset
   598
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   599
    public static XObject leapYear(String datetimeIn)
7f561c08de6b Initial load
duke
parents:
diff changeset
   600
      throws ParseException
7f561c08de6b Initial load
duke
parents:
diff changeset
   601
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   602
      String[] edz = getEraDatetimeZone(datetimeIn);
7f561c08de6b Initial load
duke
parents:
diff changeset
   603
      String datetime = edz[1];
7f561c08de6b Initial load
duke
parents:
diff changeset
   604
      if (datetime == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   605
        return new XNumber(Double.NaN);
7f561c08de6b Initial load
duke
parents:
diff changeset
   606
7f561c08de6b Initial load
duke
parents:
diff changeset
   607
      String[] formats = {dt, d, gym, gy};
7f561c08de6b Initial load
duke
parents:
diff changeset
   608
      double dbl = getNumber(datetime, formats, Calendar.YEAR);
7f561c08de6b Initial load
duke
parents:
diff changeset
   609
      if (dbl == Double.NaN)
7f561c08de6b Initial load
duke
parents:
diff changeset
   610
        return new XNumber(Double.NaN);
7f561c08de6b Initial load
duke
parents:
diff changeset
   611
      int yr = (int)dbl;
7f561c08de6b Initial load
duke
parents:
diff changeset
   612
      return new XBoolean(yr % 400 == 0 || (yr % 100 != 0 && yr % 4 == 0));
7f561c08de6b Initial load
duke
parents:
diff changeset
   613
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   614
7f561c08de6b Initial load
duke
parents:
diff changeset
   615
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   616
     * See above.
7f561c08de6b Initial load
duke
parents:
diff changeset
   617
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   618
    public static boolean leapYear()
7f561c08de6b Initial load
duke
parents:
diff changeset
   619
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   620
      Calendar cal = Calendar.getInstance();
7f561c08de6b Initial load
duke
parents:
diff changeset
   621
      int yr = (int)cal.get(Calendar.YEAR);
7f561c08de6b Initial load
duke
parents:
diff changeset
   622
      return (yr % 400 == 0 || (yr % 100 != 0 && yr % 4 == 0));
7f561c08de6b Initial load
duke
parents:
diff changeset
   623
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   624
7f561c08de6b Initial load
duke
parents:
diff changeset
   625
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   626
     * The date:month-name function returns the full name of the month of a date.
7f561c08de6b Initial load
duke
parents:
diff changeset
   627
     * If no argument is given, then the current local date/time, as returned by
7f561c08de6b Initial load
duke
parents:
diff changeset
   628
     * date:date-time is used the default argument.
7f561c08de6b Initial load
duke
parents:
diff changeset
   629
     * The date/time string specified as the argument is a left or right-truncated
7f561c08de6b Initial load
duke
parents:
diff changeset
   630
     * string in the format defined as the lexical representation of xs:dateTime in
7f561c08de6b Initial load
duke
parents:
diff changeset
   631
     *  one of the formats defined in
7f561c08de6b Initial load
duke
parents:
diff changeset
   632
     * <a href="http://www.w3.org/TR/xmlschema-2/">[XML Schema Part 2: Datatypes]</a>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   633
     * The permitted formats are as follows:
7f561c08de6b Initial load
duke
parents:
diff changeset
   634
     *    xs:dateTime (CCYY-MM-DDThh:mm:ss)
7f561c08de6b Initial load
duke
parents:
diff changeset
   635
     *    xs:date (CCYY-MM-DD)
7f561c08de6b Initial load
duke
parents:
diff changeset
   636
     *    xs:gYearMonth (CCYY-MM)
7f561c08de6b Initial load
duke
parents:
diff changeset
   637
     *    xs:gMonth (--MM--)
7f561c08de6b Initial load
duke
parents:
diff changeset
   638
     * If the date/time string is not in one of these formats, then an empty string ('')
7f561c08de6b Initial load
duke
parents:
diff changeset
   639
     * is returned.
7f561c08de6b Initial load
duke
parents:
diff changeset
   640
     * The result is an English month name: one of 'January', 'February', 'March',
7f561c08de6b Initial load
duke
parents:
diff changeset
   641
     * 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November'
7f561c08de6b Initial load
duke
parents:
diff changeset
   642
     * or 'December'.
7f561c08de6b Initial load
duke
parents:
diff changeset
   643
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   644
    public static String monthName(String datetimeIn)
7f561c08de6b Initial load
duke
parents:
diff changeset
   645
      throws ParseException
7f561c08de6b Initial load
duke
parents:
diff changeset
   646
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   647
      String[] edz = getEraDatetimeZone(datetimeIn);
7f561c08de6b Initial load
duke
parents:
diff changeset
   648
      String datetime = edz[1];
7f561c08de6b Initial load
duke
parents:
diff changeset
   649
      if (datetime == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   650
        return EMPTY_STR;
7f561c08de6b Initial load
duke
parents:
diff changeset
   651
7f561c08de6b Initial load
duke
parents:
diff changeset
   652
      String[] formatsIn = {dt, d, gym, gm};
7f561c08de6b Initial load
duke
parents:
diff changeset
   653
      String formatOut = "MMMM";
7f561c08de6b Initial load
duke
parents:
diff changeset
   654
      return getNameOrAbbrev(datetimeIn, formatsIn, formatOut);
7f561c08de6b Initial load
duke
parents:
diff changeset
   655
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   656
7f561c08de6b Initial load
duke
parents:
diff changeset
   657
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   658
     * See above.
7f561c08de6b Initial load
duke
parents:
diff changeset
   659
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   660
    public static String monthName()
7f561c08de6b Initial load
duke
parents:
diff changeset
   661
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   662
      Calendar cal = Calendar.getInstance();
7f561c08de6b Initial load
duke
parents:
diff changeset
   663
      String format = "MMMM";
7f561c08de6b Initial load
duke
parents:
diff changeset
   664
      return getNameOrAbbrev(format);
7f561c08de6b Initial load
duke
parents:
diff changeset
   665
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   666
7f561c08de6b Initial load
duke
parents:
diff changeset
   667
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   668
     * The date:month-abbreviation function returns the abbreviation of the month of
7f561c08de6b Initial load
duke
parents:
diff changeset
   669
     * a date. If no argument is given, then the current local date/time, as returned
7f561c08de6b Initial load
duke
parents:
diff changeset
   670
     * by date:date-time is used the default argument.
7f561c08de6b Initial load
duke
parents:
diff changeset
   671
     * The date/time string specified as the argument is a left or right-truncated
7f561c08de6b Initial load
duke
parents:
diff changeset
   672
     * string in the format defined as the lexical representation of xs:dateTime in
7f561c08de6b Initial load
duke
parents:
diff changeset
   673
     * one of the formats defined in
7f561c08de6b Initial load
duke
parents:
diff changeset
   674
     * <a href="http://www.w3.org/TR/xmlschema-2/">[XML Schema Part 2: Datatypes]</a>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   675
     * The permitted formats are as follows:
7f561c08de6b Initial load
duke
parents:
diff changeset
   676
     *    xs:dateTime (CCYY-MM-DDThh:mm:ss)
7f561c08de6b Initial load
duke
parents:
diff changeset
   677
     *    xs:date (CCYY-MM-DD)
7f561c08de6b Initial load
duke
parents:
diff changeset
   678
     *    xs:gYearMonth (CCYY-MM)
7f561c08de6b Initial load
duke
parents:
diff changeset
   679
     *    xs:gMonth (--MM--)
7f561c08de6b Initial load
duke
parents:
diff changeset
   680
     * If the date/time string is not in one of these formats, then an empty string ('')
7f561c08de6b Initial load
duke
parents:
diff changeset
   681
     * is returned.
7f561c08de6b Initial load
duke
parents:
diff changeset
   682
     * The result is a three-letter English month abbreviation: one of 'Jan', 'Feb', 'Mar',
7f561c08de6b Initial load
duke
parents:
diff changeset
   683
     * 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov' or 'Dec'.
7f561c08de6b Initial load
duke
parents:
diff changeset
   684
     * An implementation of this extension function in the EXSLT date namespace must conform
7f561c08de6b Initial load
duke
parents:
diff changeset
   685
     * to the behaviour described in this document.
7f561c08de6b Initial load
duke
parents:
diff changeset
   686
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   687
    public static String monthAbbreviation(String datetimeIn)
7f561c08de6b Initial load
duke
parents:
diff changeset
   688
      throws ParseException
7f561c08de6b Initial load
duke
parents:
diff changeset
   689
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   690
      String[] edz = getEraDatetimeZone(datetimeIn);
7f561c08de6b Initial load
duke
parents:
diff changeset
   691
      String datetime = edz[1];
7f561c08de6b Initial load
duke
parents:
diff changeset
   692
      if (datetime == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   693
        return EMPTY_STR;
7f561c08de6b Initial load
duke
parents:
diff changeset
   694
7f561c08de6b Initial load
duke
parents:
diff changeset
   695
      String[] formatsIn = {dt, d, gym, gm};
7f561c08de6b Initial load
duke
parents:
diff changeset
   696
      String formatOut = "MMM";
7f561c08de6b Initial load
duke
parents:
diff changeset
   697
      return getNameOrAbbrev(datetimeIn, formatsIn, formatOut);
7f561c08de6b Initial load
duke
parents:
diff changeset
   698
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   699
7f561c08de6b Initial load
duke
parents:
diff changeset
   700
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   701
     * See above.
7f561c08de6b Initial load
duke
parents:
diff changeset
   702
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   703
    public static String monthAbbreviation()
7f561c08de6b Initial load
duke
parents:
diff changeset
   704
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   705
      String format = "MMM";
7f561c08de6b Initial load
duke
parents:
diff changeset
   706
      return getNameOrAbbrev(format);
7f561c08de6b Initial load
duke
parents:
diff changeset
   707
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   708
7f561c08de6b Initial load
duke
parents:
diff changeset
   709
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   710
     * The date:day-name function returns the full name of the day of the week
7f561c08de6b Initial load
duke
parents:
diff changeset
   711
     * of a date.  If no argument is given, then the current local date/time,
7f561c08de6b Initial load
duke
parents:
diff changeset
   712
     * as returned by date:date-time is used the default argument.
7f561c08de6b Initial load
duke
parents:
diff changeset
   713
     * The date/time string specified as the argument is a left or right-truncated
7f561c08de6b Initial load
duke
parents:
diff changeset
   714
     * string in the format defined as the lexical representation of xs:dateTime
7f561c08de6b Initial load
duke
parents:
diff changeset
   715
     * in one of the formats defined in
7f561c08de6b Initial load
duke
parents:
diff changeset
   716
     * <a href="http://www.w3.org/TR/xmlschema-2/">[XML Schema Part 2: Datatypes]</a>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   717
     * The permitted formats are as follows:
7f561c08de6b Initial load
duke
parents:
diff changeset
   718
     *     xs:dateTime (CCYY-MM-DDThh:mm:ss)
7f561c08de6b Initial load
duke
parents:
diff changeset
   719
     *     xs:date (CCYY-MM-DD)
7f561c08de6b Initial load
duke
parents:
diff changeset
   720
     * If the date/time string is not in one of these formats, then the empty string ('')
7f561c08de6b Initial load
duke
parents:
diff changeset
   721
     * is returned.
7f561c08de6b Initial load
duke
parents:
diff changeset
   722
     * The result is an English day name: one of 'Sunday', 'Monday', 'Tuesday', 'Wednesday',
7f561c08de6b Initial load
duke
parents:
diff changeset
   723
     * 'Thursday' or 'Friday'.
7f561c08de6b Initial load
duke
parents:
diff changeset
   724
     * An implementation of this extension function in the EXSLT date namespace must conform
7f561c08de6b Initial load
duke
parents:
diff changeset
   725
     * to the behaviour described in this document.
7f561c08de6b Initial load
duke
parents:
diff changeset
   726
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   727
    public static String dayName(String datetimeIn)
7f561c08de6b Initial load
duke
parents:
diff changeset
   728
      throws ParseException
7f561c08de6b Initial load
duke
parents:
diff changeset
   729
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   730
      String[] edz = getEraDatetimeZone(datetimeIn);
7f561c08de6b Initial load
duke
parents:
diff changeset
   731
      String datetime = edz[1];
7f561c08de6b Initial load
duke
parents:
diff changeset
   732
      if (datetime == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   733
        return EMPTY_STR;
7f561c08de6b Initial load
duke
parents:
diff changeset
   734
7f561c08de6b Initial load
duke
parents:
diff changeset
   735
      String[] formatsIn = {dt, d};
7f561c08de6b Initial load
duke
parents:
diff changeset
   736
      String formatOut = "EEEE";
7f561c08de6b Initial load
duke
parents:
diff changeset
   737
      return getNameOrAbbrev(datetimeIn, formatsIn, formatOut);
7f561c08de6b Initial load
duke
parents:
diff changeset
   738
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   739
7f561c08de6b Initial load
duke
parents:
diff changeset
   740
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   741
     * See above.
7f561c08de6b Initial load
duke
parents:
diff changeset
   742
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   743
    public static String dayName()
7f561c08de6b Initial load
duke
parents:
diff changeset
   744
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   745
      String format = "EEEE";
7f561c08de6b Initial load
duke
parents:
diff changeset
   746
      return getNameOrAbbrev(format);
7f561c08de6b Initial load
duke
parents:
diff changeset
   747
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   748
7f561c08de6b Initial load
duke
parents:
diff changeset
   749
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   750
     * The date:day-abbreviation function returns the abbreviation of the day
7f561c08de6b Initial load
duke
parents:
diff changeset
   751
     * of the week of a date. If no argument is given, then the current local
7f561c08de6b Initial load
duke
parents:
diff changeset
   752
     * date/time, as returned  by date:date-time is used the default argument.
7f561c08de6b Initial load
duke
parents:
diff changeset
   753
     * The date/time string specified as the argument is a left or right-truncated
7f561c08de6b Initial load
duke
parents:
diff changeset
   754
     * string in the format defined as the lexical representation of xs:dateTime
7f561c08de6b Initial load
duke
parents:
diff changeset
   755
     * in one of the formats defined in
7f561c08de6b Initial load
duke
parents:
diff changeset
   756
     * <a href="http://www.w3.org/TR/xmlschema-2/">[XML Schema Part 2: Datatypes]</a>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   757
     * The permitted formats are as follows:
7f561c08de6b Initial load
duke
parents:
diff changeset
   758
     *     xs:dateTime (CCYY-MM-DDThh:mm:ss)
7f561c08de6b Initial load
duke
parents:
diff changeset
   759
     *     xs:date (CCYY-MM-DD)
7f561c08de6b Initial load
duke
parents:
diff changeset
   760
     * If the date/time string is not in one of these formats, then the empty string
7f561c08de6b Initial load
duke
parents:
diff changeset
   761
     * ('') is returned.
7f561c08de6b Initial load
duke
parents:
diff changeset
   762
     * The result is a three-letter English day abbreviation: one of 'Sun', 'Mon', 'Tue',
7f561c08de6b Initial load
duke
parents:
diff changeset
   763
     * 'Wed', 'Thu' or 'Fri'.
7f561c08de6b Initial load
duke
parents:
diff changeset
   764
     * An implementation of this extension function in the EXSLT date namespace must conform
7f561c08de6b Initial load
duke
parents:
diff changeset
   765
     * to the behaviour described in this document.
7f561c08de6b Initial load
duke
parents:
diff changeset
   766
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   767
    public static String dayAbbreviation(String datetimeIn)
7f561c08de6b Initial load
duke
parents:
diff changeset
   768
      throws ParseException
7f561c08de6b Initial load
duke
parents:
diff changeset
   769
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   770
      String[] edz = getEraDatetimeZone(datetimeIn);
7f561c08de6b Initial load
duke
parents:
diff changeset
   771
      String datetime = edz[1];
7f561c08de6b Initial load
duke
parents:
diff changeset
   772
      if (datetime == null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   773
        return EMPTY_STR;
7f561c08de6b Initial load
duke
parents:
diff changeset
   774
7f561c08de6b Initial load
duke
parents:
diff changeset
   775
      String[] formatsIn = {dt, d};
7f561c08de6b Initial load
duke
parents:
diff changeset
   776
      String formatOut = "EEE";
7f561c08de6b Initial load
duke
parents:
diff changeset
   777
      return getNameOrAbbrev(datetimeIn, formatsIn, formatOut);
7f561c08de6b Initial load
duke
parents:
diff changeset
   778
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   779
7f561c08de6b Initial load
duke
parents:
diff changeset
   780
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   781
     * See above.
7f561c08de6b Initial load
duke
parents:
diff changeset
   782
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   783
    public static String dayAbbreviation()
7f561c08de6b Initial load
duke
parents:
diff changeset
   784
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   785
      String format = "EEE";
7f561c08de6b Initial load
duke
parents:
diff changeset
   786
      return getNameOrAbbrev(format);
7f561c08de6b Initial load
duke
parents:
diff changeset
   787
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   788
7f561c08de6b Initial load
duke
parents:
diff changeset
   789
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   790
     * Returns an array with the 3 components that a datetime input string
7f561c08de6b Initial load
duke
parents:
diff changeset
   791
     * may contain: - (for BC era), datetime, and zone. If the zone is not
7f561c08de6b Initial load
duke
parents:
diff changeset
   792
     * valid, return null for that component.
7f561c08de6b Initial load
duke
parents:
diff changeset
   793
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   794
    private static String[] getEraDatetimeZone(String in)
7f561c08de6b Initial load
duke
parents:
diff changeset
   795
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   796
      String leader = "";
7f561c08de6b Initial load
duke
parents:
diff changeset
   797
      String datetime = in;
7f561c08de6b Initial load
duke
parents:
diff changeset
   798
      String zone = "";
7f561c08de6b Initial load
duke
parents:
diff changeset
   799
      if (in.charAt(0)=='-' && !in.startsWith("--"))
7f561c08de6b Initial load
duke
parents:
diff changeset
   800
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
   801
        leader = "-"; //  '+' is implicit , not allowed
7f561c08de6b Initial load
duke
parents:
diff changeset
   802
        datetime = in.substring(1);
7f561c08de6b Initial load
duke
parents:
diff changeset
   803
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   804
      int z = getZoneStart(datetime);
7f561c08de6b Initial load
duke
parents:
diff changeset
   805
      if (z > 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
   806
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
   807
        zone = datetime.substring(z);
7f561c08de6b Initial load
duke
parents:
diff changeset
   808
        datetime = datetime.substring(0, z);
7f561c08de6b Initial load
duke
parents:
diff changeset
   809
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   810
      else if (z == -2)
7f561c08de6b Initial load
duke
parents:
diff changeset
   811
        zone = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   812
      //System.out.println("'" + leader + "' " + datetime + " " + zone);
7f561c08de6b Initial load
duke
parents:
diff changeset
   813
      return new String[]{leader, datetime, zone};
7f561c08de6b Initial load
duke
parents:
diff changeset
   814
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   815
7f561c08de6b Initial load
duke
parents:
diff changeset
   816
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   817
     * Get the start of zone information if the input ends
7f561c08de6b Initial load
duke
parents:
diff changeset
   818
     * with 'Z' or +/-hh:mm. If a zone string is not
7f561c08de6b Initial load
duke
parents:
diff changeset
   819
     * found, return -1; if the zone string is invalid,
7f561c08de6b Initial load
duke
parents:
diff changeset
   820
     * return -2.
7f561c08de6b Initial load
duke
parents:
diff changeset
   821
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   822
    private static int getZoneStart (String datetime)
7f561c08de6b Initial load
duke
parents:
diff changeset
   823
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   824
      if (datetime.indexOf("Z") == datetime.length()-1)
7f561c08de6b Initial load
duke
parents:
diff changeset
   825
        return datetime.length()-1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   826
      else if (datetime.length() >=6
7f561c08de6b Initial load
duke
parents:
diff changeset
   827
                && datetime.charAt(datetime.length()-3) == ':'
7f561c08de6b Initial load
duke
parents:
diff changeset
   828
                && (datetime.charAt(datetime.length()-6) == '+'
7f561c08de6b Initial load
duke
parents:
diff changeset
   829
                    || datetime.charAt(datetime.length()-6) == '-'))
7f561c08de6b Initial load
duke
parents:
diff changeset
   830
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
   831
        try
7f561c08de6b Initial load
duke
parents:
diff changeset
   832
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   833
          SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
7f561c08de6b Initial load
duke
parents:
diff changeset
   834
          dateFormat.setLenient(false);
7f561c08de6b Initial load
duke
parents:
diff changeset
   835
          Date d = dateFormat.parse(datetime.substring(datetime.length() -5));
7f561c08de6b Initial load
duke
parents:
diff changeset
   836
          return datetime.length()-6;
7f561c08de6b Initial load
duke
parents:
diff changeset
   837
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   838
        catch (ParseException pe)
7f561c08de6b Initial load
duke
parents:
diff changeset
   839
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   840
          System.out.println("ParseException " + pe.getErrorOffset());
7f561c08de6b Initial load
duke
parents:
diff changeset
   841
          return -2; // Invalid.
7f561c08de6b Initial load
duke
parents:
diff changeset
   842
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   843
7f561c08de6b Initial load
duke
parents:
diff changeset
   844
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   845
        return -1; // No zone information.
7f561c08de6b Initial load
duke
parents:
diff changeset
   846
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   847
7f561c08de6b Initial load
duke
parents:
diff changeset
   848
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   849
     * Attempt to parse an input string with the allowed formats, returning
7f561c08de6b Initial load
duke
parents:
diff changeset
   850
     * null if none of the formats work.
7f561c08de6b Initial load
duke
parents:
diff changeset
   851
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   852
    private static Date testFormats (String in, String[] formats)
7f561c08de6b Initial load
duke
parents:
diff changeset
   853
      throws ParseException
7f561c08de6b Initial load
duke
parents:
diff changeset
   854
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   855
      for (int i = 0; i <formats.length; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
   856
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
   857
        try
7f561c08de6b Initial load
duke
parents:
diff changeset
   858
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   859
          SimpleDateFormat dateFormat = new SimpleDateFormat(formats[i]);
7f561c08de6b Initial load
duke
parents:
diff changeset
   860
          dateFormat.setLenient(false);
7f561c08de6b Initial load
duke
parents:
diff changeset
   861
          return dateFormat.parse(in);
7f561c08de6b Initial load
duke
parents:
diff changeset
   862
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   863
        catch (ParseException pe)
7f561c08de6b Initial load
duke
parents:
diff changeset
   864
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   865
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   866
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   867
      return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   868
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   869
7f561c08de6b Initial load
duke
parents:
diff changeset
   870
7f561c08de6b Initial load
duke
parents:
diff changeset
   871
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   872
     * Parse the input string and return the corresponding calendar field
7f561c08de6b Initial load
duke
parents:
diff changeset
   873
     * number.
7f561c08de6b Initial load
duke
parents:
diff changeset
   874
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   875
    private static double getNumber(String in, String[] formats, int calField)
7f561c08de6b Initial load
duke
parents:
diff changeset
   876
      throws ParseException
7f561c08de6b Initial load
duke
parents:
diff changeset
   877
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   878
      Calendar cal = Calendar.getInstance();
7f561c08de6b Initial load
duke
parents:
diff changeset
   879
      cal.setLenient(false);
7f561c08de6b Initial load
duke
parents:
diff changeset
   880
      // Try the allowed formats, from longest to shortest.
7f561c08de6b Initial load
duke
parents:
diff changeset
   881
      Date date = testFormats(in, formats);
7f561c08de6b Initial load
duke
parents:
diff changeset
   882
      if (date == null) return Double.NaN;
7f561c08de6b Initial load
duke
parents:
diff changeset
   883
      cal.setTime(date);
7f561c08de6b Initial load
duke
parents:
diff changeset
   884
      return cal.get(calField);
7f561c08de6b Initial load
duke
parents:
diff changeset
   885
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   886
7f561c08de6b Initial load
duke
parents:
diff changeset
   887
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   888
     *  Get the full name or abbreviation of the month or day.
7f561c08de6b Initial load
duke
parents:
diff changeset
   889
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   890
    private static String getNameOrAbbrev(String in,
7f561c08de6b Initial load
duke
parents:
diff changeset
   891
                                         String[] formatsIn,
7f561c08de6b Initial load
duke
parents:
diff changeset
   892
                                         String formatOut)
7f561c08de6b Initial load
duke
parents:
diff changeset
   893
      throws ParseException
7f561c08de6b Initial load
duke
parents:
diff changeset
   894
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   895
      for (int i = 0; i <formatsIn.length; i++) // from longest to shortest.
7f561c08de6b Initial load
duke
parents:
diff changeset
   896
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
   897
        try
7f561c08de6b Initial load
duke
parents:
diff changeset
   898
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   899
          SimpleDateFormat dateFormat = new SimpleDateFormat(formatsIn[i], Locale.ENGLISH);
7f561c08de6b Initial load
duke
parents:
diff changeset
   900
          dateFormat.setLenient(false);
7f561c08de6b Initial load
duke
parents:
diff changeset
   901
          Date dt = dateFormat.parse(in);
7f561c08de6b Initial load
duke
parents:
diff changeset
   902
          dateFormat.applyPattern(formatOut);
7f561c08de6b Initial load
duke
parents:
diff changeset
   903
          return dateFormat.format(dt);
7f561c08de6b Initial load
duke
parents:
diff changeset
   904
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   905
        catch (ParseException pe)
7f561c08de6b Initial load
duke
parents:
diff changeset
   906
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   907
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   908
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   909
      return "";
7f561c08de6b Initial load
duke
parents:
diff changeset
   910
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   911
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   912
     * Get the full name or abbreviation for the current month or day
7f561c08de6b Initial load
duke
parents:
diff changeset
   913
     * (no input string).
7f561c08de6b Initial load
duke
parents:
diff changeset
   914
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   915
    private static String getNameOrAbbrev(String format)
7f561c08de6b Initial load
duke
parents:
diff changeset
   916
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   917
      Calendar cal = Calendar.getInstance();
7f561c08de6b Initial load
duke
parents:
diff changeset
   918
      SimpleDateFormat dateFormat = new SimpleDateFormat(format, Locale.ENGLISH);
7f561c08de6b Initial load
duke
parents:
diff changeset
   919
      return dateFormat.format(cal.getTime());
7f561c08de6b Initial load
duke
parents:
diff changeset
   920
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   921
7f561c08de6b Initial load
duke
parents:
diff changeset
   922
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   923
     * The date:format-date function formats a date/time according to a pattern.
7f561c08de6b Initial load
duke
parents:
diff changeset
   924
     * <p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   925
     * The first argument to date:format-date specifies the date/time to be
7f561c08de6b Initial load
duke
parents:
diff changeset
   926
     * formatted. It must be right or left-truncated date/time strings in one of
7f561c08de6b Initial load
duke
parents:
diff changeset
   927
     * the formats defined in
7f561c08de6b Initial load
duke
parents:
diff changeset
   928
     * <a href="http://www.w3.org/TR/xmlschema-2/">[XML Schema Part 2: Datatypes]</a>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   929
     * The permitted formats are as follows:
7f561c08de6b Initial load
duke
parents:
diff changeset
   930
     * <ul>
7f561c08de6b Initial load
duke
parents:
diff changeset
   931
     * <li>xs:dateTime (CCYY-MM-DDThh:mm:ss)
7f561c08de6b Initial load
duke
parents:
diff changeset
   932
     * <li>xs:date (CCYY-MM-DD)
7f561c08de6b Initial load
duke
parents:
diff changeset
   933
     * <li>xs:time (hh:mm:ss)
7f561c08de6b Initial load
duke
parents:
diff changeset
   934
     * <li>xs:gYearMonth (CCYY-MM)
7f561c08de6b Initial load
duke
parents:
diff changeset
   935
     * <li>xs:gYear (CCYY)
7f561c08de6b Initial load
duke
parents:
diff changeset
   936
     * <li>xs:gMonthDay (--MM-DD)
7f561c08de6b Initial load
duke
parents:
diff changeset
   937
     * <li>xs:gMonth (--MM--)
7f561c08de6b Initial load
duke
parents:
diff changeset
   938
     * <li>xs:gDay (---DD)
7f561c08de6b Initial load
duke
parents:
diff changeset
   939
     * </ul>
7f561c08de6b Initial load
duke
parents:
diff changeset
   940
     * The second argument is a string that gives the format pattern used to
7f561c08de6b Initial load
duke
parents:
diff changeset
   941
     * format the date. The format pattern must be in the syntax specified by
7f561c08de6b Initial load
duke
parents:
diff changeset
   942
     * the JDK 1.1 SimpleDateFormat class. The format pattern string is
7f561c08de6b Initial load
duke
parents:
diff changeset
   943
     * interpreted as described for the JDK 1.1 SimpleDateFormat class.
7f561c08de6b Initial load
duke
parents:
diff changeset
   944
     * <p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   945
     * If the date/time format is right-truncated (i.e. in a format other than
7f561c08de6b Initial load
duke
parents:
diff changeset
   946
     * xs:time, or xs:dateTime) then any missing components are assumed to be as
7f561c08de6b Initial load
duke
parents:
diff changeset
   947
     * follows: if no month is specified, it is given a month of 01; if no day
7f561c08de6b Initial load
duke
parents:
diff changeset
   948
     * is specified, it is given a day of 01; if no time is specified, it is
7f561c08de6b Initial load
duke
parents:
diff changeset
   949
     * given a time of 00:00:00.
7f561c08de6b Initial load
duke
parents:
diff changeset
   950
     * <p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   951
     * If the date/time format is left-truncated (i.e. xs:time, xs:gMonthDay,
7f561c08de6b Initial load
duke
parents:
diff changeset
   952
     * xs:gMonth or xs:gDay) and the format pattern has a token that uses a
7f561c08de6b Initial load
duke
parents:
diff changeset
   953
     * component that is missing from the date/time format used, then that token
7f561c08de6b Initial load
duke
parents:
diff changeset
   954
     * is replaced with an empty string ('') within the result.
7f561c08de6b Initial load
duke
parents:
diff changeset
   955
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   956
     * The author is Helg Bredow (helg.bredow@kalido.com)
7f561c08de6b Initial load
duke
parents:
diff changeset
   957
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   958
    public static String formatDate(String dateTime, String pattern)
7f561c08de6b Initial load
duke
parents:
diff changeset
   959
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   960
        final String yearSymbols = "Gy";
7f561c08de6b Initial load
duke
parents:
diff changeset
   961
        final String monthSymbols = "M";
7f561c08de6b Initial load
duke
parents:
diff changeset
   962
        final String daySymbols = "dDEFwW";
7f561c08de6b Initial load
duke
parents:
diff changeset
   963
        TimeZone timeZone;
7f561c08de6b Initial load
duke
parents:
diff changeset
   964
        String zone;
7f561c08de6b Initial load
duke
parents:
diff changeset
   965
7f561c08de6b Initial load
duke
parents:
diff changeset
   966
        // Get the timezone information if it was supplied and modify the
7f561c08de6b Initial load
duke
parents:
diff changeset
   967
        // dateTime so that SimpleDateFormat will understand it.
7f561c08de6b Initial load
duke
parents:
diff changeset
   968
        if (dateTime.endsWith("Z") || dateTime.endsWith("z"))
7f561c08de6b Initial load
duke
parents:
diff changeset
   969
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   970
            timeZone = TimeZone.getTimeZone("GMT");
7f561c08de6b Initial load
duke
parents:
diff changeset
   971
            dateTime = dateTime.substring(0, dateTime.length()-1) + "GMT";
7f561c08de6b Initial load
duke
parents:
diff changeset
   972
            zone = "z";
7f561c08de6b Initial load
duke
parents:
diff changeset
   973
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   974
        else if ((dateTime.length() >= 6)
7f561c08de6b Initial load
duke
parents:
diff changeset
   975
                 && (dateTime.charAt(dateTime.length()-3) == ':')
7f561c08de6b Initial load
duke
parents:
diff changeset
   976
                 && ((dateTime.charAt(dateTime.length()-6) == '+')
7f561c08de6b Initial load
duke
parents:
diff changeset
   977
                    || (dateTime.charAt(dateTime.length()-6) == '-')))
7f561c08de6b Initial load
duke
parents:
diff changeset
   978
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   979
            String offset = dateTime.substring(dateTime.length()-6);
7f561c08de6b Initial load
duke
parents:
diff changeset
   980
7f561c08de6b Initial load
duke
parents:
diff changeset
   981
            if ("+00:00".equals(offset) || "-00:00".equals(offset))
7f561c08de6b Initial load
duke
parents:
diff changeset
   982
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
   983
                timeZone = TimeZone.getTimeZone("GMT");
7f561c08de6b Initial load
duke
parents:
diff changeset
   984
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   985
            else
7f561c08de6b Initial load
duke
parents:
diff changeset
   986
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
   987
                timeZone = TimeZone.getTimeZone("GMT" + offset);
7f561c08de6b Initial load
duke
parents:
diff changeset
   988
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   989
            zone = "z";
7f561c08de6b Initial load
duke
parents:
diff changeset
   990
            // Need to adjust it since SimpleDateFormat requires GMT+hh:mm but
7f561c08de6b Initial load
duke
parents:
diff changeset
   991
            // we have +hh:mm.
7f561c08de6b Initial load
duke
parents:
diff changeset
   992
            dateTime = dateTime.substring(0, dateTime.length()-6) + "GMT" + offset;
7f561c08de6b Initial load
duke
parents:
diff changeset
   993
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   994
        else
7f561c08de6b Initial load
duke
parents:
diff changeset
   995
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   996
            // Assume local time.
7f561c08de6b Initial load
duke
parents:
diff changeset
   997
            timeZone = TimeZone.getDefault();
7f561c08de6b Initial load
duke
parents:
diff changeset
   998
            zone = "";
7f561c08de6b Initial load
duke
parents:
diff changeset
   999
            // Leave off the timezone since SimpleDateFormat will assume local
7f561c08de6b Initial load
duke
parents:
diff changeset
  1000
            // time if time zone is not included.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1001
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1002
        String[] formats = {dt + zone, d, gym, gy};
7f561c08de6b Initial load
duke
parents:
diff changeset
  1003
7f561c08de6b Initial load
duke
parents:
diff changeset
  1004
        // Try the time format first. We need to do this to prevent
7f561c08de6b Initial load
duke
parents:
diff changeset
  1005
        // SimpleDateFormat from interpreting a time as a year. i.e we just need
7f561c08de6b Initial load
duke
parents:
diff changeset
  1006
        // to check if it's a time before we check it's a year.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1007
        try
7f561c08de6b Initial load
duke
parents:
diff changeset
  1008
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1009
            SimpleDateFormat inFormat = new SimpleDateFormat(t + zone);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1010
            inFormat.setLenient(false);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1011
            Date d= inFormat.parse(dateTime);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1012
            SimpleDateFormat outFormat = new SimpleDateFormat(strip
7f561c08de6b Initial load
duke
parents:
diff changeset
  1013
                (yearSymbols + monthSymbols + daySymbols, pattern));
7f561c08de6b Initial load
duke
parents:
diff changeset
  1014
            outFormat.setTimeZone(timeZone);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1015
            return outFormat.format(d);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1016
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1017
        catch (ParseException pe)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1018
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1019
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1020
7f561c08de6b Initial load
duke
parents:
diff changeset
  1021
        // Try the right truncated formats.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1022
        for (int i = 0; i < formats.length; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1023
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1024
            try
7f561c08de6b Initial load
duke
parents:
diff changeset
  1025
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1026
                SimpleDateFormat inFormat = new SimpleDateFormat(formats[i]);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1027
                inFormat.setLenient(false);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1028
                Date d = inFormat.parse(dateTime);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1029
                SimpleDateFormat outFormat = new SimpleDateFormat(pattern);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1030
                outFormat.setTimeZone(timeZone);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1031
                return outFormat.format(d);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1032
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1033
            catch (ParseException pe)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1034
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1035
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1036
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1037
7f561c08de6b Initial load
duke
parents:
diff changeset
  1038
        // Now try the left truncated ones. The Java format() function doesn't
7f561c08de6b Initial load
duke
parents:
diff changeset
  1039
        // return the correct strings in this case. We strip any pattern
7f561c08de6b Initial load
duke
parents:
diff changeset
  1040
        // symbols that shouldn't be output so that they are not defaulted to
7f561c08de6b Initial load
duke
parents:
diff changeset
  1041
        // inappropriate values in the output.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1042
        try
7f561c08de6b Initial load
duke
parents:
diff changeset
  1043
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1044
            SimpleDateFormat inFormat = new SimpleDateFormat(gmd);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1045
            inFormat.setLenient(false);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1046
            Date d = inFormat.parse(dateTime);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1047
            SimpleDateFormat outFormat = new SimpleDateFormat(strip(yearSymbols, pattern));
7f561c08de6b Initial load
duke
parents:
diff changeset
  1048
            outFormat.setTimeZone(timeZone);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1049
            return outFormat.format(d);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1050
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1051
        catch (ParseException pe)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1052
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1053
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1054
        try
7f561c08de6b Initial load
duke
parents:
diff changeset
  1055
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1056
            SimpleDateFormat inFormat = new SimpleDateFormat(gm);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1057
            inFormat.setLenient(false);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1058
            Date d = inFormat.parse(dateTime);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1059
            SimpleDateFormat outFormat = new SimpleDateFormat(strip(yearSymbols, pattern));
7f561c08de6b Initial load
duke
parents:
diff changeset
  1060
            outFormat.setTimeZone(timeZone);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1061
            return outFormat.format(d);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1062
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1063
        catch (ParseException pe)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1064
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1065
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1066
        try
7f561c08de6b Initial load
duke
parents:
diff changeset
  1067
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1068
            SimpleDateFormat inFormat = new SimpleDateFormat(gd);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1069
            inFormat.setLenient(false);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1070
            Date d = inFormat.parse(dateTime);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1071
            SimpleDateFormat outFormat = new SimpleDateFormat(strip(yearSymbols + monthSymbols, pattern));
7f561c08de6b Initial load
duke
parents:
diff changeset
  1072
            outFormat.setTimeZone(timeZone);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1073
            return outFormat.format(d);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1074
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1075
        catch (ParseException pe)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1076
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1077
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1078
        return EMPTY_STR;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1079
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1080
7f561c08de6b Initial load
duke
parents:
diff changeset
  1081
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1082
     * Strips occurrences of the given character from a date format pattern.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1083
     * @param symbols list of symbols to strip.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1084
     * @param pattern
7f561c08de6b Initial load
duke
parents:
diff changeset
  1085
     * @return
7f561c08de6b Initial load
duke
parents:
diff changeset
  1086
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1087
    private static String strip(String symbols, String pattern)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1088
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1089
        int quoteSemaphore = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1090
        int i = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1091
        StringBuffer result = new StringBuffer(pattern.length());
7f561c08de6b Initial load
duke
parents:
diff changeset
  1092
7f561c08de6b Initial load
duke
parents:
diff changeset
  1093
        while (i < pattern.length())
7f561c08de6b Initial load
duke
parents:
diff changeset
  1094
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1095
            char ch = pattern.charAt(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1096
            if (ch == '\'')
7f561c08de6b Initial load
duke
parents:
diff changeset
  1097
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1098
                // Assume it's an openening quote so simply copy the quoted
7f561c08de6b Initial load
duke
parents:
diff changeset
  1099
                // text to the result. There is nothing to strip here.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1100
                int endQuote = pattern.indexOf('\'', i + 1);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1101
                if (endQuote == -1)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1102
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1103
                    endQuote = pattern.length();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1104
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1105
                result.append(pattern.substring(i, endQuote));
7f561c08de6b Initial load
duke
parents:
diff changeset
  1106
                i = endQuote++;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1107
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1108
            else if (symbols.indexOf(ch) > -1)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1109
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1110
                // The char needs to be stripped.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1111
                i++;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1112
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1113
            else
7f561c08de6b Initial load
duke
parents:
diff changeset
  1114
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1115
                result.append(ch);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1116
                i++;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1117
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1118
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1119
        return result.toString();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1120
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1121
7f561c08de6b Initial load
duke
parents:
diff changeset
  1122
}