001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package java.sql;
019:
020: import java.text.SimpleDateFormat;
021: import java.util.Date;
022:
023: /**
024: * Java representation of an SQL TIME value. Provides functions to aid
025: * generation and interpretation of JDBC escape format for time values.
026: *
027: */
028: public class Time extends Date {
029:
030: private static final long serialVersionUID = 8397324403548013681L;
031:
032: /**
033: * @deprecated Please use the constructor Time( long ) Constructs a Time
034: * object using the supplied values for Hour, Minute and Second.
035: * The Year, Month and Day elements of the Time object are set
036: * to 1970, January, 1 reflecting the Epoch (Time in
037: * milliseconds = 0).
038: * <p>
039: * Any attempt to access the Year, Month or Day elements of a
040: * Time object will result in an IllegalArgumentException.
041: * <p>
042: * Result is undefined if any argument is out of bounds.
043: * @param theHour
044: * a value from 0 - 23
045: * @param theMinute
046: * a value from 0 - 59
047: * @param theSecond
048: * a value from 0 - 59
049: */
050: @SuppressWarnings("deprecation")
051: @Deprecated
052: public Time(int theHour, int theMinute, int theSecond) {
053: super (70, 0, 1, theHour, theMinute, theSecond);
054: }
055:
056: /**
057: * Constructs a Time object using a supplied time specified in milliseconds
058: *
059: * @param theTime
060: * a Time specified in milliseconds since the Epoch (January 1st
061: * 1970, 00:00:00.000)
062: */
063: public Time(long theTime) {
064: super (theTime);
065: }
066:
067: /**
068: * @deprecated This method is deprecated and must not be used. An SQL Time
069: * object does not have a Date component.
070: * @return does not return
071: * @throws IllegalArgumentException
072: * if this method is called
073: */
074: @SuppressWarnings("deprecation")
075: @Deprecated
076: @Override
077: public int getDate() {
078: throw new IllegalArgumentException();
079: }
080:
081: /**
082: * @deprecated This method is deprecated and must not be used. An SQL Time
083: * object does not have a Day component.
084: * @return does not return
085: * @throws IllegalArgumentException
086: * if this method is called
087: */
088: @SuppressWarnings("deprecation")
089: @Deprecated
090: @Override
091: public int getDay() {
092: throw new IllegalArgumentException();
093: }
094:
095: /**
096: * @deprecated This method is deprecated and must not be used. An SQL Time
097: * object does not have a Month component.
098: * @return does not return
099: * @throws IllegalArgumentException
100: * if this method is called
101: */
102: @SuppressWarnings("deprecation")
103: @Deprecated
104: @Override
105: public int getMonth() {
106: throw new IllegalArgumentException();
107: }
108:
109: /**
110: * @deprecated This method is deprecated and must not be used. An SQL Time
111: * object does not have a Year component.
112: * @return does not return
113: * @throws IllegalArgumentException
114: * if this method is called
115: */
116: @SuppressWarnings("deprecation")
117: @Deprecated
118: @Override
119: public int getYear() {
120: throw new IllegalArgumentException();
121: }
122:
123: /**
124: * @deprecated This method is deprecated and must not be used. An SQL Time
125: * object does not have a Date component.
126: * @throws IllegalArgumentException
127: * if this method is called
128: */
129: @SuppressWarnings("deprecation")
130: @Deprecated
131: @Override
132: public void setDate(int i) {
133: throw new IllegalArgumentException();
134: }
135:
136: /**
137: * @deprecated This method is deprecated and must not be used. An SQL Time
138: * object does not have a Month component.
139: * @throws IllegalArgumentException
140: * if this method is called
141: */
142: @SuppressWarnings("deprecation")
143: @Deprecated
144: @Override
145: public void setMonth(int i) {
146: throw new IllegalArgumentException();
147: }
148:
149: /**
150: * @deprecated This method is deprecated and must not be used. An SQL Time
151: * object does not have a Year component.
152: * @throws IllegalArgumentException
153: * if this method is called
154: */
155: @SuppressWarnings("deprecation")
156: @Deprecated
157: @Override
158: public void setYear(int i) {
159: throw new IllegalArgumentException();
160: }
161:
162: /**
163: * Sets the time for this Time object to the supplied milliseconds value.
164: *
165: * @param time
166: * A time value expressed as milliseconds since the Epoch.
167: * Negative values are milliseconds before the Epoch. The Epoch
168: * is January 1 1970, 00:00:00.000
169: */
170: @Override
171: public void setTime(long time) {
172: super .setTime(time);
173: }
174:
175: /**
176: * Formats the Time as a String in JDBC escape format: hh:mm:ss
177: *
178: * @return A String representing the Time value in JDBC escape format:
179: * HH:mm:ss
180: */
181: @Override
182: public String toString() {
183: SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); //$NON-NLS-1$
184: return dateFormat.format(this );
185: }
186:
187: /**
188: * Creates a Time object from a String holding a time represented in JDBC
189: * escape format: hh:mm:ss.
190: * <p>
191: * An exception occurs if the input string is not in the form of a time in
192: * JDBC escape format.
193: *
194: * @param timeString
195: * A String representing the time value in JDBC escape format:
196: * hh:mm:ss
197: * @return The Time object set to a time corresponding to the given time
198: * @throws IllegalArgumentException
199: * if the supplied time string is not in JDBC escape format.
200: */
201: public static Time valueOf(String timeString) {
202: if (timeString == null) {
203: throw new IllegalArgumentException();
204: }
205: int firstIndex = timeString.indexOf(':');
206: int secondIndex = timeString.indexOf(':', firstIndex + 1);
207: // secondIndex == -1 means none or only one separator '-' has been
208: // found.
209: // The string is separated into three parts by two separator characters,
210: // if the first or the third part is null string, we should throw
211: // IllegalArgumentException to follow RI
212: if (secondIndex == -1 || firstIndex == 0
213: || secondIndex + 1 == timeString.length()) {
214: throw new IllegalArgumentException();
215: }
216: // parse each part of the string
217: int hour = Integer
218: .parseInt(timeString.substring(0, firstIndex));
219: int minute = Integer.parseInt(timeString.substring(
220: firstIndex + 1, secondIndex));
221: int second = Integer.parseInt(timeString.substring(
222: secondIndex + 1, timeString.length()));
223: return new Time(hour, minute, second);
224: }
225: }
|