KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > sql > Time


1 /*
2  * @(#)Time.java 1.32 04/05/18
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.sql;
9
10 /**
11  * <P>A thin wrapper around the <code>java.util.Date</code> class that allows the JDBC
12  * API to identify this as an SQL <code>TIME</code> value. The <code>Time</code>
13  * class adds formatting and
14  * parsing operations to support the JDBC escape syntax for time
15  * values.
16  * <p>The date components should be set to the "zero epoch"
17  * value of January 1, 1970 and should not be accessed.
18  */

19 public class Time extends java.util.Date JavaDoc {
20
21     /**
22      * Constructs a <code>Time</code> object initialized with the
23      * given values for the hour, minute, and second.
24      * The driver sets the date components to January 1, 1970.
25      * Any method that attempts to access the date components of a
26      * <code>Time</code> object will throw a
27      * <code>java.lang.IllegalArgumentException</code>.
28      * <P>
29      * The result is undefined if a given argument is out of bounds.
30      *
31      * @param hour 0 to 23
32      * @param minute 0 to 59
33      * @param second 0 to 59
34      *
35      * @deprecated Use the constructor that takes a milliseconds value
36      * in place of this constructor
37      */

38     @Deprecated JavaDoc
39     public Time(int hour, int minute, int second) {
40     super(70, 0, 1, hour, minute, second);
41     }
42    
43     /**
44      * Constructs a <code>Time</code> object using a milliseconds time value.
45      *
46      * @param time milliseconds since January 1, 1970, 00:00:00 GMT;
47      * a negative number is milliseconds before
48      * January 1, 1970, 00:00:00 GMT
49      */

50     public Time(long time) {
51     super(time);
52     }
53
54     /**
55      * Sets a <code>Time</code> object using a milliseconds time value.
56      *
57      * @param time milliseconds since January 1, 1970, 00:00:00 GMT;
58      * a negative number is milliseconds before
59      * January 1, 1970, 00:00:00 GMT
60      */

61     public void setTime(long time) {
62     super.setTime(time);
63     }
64
65     /**
66      * Converts a string in JDBC time escape format to a <code>Time</code> value.
67      *
68      * @param s time in format "hh:mm:ss"
69      * @return a corresponding <code>Time</code> object
70      */

71     public static Time JavaDoc valueOf(String JavaDoc s) {
72     int hour;
73     int minute;
74     int second;
75     int firstColon;
76     int secondColon;
77
78     if (s == null) throw new java.lang.IllegalArgumentException JavaDoc();
79
80     firstColon = s.indexOf(':');
81     secondColon = s.indexOf(':', firstColon+1);
82     if ((firstColon > 0) & (secondColon > 0) &
83         (secondColon < s.length()-1)) {
84         hour = Integer.parseInt(s.substring(0, firstColon));
85         minute =
86         Integer.parseInt(s.substring(firstColon+1, secondColon));
87         second = Integer.parseInt(s.substring(secondColon+1));
88     } else {
89         throw new java.lang.IllegalArgumentException JavaDoc();
90     }
91
92     return new Time JavaDoc(hour, minute, second);
93     }
94    
95     /**
96      * Formats a time in JDBC time escape format.
97      *
98      * @return a <code>String</code> in hh:mm:ss format
99      */

100     public String JavaDoc toString () {
101     int hour = super.getHours();
102     int minute = super.getMinutes();
103     int second = super.getSeconds();
104     String JavaDoc hourString;
105     String JavaDoc minuteString;
106     String JavaDoc secondString;
107
108     if (hour < 10) {
109         hourString = "0" + hour;
110     } else {
111         hourString = Integer.toString(hour);
112     }
113     if (minute < 10) {
114         minuteString = "0" + minute;
115     } else {
116         minuteString = Integer.toString(minute);
117     }
118     if (second < 10) {
119         secondString = "0" + second;
120     } else {
121         secondString = Integer.toString(second);
122     }
123     return (hourString + ":" + minuteString + ":" + secondString);
124     }
125
126     // Override all the date operations inherited from java.util.Date;
127

128    /**
129     * This method is deprecated and should not be used because SQL <code>TIME</code>
130     * values do not have a year component.
131     *
132     * @deprecated
133     * @exception java.lang.IllegalArgumentException if this
134     * method is invoked
135     * @see #setYear
136     */

137     @Deprecated JavaDoc
138     public int getYear() {
139     throw new java.lang.IllegalArgumentException JavaDoc();
140     }
141
142    /**
143     * This method is deprecated and should not be used because SQL <code>TIME</code>
144     * values do not have a month component.
145     *
146     * @deprecated
147     * @exception java.lang.IllegalArgumentException if this
148     * method is invoked
149     * @see #setMonth
150     */

151     @Deprecated JavaDoc
152     public int getMonth() {
153     throw new java.lang.IllegalArgumentException JavaDoc();
154     }
155     
156    /**
157     * This method is deprecated and should not be used because SQL <code>TIME</code>
158     * values do not have a day component.
159     *
160     * @deprecated
161     * @exception java.lang.IllegalArgumentException if this
162     * method is invoked
163     */

164     @Deprecated JavaDoc
165     public int getDay() {
166     throw new java.lang.IllegalArgumentException JavaDoc();
167     }
168
169    /**
170     * This method is deprecated and should not be used because SQL <code>TIME</code>
171     * values do not have a date component.
172     *
173     * @deprecated
174     * @exception java.lang.IllegalArgumentException if this
175     * method is invoked
176     * @see #setDate
177     */

178     @Deprecated JavaDoc
179     public int getDate() {
180     throw new java.lang.IllegalArgumentException JavaDoc();
181     }
182
183    /**
184     * This method is deprecated and should not be used because SQL <code>TIME</code>
185     * values do not have a year component.
186     *
187     * @deprecated
188     * @exception java.lang.IllegalArgumentException if this
189     * method is invoked
190     * @see #getYear
191     */

192     @Deprecated JavaDoc
193     public void setYear(int i) {
194     throw new java.lang.IllegalArgumentException JavaDoc();
195     }
196
197    /**
198     * This method is deprecated and should not be used because SQL <code>TIME</code>
199     * values do not have a month component.
200     *
201     * @deprecated
202     * @exception java.lang.IllegalArgumentException if this
203     * method is invoked
204     * @see #getMonth
205     */

206     @Deprecated JavaDoc
207     public void setMonth(int i) {
208     throw new java.lang.IllegalArgumentException JavaDoc();
209     }
210
211    /**
212     * This method is deprecated and should not be used because SQL <code>TIME</code>
213     * values do not have a date component.
214     *
215     * @deprecated
216     * @exception java.lang.IllegalArgumentException if this
217     * method is invoked
218     * @see #getDate
219     */

220     @Deprecated JavaDoc
221     public void setDate(int i) {
222     throw new java.lang.IllegalArgumentException JavaDoc();
223     }
224
225    /**
226     * Private serial version unique ID to ensure serialization
227     * compatibility.
228     */

229     static final long serialVersionUID = 8397324403548013681L;
230 }
231
232
233
234
Popular Tags