jdk/src/share/classes/java/sql/Date.java
author chegar
Tue, 12 May 2009 16:32:34 +0100
changeset 3450 2f08a8bb9b83
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6801071: Remote sites can compromise user privacy and possibly hijack web sessions Reviewed-by: jccollet, hawtin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1996-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.sql;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * <P>A thin wrapper around a millisecond value that allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * JDBC to identify this as an SQL <code>DATE</code> value.  A
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * milliseconds value represents the number of milliseconds that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * have passed since January 1, 1970 00:00:00.000 GMT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * To conform with the definition of SQL <code>DATE</code>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * millisecond values wrapped by a <code>java.sql.Date</code> instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * must be 'normalized' by setting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * hours, minutes, seconds, and milliseconds to zero in the particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * time zone with which the instance is associated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public class Date extends java.util.Date {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     * Constructs a <code>Date</code> object initialized with the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     * year, month, and day.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * The result is undefined if a given argument is out of bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * @param year the year minus 1900; must be 0 to 8099. (Note that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     *        8099 is 9999 minus 1900.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * @param month 0 to 11
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * @param day 1 to 31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * @deprecated instead use the constructor <code>Date(long date)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public Date(int year, int month, int day) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        super(year, month, day);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * Constructs a <code>Date</code> object using the given milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * time value.  If the given milliseconds value contains time
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * information, the driver will set the time components to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * time in the default time zone (the time zone of the Java virtual
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * machine running the application) that corresponds to zero GMT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * @param date milliseconds since January 1, 1970, 00:00:00 GMT not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *        to exceed the milliseconds representation for the year 8099.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     *        A negative number indicates the number of milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     *        before January 1, 1970, 00:00:00 GMT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public Date(long date) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        // If the millisecond date value contains time info, mask it out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        super(date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * Sets an existing <code>Date</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * using the given milliseconds time value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * If the given milliseconds value contains time information,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * the driver will set the time components to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * time in the default time zone (the time zone of the Java virtual
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * machine running the application) that corresponds to zero GMT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @param date milliseconds since January 1, 1970, 00:00:00 GMT not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *        to exceed the milliseconds representation for the year 8099.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *        A negative number indicates the number of milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *        before January 1, 1970, 00:00:00 GMT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public void setTime(long date) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        // If the millisecond date value contains time info, mask it out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        super.setTime(date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Converts a string in JDBC date escape format to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * a <code>Date</code> value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @param s a <code>String</code> object representing a date in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *        in the format "yyyy-mm-dd"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @return a <code>java.sql.Date</code> object representing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *         given date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @throws IllegalArgumentException if the date given is not in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *         JDBC date escape format (yyyy-mm-dd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public static Date valueOf(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        int year;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        int month;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        int day;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        int firstDash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        int secondDash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (s == null) throw new java.lang.IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        firstDash = s.indexOf('-');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        secondDash = s.indexOf('-', firstDash+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if ((firstDash > 0) & (secondDash > 0) & (secondDash < s.length()-1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            year = Integer.parseInt(s.substring(0, firstDash)) - 1900;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            month = Integer.parseInt(s.substring(firstDash+1, secondDash)) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            day = Integer.parseInt(s.substring(secondDash+1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            throw new java.lang.IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        return new Date(year, month, day);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Formats a date in the date escape format yyyy-mm-dd.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @return a String in yyyy-mm-dd format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public String toString () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        int year = super.getYear() + 1900;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        int month = super.getMonth() + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        int day = super.getDate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        char buf[] = "2000-00-00".toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        buf[0] = Character.forDigit(year/1000,10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        buf[1] = Character.forDigit((year/100)%10,10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        buf[2] = Character.forDigit((year/10)%10,10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        buf[3] = Character.forDigit(year%10,10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        buf[5] = Character.forDigit(month/10,10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        buf[6] = Character.forDigit(month%10,10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        buf[8] = Character.forDigit(day/10,10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        buf[9] = Character.forDigit(day%10,10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        return new String(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    // Override all the time operations inherited from java.util.Date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    * This method is deprecated and should not be used because SQL Date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    * values do not have a time component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    * @deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    * @exception java.lang.IllegalArgumentException if this method is invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    * @see #setHours
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public int getHours() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        throw new java.lang.IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    * This method is deprecated and should not be used because SQL Date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    * values do not have a time component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    * @deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    * @exception java.lang.IllegalArgumentException if this method is invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    * @see #setMinutes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    public int getMinutes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        throw new java.lang.IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    * This method is deprecated and should not be used because SQL Date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    * values do not have a time component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    * @deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    * @exception java.lang.IllegalArgumentException if this method is invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    * @see #setSeconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public int getSeconds() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        throw new java.lang.IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    * This method is deprecated and should not be used because SQL Date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    * values do not have a time component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    * @deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    * @exception java.lang.IllegalArgumentException if this method is invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    * @see #getHours
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    public void setHours(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        throw new java.lang.IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    * This method is deprecated and should not be used because SQL Date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    * values do not have a time component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    * @deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    * @exception java.lang.IllegalArgumentException if this method is invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    * @see #getMinutes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    public void setMinutes(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        throw new java.lang.IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    * This method is deprecated and should not be used because SQL Date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    * values do not have a time component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    * @deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    * @exception java.lang.IllegalArgumentException if this method is invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    * @see #getSeconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    public void setSeconds(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        throw new java.lang.IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    * Private serial version unique ID to ensure serialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    * compatibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    static final long serialVersionUID = 1511598038487230103L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
}