Showing the changes to java/pkg/java/sql/Date.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,250 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved. | ||
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
| 4 | * | ||
| 5 | * This code is free software; you can redistribute it and/or modify it | ||
| 6 | * under the terms of the GNU General Public License version 2 only, as | ||
| 7 | * published by the Free Software Foundation. Oracle designates this | ||
| 8 | * particular file as subject to the "Classpath" exception as provided | ||
| 9 | * by Oracle in the LICENSE file that accompanied this code. | ||
| 10 | * | ||
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT | ||
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| 14 | * version 2 for more details (a copy is included in the LICENSE file that | ||
| 15 | * accompanied this code). | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU General Public License version | ||
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, | ||
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| 20 | * | ||
| 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
| 22 | * or visit www.oracle.com if you need additional information or have any | ||
| 23 | * questions. | ||
| 24 | */ | ||
| 25 | |||
| 26 | package java.sql; | ||
| 27 | |||
| 28 | /** | ||
| 29 | * <P>A thin wrapper around a millisecond value that allows | ||
| 30 | * JDBC to identify this as an SQL <code>DATE</code> value. A | ||
| 31 | * milliseconds value represents the number of milliseconds that | ||
| 32 | * have passed since January 1, 1970 00:00:00.000 GMT. | ||
| 33 | * <p> | ||
| 34 | * To conform with the definition of SQL <code>DATE</code>, the | ||
| 35 | * millisecond values wrapped by a <code>java.sql.Date</code> instance | ||
| 36 | * must be 'normalized' by setting the | ||
| 37 | * hours, minutes, seconds, and milliseconds to zero in the particular | ||
| 38 | * time zone with which the instance is associated. | ||
| 39 | */ | ||
| 40 | public class Date extends java.util.Date { | ||
| 41 | |||
| 42 | /** | ||
| 43 | * Constructs a <code>Date</code> object initialized with the given | ||
| 44 | * year, month, and day. | ||
| 45 | * <P> | ||
| 46 | * The result is undefined if a given argument is out of bounds. | ||
| 47 | * | ||
| 48 | * @param year the year minus 1900; must be 0 to 8099. (Note that | ||
| 49 | * 8099 is 9999 minus 1900.) | ||
| 50 | * @param month 0 to 11 | ||
| 51 | * @param day 1 to 31 | ||
| 52 | * @deprecated instead use the constructor <code>Date(long date)</code> | ||
| 53 | */ | ||
| 54 | public Date(int year, int month, int day) { | ||
| 55 | super(year, month, day); | ||
| 56 | } | ||
| 57 | |||
| 58 | /** | ||
| 59 | * Constructs a <code>Date</code> object using the given milliseconds | ||
| 60 | * time value. If the given milliseconds value contains time | ||
| 61 | * information, the driver will set the time components to the | ||
| 62 | * time in the default time zone (the time zone of the Java virtual | ||
| 63 | * machine running the application) that corresponds to zero GMT. | ||
| 64 | * | ||
| 65 | * @param date milliseconds since January 1, 1970, 00:00:00 GMT not | ||
| 66 | * to exceed the milliseconds representation for the year 8099. | ||
| 67 | * A negative number indicates the number of milliseconds | ||
| 68 | * before January 1, 1970, 00:00:00 GMT. | ||
| 69 | */ | ||
| 70 | public Date(long date) { | ||
| 71 | // If the millisecond date value contains time info, mask it out. | ||
| 72 | super(date); | ||
| 73 | |||
| 74 | } | ||
| 75 | |||
| 76 | /** | ||
| 77 | * Sets an existing <code>Date</code> object | ||
| 78 | * using the given milliseconds time value. | ||
| 79 | * If the given milliseconds value contains time information, | ||
| 80 | * the driver will set the time components to the | ||
| 81 | * time in the default time zone (the time zone of the Java virtual | ||
| 82 | * machine running the application) that corresponds to zero GMT. | ||
| 83 | * | ||
| 84 | * @param date milliseconds since January 1, 1970, 00:00:00 GMT not | ||
| 85 | * to exceed the milliseconds representation for the year 8099. | ||
| 86 | * A negative number indicates the number of milliseconds | ||
| 87 | * before January 1, 1970, 00:00:00 GMT. | ||
| 88 | */ | ||
| 89 | public void setTime(long date) { | ||
| 90 | // If the millisecond date value contains time info, mask it out. | ||
| 91 | super.setTime(date); | ||
| 92 | } | ||
| 93 | |||
| 94 | /** | ||
| 95 | * Converts a string in JDBC date escape format to | ||
| 96 | * a <code>Date</code> value. | ||
| 97 | * | ||
| 98 | * @param s a <code>String</code> object representing a date in | ||
| 99 | * in the format "yyyy-[m]m-[d]d". The leading zero for <code>mm</code> | ||
| 100 | * and <code>dd</code> may also be omitted. | ||
| 101 | * @return a <code>java.sql.Date</code> object representing the | ||
| 102 | * given date | ||
| 103 | * @throws IllegalArgumentException if the date given is not in the | ||
| 104 | * JDBC date escape format (yyyy-[m]m-[d]d) | ||
| 105 | */ | ||
| 106 | public static Date valueOf(String s) { | ||
| 107 | final int YEAR_LENGTH = 4; | ||
| 108 | final int MONTH_LENGTH = 2; | ||
| 109 | final int DAY_LENGTH = 2; | ||
| 110 | final int MAX_MONTH = 12; | ||
| 111 | final int MAX_DAY = 31; | ||
| 112 | int firstDash; | ||
| 113 | int secondDash; | ||
| 114 | Date d = null; | ||
| 115 | |||
| 116 | if (s == null) { | ||
| 117 | throw new java.lang.IllegalArgumentException(); | ||
| 118 | } | ||
| 119 | |||
| 120 | firstDash = s.indexOf('-'); | ||
| 121 | secondDash = s.indexOf('-', firstDash + 1); | ||
| 122 | |||
| 123 | if ((firstDash > 0) && (secondDash > 0) && (secondDash < s.length() - 1)) { | ||
| 124 | String yyyy = s.substring(0, firstDash); | ||
| 125 | String mm = s.substring(firstDash + 1, secondDash); | ||
| 126 | String dd = s.substring(secondDash + 1); | ||
| 127 | if (yyyy.length() == YEAR_LENGTH && | ||
| 128 | (mm.length() >= 1 && mm.length() <= MONTH_LENGTH) && | ||
| 129 | (dd.length() >= 1 && dd.length() <= DAY_LENGTH)) { | ||
| 130 | int year = Integer.parseInt(yyyy); | ||
| 131 | int month = Integer.parseInt(mm); | ||
| 132 | int day = Integer.parseInt(dd); | ||
| 133 | |||
| 134 | if ((month >= 1 && month <= MAX_MONTH) && (day >= 1 && day <= MAX_DAY)) { | ||
| 135 | d = new Date(year - 1900, month - 1, day); | ||
| 136 | } | ||
| 137 | } | ||
| 138 | } | ||
| 139 | if (d == null) { | ||
| 140 | throw new java.lang.IllegalArgumentException(); | ||
| 141 | } | ||
| 142 | |||
| 143 | return d; | ||
| 144 | |||
| 145 | } | ||
| 146 | |||
| 147 | |||
| 148 | /** | ||
| 149 | * Formats a date in the date escape format yyyy-mm-dd. | ||
| 150 | * <P> | ||
| 151 | * @return a String in yyyy-mm-dd format | ||
| 152 | */ | ||
| 153 | public String toString () { | ||
| 154 | int year = super.getYear() + 1900; | ||
| 155 | int month = super.getMonth() + 1; | ||
| 156 | int day = super.getDate(); | ||
| 157 | |||
| 158 | char buf[] = "2000-00-00".toCharArray(); | ||
| 159 | buf[0] = Character.forDigit(year/1000,10); | ||
| 160 | buf[1] = Character.forDigit((year/100)%10,10); | ||
| 161 | buf[2] = Character.forDigit((year/10)%10,10); | ||
| 162 | buf[3] = Character.forDigit(year%10,10); | ||
| 163 | buf[5] = Character.forDigit(month/10,10); | ||
| 164 | buf[6] = Character.forDigit(month%10,10); | ||
| 165 | buf[8] = Character.forDigit(day/10,10); | ||
| 166 | buf[9] = Character.forDigit(day%10,10); | ||
| 167 | |||
| 168 | return new String(buf); | ||
| 169 | } | ||
| 170 | |||
| 171 | // Override all the time operations inherited from java.util.Date; | ||
| 172 | |||
| 173 | /** | ||
| 174 | * This method is deprecated and should not be used because SQL Date | ||
| 175 | * values do not have a time component. | ||
| 176 | * | ||
| 177 | * @deprecated | ||
| 178 | * @exception java.lang.IllegalArgumentException if this method is invoked | ||
| 179 | * @see #setHours | ||
| 180 | */ | ||
| 181 | public int getHours() { | ||
| 182 | throw new java.lang.IllegalArgumentException(); | ||
| 183 | } | ||
| 184 | |||
| 185 | /** | ||
| 186 | * This method is deprecated and should not be used because SQL Date | ||
| 187 | * values do not have a time component. | ||
| 188 | * | ||
| 189 | * @deprecated | ||
| 190 | * @exception java.lang.IllegalArgumentException if this method is invoked | ||
| 191 | * @see #setMinutes | ||
| 192 | */ | ||
| 193 | public int getMinutes() { | ||
| 194 | throw new java.lang.IllegalArgumentException(); | ||
| 195 | } | ||
| 196 | |||
| 197 | /** | ||
| 198 | * This method is deprecated and should not be used because SQL Date | ||
| 199 | * values do not have a time component. | ||
| 200 | * | ||
| 201 | * @deprecated | ||
| 202 | * @exception java.lang.IllegalArgumentException if this method is invoked | ||
| 203 | * @see #setSeconds | ||
| 204 | */ | ||
| 205 | public int getSeconds() { | ||
| 206 | throw new java.lang.IllegalArgumentException(); | ||
| 207 | } | ||
| 208 | |||
| 209 | /** | ||
| 210 | * This method is deprecated and should not be used because SQL Date | ||
| 211 | * values do not have a time component. | ||
| 212 | * | ||
| 213 | * @deprecated | ||
| 214 | * @exception java.lang.IllegalArgumentException if this method is invoked | ||
| 215 | * @see #getHours | ||
| 216 | */ | ||
| 217 | public void setHours(int i) { | ||
| 218 | throw new java.lang.IllegalArgumentException(); | ||
| 219 | } | ||
| 220 | |||
| 221 | /** | ||
| 222 | * This method is deprecated and should not be used because SQL Date | ||
| 223 | * values do not have a time component. | ||
| 224 | * | ||
| 225 | * @deprecated | ||
| 226 | * @exception java.lang.IllegalArgumentException if this method is invoked | ||
| 227 | * @see #getMinutes | ||
| 228 | */ | ||
| 229 | public void setMinutes(int i) { | ||
| 230 | throw new java.lang.IllegalArgumentException(); | ||
| 231 | } | ||
| 232 | |||
| 233 | /** | ||
| 234 | * This method is deprecated and should not be used because SQL Date | ||
| 235 | * values do not have a time component. | ||
| 236 | * | ||
| 237 | * @deprecated | ||
| 238 | * @exception java.lang.IllegalArgumentException if this method is invoked | ||
| 239 | * @see #getSeconds | ||
| 240 | */ | ||
| 241 | public void setSeconds(int i) { | ||
| 242 | throw new java.lang.IllegalArgumentException(); | ||
| 243 | } | ||
| 244 | |||
| 245 | /** | ||
| 246 | * Private serial version unique ID to ensure serialization | ||
| 247 | * compatibility. | ||
| 248 | */ | ||
| 249 | static final long serialVersionUID = 1511598038487230103L; | ||
| 250 | } | ||