001 /*
002 * Copyright 1996-2004 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 the <code>java.util.Date</code> class that allows the JDBC
030 * API to identify this as an SQL <code>TIME</code> value. The <code>Time</code>
031 * class adds formatting and
032 * parsing operations to support the JDBC escape syntax for time
033 * values.
034 * <p>The date components should be set to the "zero epoch"
035 * value of January 1, 1970 and should not be accessed.
036 */
037 public class Time extends java.util.Date {
038
039 /**
040 * Constructs a <code>Time</code> object initialized with the
041 * given values for the hour, minute, and second.
042 * The driver sets the date components to January 1, 1970.
043 * Any method that attempts to access the date components of a
044 * <code>Time</code> object will throw a
045 * <code>java.lang.IllegalArgumentException</code>.
046 * <P>
047 * The result is undefined if a given argument is out of bounds.
048 *
049 * @param hour 0 to 23
050 * @param minute 0 to 59
051 * @param second 0 to 59
052 *
053 * @deprecated Use the constructor that takes a milliseconds value
054 * in place of this constructor
055 */
056 @Deprecated
057 public Time(int hour, int minute, int second) {
058 super (70, 0, 1, hour, minute, second);
059 }
060
061 /**
062 * Constructs a <code>Time</code> object using a milliseconds time value.
063 *
064 * @param time milliseconds since January 1, 1970, 00:00:00 GMT;
065 * a negative number is milliseconds before
066 * January 1, 1970, 00:00:00 GMT
067 */
068 public Time(long time) {
069 super (time);
070 }
071
072 /**
073 * Sets a <code>Time</code> object using a milliseconds time value.
074 *
075 * @param time milliseconds since January 1, 1970, 00:00:00 GMT;
076 * a negative number is milliseconds before
077 * January 1, 1970, 00:00:00 GMT
078 */
079 public void setTime(long time) {
080 super .setTime(time);
081 }
082
083 /**
084 * Converts a string in JDBC time escape format to a <code>Time</code> value.
085 *
086 * @param s time in format "hh:mm:ss"
087 * @return a corresponding <code>Time</code> object
088 */
089 public static Time valueOf(String s) {
090 int hour;
091 int minute;
092 int second;
093 int firstColon;
094 int secondColon;
095
096 if (s == null)
097 throw new java.lang.IllegalArgumentException();
098
099 firstColon = s.indexOf(':');
100 secondColon = s.indexOf(':', firstColon + 1);
101 if ((firstColon > 0) & (secondColon > 0)
102 & (secondColon < s.length() - 1)) {
103 hour = Integer.parseInt(s.substring(0, firstColon));
104 minute = Integer.parseInt(s.substring(firstColon + 1,
105 secondColon));
106 second = Integer.parseInt(s.substring(secondColon + 1));
107 } else {
108 throw new java.lang.IllegalArgumentException();
109 }
110
111 return new Time(hour, minute, second);
112 }
113
114 /**
115 * Formats a time in JDBC time escape format.
116 *
117 * @return a <code>String</code> in hh:mm:ss format
118 */
119 public String toString() {
120 int hour = super .getHours();
121 int minute = super .getMinutes();
122 int second = super .getSeconds();
123 String hourString;
124 String minuteString;
125 String secondString;
126
127 if (hour < 10) {
128 hourString = "0" + hour;
129 } else {
130 hourString = Integer.toString(hour);
131 }
132 if (minute < 10) {
133 minuteString = "0" + minute;
134 } else {
135 minuteString = Integer.toString(minute);
136 }
137 if (second < 10) {
138 secondString = "0" + second;
139 } else {
140 secondString = Integer.toString(second);
141 }
142 return (hourString + ":" + minuteString + ":" + secondString);
143 }
144
145 // Override all the date operations inherited from java.util.Date;
146
147 /**
148 * This method is deprecated and should not be used because SQL <code>TIME</code>
149 * values do not have a year component.
150 *
151 * @deprecated
152 * @exception java.lang.IllegalArgumentException if this
153 * method is invoked
154 * @see #setYear
155 */
156 @Deprecated
157 public int getYear() {
158 throw new java.lang.IllegalArgumentException();
159 }
160
161 /**
162 * This method is deprecated and should not be used because SQL <code>TIME</code>
163 * values do not have a month component.
164 *
165 * @deprecated
166 * @exception java.lang.IllegalArgumentException if this
167 * method is invoked
168 * @see #setMonth
169 */
170 @Deprecated
171 public int getMonth() {
172 throw new java.lang.IllegalArgumentException();
173 }
174
175 /**
176 * This method is deprecated and should not be used because SQL <code>TIME</code>
177 * values do not have a day component.
178 *
179 * @deprecated
180 * @exception java.lang.IllegalArgumentException if this
181 * method is invoked
182 */
183 @Deprecated
184 public int getDay() {
185 throw new java.lang.IllegalArgumentException();
186 }
187
188 /**
189 * This method is deprecated and should not be used because SQL <code>TIME</code>
190 * values do not have a date component.
191 *
192 * @deprecated
193 * @exception java.lang.IllegalArgumentException if this
194 * method is invoked
195 * @see #setDate
196 */
197 @Deprecated
198 public int getDate() {
199 throw new java.lang.IllegalArgumentException();
200 }
201
202 /**
203 * This method is deprecated and should not be used because SQL <code>TIME</code>
204 * values do not have a year component.
205 *
206 * @deprecated
207 * @exception java.lang.IllegalArgumentException if this
208 * method is invoked
209 * @see #getYear
210 */
211 @Deprecated
212 public void setYear(int i) {
213 throw new java.lang.IllegalArgumentException();
214 }
215
216 /**
217 * This method is deprecated and should not be used because SQL <code>TIME</code>
218 * values do not have a month component.
219 *
220 * @deprecated
221 * @exception java.lang.IllegalArgumentException if this
222 * method is invoked
223 * @see #getMonth
224 */
225 @Deprecated
226 public void setMonth(int i) {
227 throw new java.lang.IllegalArgumentException();
228 }
229
230 /**
231 * This method is deprecated and should not be used because SQL <code>TIME</code>
232 * values do not have a date component.
233 *
234 * @deprecated
235 * @exception java.lang.IllegalArgumentException if this
236 * method is invoked
237 * @see #getDate
238 */
239 @Deprecated
240 public void setDate(int i) {
241 throw new java.lang.IllegalArgumentException();
242 }
243
244 /**
245 * Private serial version unique ID to ensure serialization
246 * compatibility.
247 */
248 static final long serialVersionUID = 8397324403548013681L;
249 }
|