• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  * Copyright (c) 1996, 2004, Oracle and/or its affiliates. All rights reserved.
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This code is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 only, as
8  * published by the Free Software Foundation.  Oracle designates this
9  * particular file as subject to the "Classpath" exception as provided
10  * by Oracle in the LICENSE file that accompanied this code.
11  *
12  * This code is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15  * version 2 for more details (a copy is included in the LICENSE file that
16  * accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License version
19  * 2 along with this work; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21  *
22  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
23  * or visit www.oracle.com if you need additional information or have any
24  * questions.
25  */
26 
27 package java.sql;
28 
29 /**
30  * <P>A thin wrapper around the <code>java.util.Date</code> class that allows the JDBC
31  * API to identify this as an SQL <code>TIME</code> value. The <code>Time</code>
32  * class adds formatting and
33  * parsing operations to support the JDBC escape syntax for time
34  * values.
35  * <p>The date components should be set to the "zero epoch"
36  * value of January 1, 1970 and should not be accessed.
37  */
38 public class Time extends java.util.Date {
39 
40     /**
41      * Constructs a <code>Time</code> object initialized with the
42      * given values for the hour, minute, and second.
43      * The driver sets the date components to January 1, 1970.
44      * Any method that attempts to access the date components of a
45      * <code>Time</code> object will throw a
46      * <code>java.lang.IllegalArgumentException</code>.
47      * <P>
48      * The result is undefined if a given argument is out of bounds.
49      *
50      * @param hour 0 to 23
51      * @param minute 0 to 59
52      * @param second 0 to 59
53      *
54      * @deprecated Use the constructor that takes a milliseconds value
55      *             in place of this constructor
56      */
57     @Deprecated
Time(int hour, int minute, int second)58     public Time(int hour, int minute, int second) {
59         super(70, 0, 1, hour, minute, second);
60     }
61 
62     /**
63      * Constructs a <code>Time</code> object using a milliseconds time value.
64      *
65      * @param time milliseconds since January 1, 1970, 00:00:00 GMT;
66      *             a negative number is milliseconds before
67      *               January 1, 1970, 00:00:00 GMT
68      */
Time(long time)69     public Time(long time) {
70         super(time);
71     }
72 
73     /**
74      * Sets a <code>Time</code> object using a milliseconds time value.
75      *
76      * @param time milliseconds since January 1, 1970, 00:00:00 GMT;
77      *             a negative number is milliseconds before
78      *               January 1, 1970, 00:00:00 GMT
79      */
setTime(long time)80     public void setTime(long time) {
81         super.setTime(time);
82     }
83 
84     /**
85      * Converts a string in JDBC time escape format to a <code>Time</code> value.
86      *
87      * @param s time in format "hh:mm:ss"
88      * @return a corresponding <code>Time</code> object
89      */
valueOf(String s)90     public static Time valueOf(String s) {
91         int hour;
92         int minute;
93         int second;
94         int firstColon;
95         int secondColon;
96 
97         if (s == null) throw new java.lang.IllegalArgumentException();
98 
99         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 =
105                 Integer.parseInt(s.substring(firstColon+1, 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      */
toString()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    // Android-changed: Moved @deprecated to include a deprecation reason in the documentation.
148    /**
149     * @deprecated This method is deprecated and should not be used because SQL <code>TIME</code>
150     * values do not have a year component.
151     *
152     * @exception java.lang.IllegalArgumentException if this
153     *           method is invoked
154     * @see #setYear
155     */
156     @Deprecated
getYear()157     public int getYear() {
158         throw new java.lang.IllegalArgumentException();
159     }
160 
161    // Android-changed: Moved @deprecated to include a deprecation reason in the documentation.
162    /**
163     * @deprecated This method is deprecated and should not be used because SQL <code>TIME</code>
164     * values do not have a month component.
165     *
166     * @exception java.lang.IllegalArgumentException if this
167     *           method is invoked
168     * @see #setMonth
169     */
170     @Deprecated
getMonth()171     public int getMonth() {
172         throw new java.lang.IllegalArgumentException();
173     }
174 
175    // Android-changed: Moved @deprecated to include a deprecation reason in the documentation.
176    /**
177     * @deprecated This method is deprecated and should not be used because SQL <code>TIME</code>
178     * values do not have a day component.
179     *
180     * @exception java.lang.IllegalArgumentException if this
181     *           method is invoked
182     */
183     @Deprecated
getDay()184     public int getDay() {
185         throw new java.lang.IllegalArgumentException();
186     }
187 
188    // Android-changed: Moved @deprecated to include a deprecation reason in the documentation.
189    /**
190     * @deprecated This method is deprecated and should not be used because SQL <code>TIME</code>
191     * values do not have a date component.
192     *
193     * @exception java.lang.IllegalArgumentException if this
194     *           method is invoked
195     * @see #setDate
196     */
197     @Deprecated
getDate()198     public int getDate() {
199         throw new java.lang.IllegalArgumentException();
200     }
201 
202    // Android-changed: Moved @deprecated to include a deprecation reason in the documentation.
203    /**
204     * @deprecated This method is deprecated and should not be used because SQL <code>TIME</code>
205     * values do not have a year component.
206     *
207     * @exception java.lang.IllegalArgumentException if this
208     *           method is invoked
209     * @see #getYear
210     */
211     @Deprecated
setYear(int i)212     public void setYear(int i) {
213         throw new java.lang.IllegalArgumentException();
214     }
215 
216    // Android-changed: Moved @deprecated to include a deprecation reason in the documentation.
217    /**
218     * @deprecated This method is deprecated and should not be used because SQL <code>TIME</code>
219     * values do not have a month component.
220     *
221     * @exception java.lang.IllegalArgumentException if this
222     *           method is invoked
223     * @see #getMonth
224     */
225     @Deprecated
setMonth(int i)226     public void setMonth(int i) {
227         throw new java.lang.IllegalArgumentException();
228     }
229 
230    // Android-changed: Moved @deprecated to include a deprecation reason in the documentation.
231    /**
232     * @deprecated This method is deprecated and should not be used because SQL <code>TIME</code>
233     * values do not have a date component.
234     *
235     * @exception java.lang.IllegalArgumentException if this
236     *           method is invoked
237     * @see #getDate
238     */
239     @Deprecated
setDate(int i)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 }
250