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