001 /*
002 * Copyright 1996-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.sql;
027
028 /**
029 * <P>A thin wrapper around a millisecond value that allows
030 * JDBC to identify this as an SQL <code>DATE</code> value. A
031 * milliseconds value represents the number of milliseconds that
032 * have passed since January 1, 1970 00:00:00.000 GMT.
033 * <p>
034 * To conform with the definition of SQL <code>DATE</code>, the
035 * millisecond values wrapped by a <code>java.sql.Date</code> instance
036 * must be 'normalized' by setting the
037 * hours, minutes, seconds, and milliseconds to zero in the particular
038 * time zone with which the instance is associated.
039 */
040 public class Date extends java.util.Date {
041
042 /**
043 * Constructs a <code>Date</code> object initialized with the given
044 * year, month, and day.
045 * <P>
046 * The result is undefined if a given argument is out of bounds.
047 *
048 * @param year the year minus 1900; must be 0 to 8099. (Note that
049 * 8099 is 9999 minus 1900.)
050 * @param month 0 to 11
051 * @param day 1 to 31
052 * @deprecated instead use the constructor <code>Date(long date)</code>
053 */
054 public Date(int year, int month, int day) {
055 super (year, month, day);
056 }
057
058 /**
059 * Constructs a <code>Date</code> object using the given milliseconds
060 * time value. If the given milliseconds value contains time
061 * information, the driver will set the time components to the
062 * time in the default time zone (the time zone of the Java virtual
063 * machine running the application) that corresponds to zero GMT.
064 *
065 * @param date milliseconds since January 1, 1970, 00:00:00 GMT not
066 * to exceed the milliseconds representation for the year 8099.
067 * A negative number indicates the number of milliseconds
068 * before January 1, 1970, 00:00:00 GMT.
069 */
070 public Date(long date) {
071 // If the millisecond date value contains time info, mask it out.
072 super (date);
073
074 }
075
076 /**
077 * Sets an existing <code>Date</code> object
078 * using the given milliseconds time value.
079 * If the given milliseconds value contains time information,
080 * the driver will set the time components to the
081 * time in the default time zone (the time zone of the Java virtual
082 * machine running the application) that corresponds to zero GMT.
083 *
084 * @param date milliseconds since January 1, 1970, 00:00:00 GMT not
085 * to exceed the milliseconds representation for the year 8099.
086 * A negative number indicates the number of milliseconds
087 * before January 1, 1970, 00:00:00 GMT.
088 */
089 public void setTime(long date) {
090 // If the millisecond date value contains time info, mask it out.
091 super .setTime(date);
092 }
093
094 /**
095 * Converts a string in JDBC date escape format to
096 * a <code>Date</code> value.
097 *
098 * @param s a <code>String</code> object representing a date in
099 * in the format "yyyy-mm-dd"
100 * @return a <code>java.sql.Date</code> object representing the
101 * given date
102 * @throws IllegalArgumentException if the date given is not in the
103 * JDBC date escape format (yyyy-mm-dd)
104 */
105 public static Date valueOf(String s) {
106 int year;
107 int month;
108 int day;
109 int firstDash;
110 int secondDash;
111
112 if (s == null)
113 throw new java.lang.IllegalArgumentException();
114
115 firstDash = s.indexOf('-');
116 secondDash = s.indexOf('-', firstDash + 1);
117 if ((firstDash > 0) & (secondDash > 0)
118 & (secondDash < s.length() - 1)) {
119 year = Integer.parseInt(s.substring(0, firstDash)) - 1900;
120 month = Integer.parseInt(s.substring(firstDash + 1,
121 secondDash)) - 1;
122 day = Integer.parseInt(s.substring(secondDash + 1));
123 } else {
124 throw new java.lang.IllegalArgumentException();
125 }
126
127 return new Date(year, month, day);
128 }
129
130 /**
131 * Formats a date in the date escape format yyyy-mm-dd.
132 * <P>
133 * @return a String in yyyy-mm-dd format
134 */
135 public String toString() {
136 int year = super .getYear() + 1900;
137 int month = super .getMonth() + 1;
138 int day = super .getDate();
139
140 char buf[] = "2000-00-00".toCharArray();
141 buf[0] = Character.forDigit(year / 1000, 10);
142 buf[1] = Character.forDigit((year / 100) % 10, 10);
143 buf[2] = Character.forDigit((year / 10) % 10, 10);
144 buf[3] = Character.forDigit(year % 10, 10);
145 buf[5] = Character.forDigit(month / 10, 10);
146 buf[6] = Character.forDigit(month % 10, 10);
147 buf[8] = Character.forDigit(day / 10, 10);
148 buf[9] = Character.forDigit(day % 10, 10);
149
150 return new String(buf);
151 }
152
153 // Override all the time operations inherited from java.util.Date;
154
155 /**
156 * This method is deprecated and should not be used because SQL Date
157 * values do not have a time component.
158 *
159 * @deprecated
160 * @exception java.lang.IllegalArgumentException if this method is invoked
161 * @see #setHours
162 */
163 public int getHours() {
164 throw new java.lang.IllegalArgumentException();
165 }
166
167 /**
168 * This method is deprecated and should not be used because SQL Date
169 * values do not have a time component.
170 *
171 * @deprecated
172 * @exception java.lang.IllegalArgumentException if this method is invoked
173 * @see #setMinutes
174 */
175 public int getMinutes() {
176 throw new java.lang.IllegalArgumentException();
177 }
178
179 /**
180 * This method is deprecated and should not be used because SQL Date
181 * values do not have a time component.
182 *
183 * @deprecated
184 * @exception java.lang.IllegalArgumentException if this method is invoked
185 * @see #setSeconds
186 */
187 public int getSeconds() {
188 throw new java.lang.IllegalArgumentException();
189 }
190
191 /**
192 * This method is deprecated and should not be used because SQL Date
193 * values do not have a time component.
194 *
195 * @deprecated
196 * @exception java.lang.IllegalArgumentException if this method is invoked
197 * @see #getHours
198 */
199 public void setHours(int i) {
200 throw new java.lang.IllegalArgumentException();
201 }
202
203 /**
204 * This method is deprecated and should not be used because SQL Date
205 * values do not have a time component.
206 *
207 * @deprecated
208 * @exception java.lang.IllegalArgumentException if this method is invoked
209 * @see #getMinutes
210 */
211 public void setMinutes(int i) {
212 throw new java.lang.IllegalArgumentException();
213 }
214
215 /**
216 * This method is deprecated and should not be used because SQL Date
217 * values do not have a time component.
218 *
219 * @deprecated
220 * @exception java.lang.IllegalArgumentException if this method is invoked
221 * @see #getSeconds
222 */
223 public void setSeconds(int i) {
224 throw new java.lang.IllegalArgumentException();
225 }
226
227 /**
228 * Private serial version unique ID to ensure serialization
229 * compatibility.
230 */
231 static final long serialVersionUID = 1511598038487230103L;
232 }
|