• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  * Copyright (c) 1996, 2010, 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 a millisecond value that allows
31  * JDBC to identify this as an SQL <code>DATE</code> value.  A
32  * milliseconds value represents the number of milliseconds that
33  * have passed since January 1, 1970 00:00:00.000 GMT.
34  * <p>
35  * To conform with the definition of SQL <code>DATE</code>, the
36  * millisecond values wrapped by a <code>java.sql.Date</code> instance
37  * must be 'normalized' by setting the
38  * hours, minutes, seconds, and milliseconds to zero in the particular
39  * time zone with which the instance is associated.
40  */
41 public class Date extends java.util.Date {
42 
43     /**
44      * Constructs a <code>Date</code> object initialized with the given
45      * year, month, and day.
46      * <P>
47      * The result is undefined if a given argument is out of bounds.
48      *
49      * @param year the year minus 1900; must be 0 to 8099. (Note that
50      *        8099 is 9999 minus 1900.)
51      * @param month 0 to 11
52      * @param day 1 to 31
53      * @deprecated instead use the constructor <code>Date(long date)</code>
54      */
55     // Android-added: @Deprecated annotation from OpenJDK8u121-b13 to fix build warnings.
56     @Deprecated
Date(int year, int month, int day)57     public Date(int year, int month, int day) {
58         super(year, month, day);
59     }
60 
61     /**
62      * Constructs a <code>Date</code> object using the given milliseconds
63      * time value.  If the given milliseconds value contains time
64      * information, the driver will set the time components to the
65      * time in the default time zone (the time zone of the Java virtual
66      * machine running the application) that corresponds to zero GMT.
67      *
68      * @param date milliseconds since January 1, 1970, 00:00:00 GMT not
69      *        to exceed the milliseconds representation for the year 8099.
70      *        A negative number indicates the number of milliseconds
71      *        before January 1, 1970, 00:00:00 GMT.
72      */
Date(long date)73     public Date(long date) {
74         // If the millisecond date value contains time info, mask it out.
75         super(date);
76 
77     }
78 
79     /**
80      * Sets an existing <code>Date</code> object
81      * using the given milliseconds time value.
82      * If the given milliseconds value contains time information,
83      * the driver will set the time components to the
84      * time in the default time zone (the time zone of the Java virtual
85      * machine running the application) that corresponds to zero GMT.
86      *
87      * @param date milliseconds since January 1, 1970, 00:00:00 GMT not
88      *        to exceed the milliseconds representation for the year 8099.
89      *        A negative number indicates the number of milliseconds
90      *        before January 1, 1970, 00:00:00 GMT.
91      */
setTime(long date)92     public void setTime(long date) {
93         // If the millisecond date value contains time info, mask it out.
94         super.setTime(date);
95     }
96 
97     /**
98      * Converts a string in JDBC date escape format to
99      * a <code>Date</code> value.
100      *
101      * @param s a <code>String</code> object representing a date in
102      *        in the format "yyyy-[m]m-[d]d". The leading zero for <code>mm</code>
103      * and <code>dd</code> may also be omitted.
104      * @return a <code>java.sql.Date</code> object representing the
105      *         given date
106      * @throws IllegalArgumentException if the date given is not in the
107      *         JDBC date escape format (yyyy-[m]m-[d]d)
108      */
valueOf(String s)109     public static Date valueOf(String s) {
110         final int YEAR_LENGTH = 4;
111         final int MONTH_LENGTH = 2;
112         final int DAY_LENGTH = 2;
113         final int MAX_MONTH = 12;
114         final int MAX_DAY = 31;
115         int firstDash;
116         int secondDash;
117         Date d = null;
118 
119         if (s == null) {
120             throw new java.lang.IllegalArgumentException();
121         }
122 
123         firstDash = s.indexOf('-');
124         secondDash = s.indexOf('-', firstDash + 1);
125 
126         if ((firstDash > 0) && (secondDash > 0) && (secondDash < s.length() - 1)) {
127             String yyyy = s.substring(0, firstDash);
128             String mm = s.substring(firstDash + 1, secondDash);
129             String dd = s.substring(secondDash + 1);
130             if (yyyy.length() == YEAR_LENGTH &&
131                     (mm.length() >= 1 && mm.length() <= MONTH_LENGTH) &&
132                     (dd.length() >= 1 && dd.length() <= DAY_LENGTH)) {
133                 int year = Integer.parseInt(yyyy);
134                 int month = Integer.parseInt(mm);
135                 int day = Integer.parseInt(dd);
136 
137                 if ((month >= 1 && month <= MAX_MONTH) && (day >= 1 && day <= MAX_DAY)) {
138                     d = new Date(year - 1900, month - 1, day);
139                 }
140             }
141         }
142         if (d == null) {
143             throw new java.lang.IllegalArgumentException();
144         }
145 
146         return d;
147 
148     }
149 
150 
151     /**
152      * Formats a date in the date escape format yyyy-mm-dd.
153      * <P>
154      * @return a String in yyyy-mm-dd format
155      */
toString()156     public String toString () {
157         int year = super.getYear() + 1900;
158         int month = super.getMonth() + 1;
159         int day = super.getDate();
160 
161         char buf[] = "2000-00-00".toCharArray();
162         buf[0] = Character.forDigit(year/1000,10);
163         buf[1] = Character.forDigit((year/100)%10,10);
164         buf[2] = Character.forDigit((year/10)%10,10);
165         buf[3] = Character.forDigit(year%10,10);
166         buf[5] = Character.forDigit(month/10,10);
167         buf[6] = Character.forDigit(month%10,10);
168         buf[8] = Character.forDigit(day/10,10);
169         buf[9] = Character.forDigit(day%10,10);
170 
171         return new String(buf);
172     }
173 
174     // Override all the time operations inherited from java.util.Date;
175 
176    // Android-changed: Moved @deprecated to include a deprecation reason in the documentation.
177    /**
178     * @deprecated This method is deprecated and should not be used because SQL Date
179     * values do not have a time component.
180     *
181     * @exception java.lang.IllegalArgumentException if this method is invoked
182     * @see #setHours
183     */
184     // Android-added: @Deprecated annotation from OpenJDK8u121-b13 to fix build warnings.
185     @Deprecated
getHours()186     public int getHours() {
187         throw new java.lang.IllegalArgumentException();
188     }
189 
190    // Android-changed: Moved @deprecated to include a deprecation reason in the documentation.
191    /**
192     * @deprecated This method is deprecated and should not be used because SQL Date
193     * values do not have a time component.
194     *
195     * @exception java.lang.IllegalArgumentException if this method is invoked
196     * @see #setMinutes
197     */
198     // Android-added: @Deprecated annotation from OpenJDK8u121-b13 to fix build warnings.
199     @Deprecated
getMinutes()200     public int getMinutes() {
201         throw new java.lang.IllegalArgumentException();
202     }
203 
204    // Android-changed: Moved @deprecated to include a deprecation reason in the documentation.
205    /**
206     * @deprecated This method is deprecated and should not be used because SQL Date
207     * values do not have a time component.
208     *
209     * @exception java.lang.IllegalArgumentException if this method is invoked
210     * @see #setSeconds
211     */
212     // Android-added: @Deprecated annotation from OpenJDK8u121-b13 to fix build warnings.
213     @Deprecated
getSeconds()214     public int getSeconds() {
215         throw new java.lang.IllegalArgumentException();
216     }
217 
218    // Android-changed: Moved @deprecated to include a deprecation reason in the documentation.
219    /**
220     * @deprecated This method is deprecated and should not be used because SQL Date
221     * values do not have a time component.
222     *
223     * @exception java.lang.IllegalArgumentException if this method is invoked
224     * @see #getHours
225     */
226     // Android-added: @Deprecated annotation from OpenJDK8u121-b13 to fix build warnings.
227     @Deprecated
setHours(int i)228     public void setHours(int i) {
229         throw new java.lang.IllegalArgumentException();
230     }
231 
232    // Android-changed: Moved @deprecated to include a deprecation reason in the documentation.
233    /**
234     * @deprecated This method is deprecated and should not be used because SQL Date
235     * values do not have a time component.
236     *
237     * @exception java.lang.IllegalArgumentException if this method is invoked
238     * @see #getMinutes
239     */
240     // Android-added: @Deprecated annotation from OpenJDK8u121-b13 to fix build warnings.
241     @Deprecated
setMinutes(int i)242     public void setMinutes(int i) {
243         throw new java.lang.IllegalArgumentException();
244     }
245 
246    // Android-changed: Moved @deprecated to include a deprecation reason in the documentation.
247    /**
248     * @deprecated This method is deprecated and should not be used because SQL Date
249     * values do not have a time component.
250     *
251     * @exception java.lang.IllegalArgumentException if this method is invoked
252     * @see #getSeconds
253     */
254     // Android-added: @Deprecated annotation from OpenJDK8u121-b13 to fix build warnings.
255     @Deprecated
setSeconds(int i)256     public void setSeconds(int i) {
257         throw new java.lang.IllegalArgumentException();
258     }
259 
260    /**
261     * Private serial version unique ID to ensure serialization
262     * compatibility.
263     */
264     static final long serialVersionUID = 1511598038487230103L;
265 }
266