1 /* 2 * Copyright (c) 1996, 2013, 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 import java.time.Instant; 29 import java.time.LocalDate; 30 31 /** 32 * <P>A thin wrapper around a millisecond value that allows 33 * JDBC to identify this as an SQL <code>DATE</code> value. A 34 * milliseconds value represents the number of milliseconds that 35 * have passed since January 1, 1970 00:00:00.000 GMT. 36 * <p> 37 * To conform with the definition of SQL <code>DATE</code>, the 38 * millisecond values wrapped by a <code>java.sql.Date</code> instance 39 * must be 'normalized' by setting the 40 * hours, minutes, seconds, and milliseconds to zero in the particular 41 * time zone with which the instance is associated. 42 */ 43 public class Date extends java.util.Date { 44 45 /** 46 * Constructs a <code>Date</code> object initialized with the given 47 * year, month, and day. 48 * <P> 49 * The result is undefined if a given argument is out of bounds. 50 * 51 * @param year the year minus 1900; must be 0 to 8099. (Note that 52 * 8099 is 9999 minus 1900.) 53 * @param month 0 to 11 54 * @param day 1 to 31 55 * @deprecated instead use the constructor <code>Date(long date)</code> 56 */ 57 @Deprecated Date(int year, int month, int day)58 public Date(int year, int month, int day) { 59 super(year, month, day); 60 } 61 62 /** 63 * Constructs a <code>Date</code> object using the given milliseconds 64 * time value. If the given milliseconds value contains time 65 * information, the driver will set the time components to the 66 * time in the default time zone (the time zone of the Java virtual 67 * machine running the application) that corresponds to zero GMT. 68 * 69 * @param date milliseconds since January 1, 1970, 00:00:00 GMT not 70 * to exceed the milliseconds representation for the year 8099. 71 * A negative number indicates the number of milliseconds 72 * before January 1, 1970, 00:00:00 GMT. 73 */ Date(long date)74 public Date(long date) { 75 // If the millisecond date value contains time info, mask it out. 76 super(date); 77 78 } 79 80 /** 81 * Sets an existing <code>Date</code> object 82 * using the given milliseconds time value. 83 * If the given milliseconds value contains time information, 84 * the driver will set the time components to the 85 * time in the default time zone (the time zone of the Java virtual 86 * machine running the application) that corresponds to zero GMT. 87 * 88 * @param date milliseconds since January 1, 1970, 00:00:00 GMT not 89 * to exceed the milliseconds representation for the year 8099. 90 * A negative number indicates the number of milliseconds 91 * before January 1, 1970, 00:00:00 GMT. 92 */ setTime(long date)93 public void setTime(long date) { 94 // If the millisecond date value contains time info, mask it out. 95 super.setTime(date); 96 } 97 98 /** 99 * Converts a string in JDBC date escape format to 100 * a <code>Date</code> value. 101 * 102 * @param s a <code>String</code> object representing a date in 103 * in the format "yyyy-[m]m-[d]d". The leading zero for <code>mm</code> 104 * and <code>dd</code> may also be omitted. 105 * @return a <code>java.sql.Date</code> object representing the 106 * given date 107 * @throws IllegalArgumentException if the date given is not in the 108 * JDBC date escape format (yyyy-[m]m-[d]d) 109 */ valueOf(String s)110 public static Date valueOf(String s) { 111 final int YEAR_LENGTH = 4; 112 final int MONTH_LENGTH = 2; 113 final int DAY_LENGTH = 2; 114 final int MAX_MONTH = 12; 115 final int MAX_DAY = 31; 116 int firstDash; 117 int secondDash; 118 Date d = null; 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 */ 156 @SuppressWarnings("deprecation") toString()157 public String toString () { 158 int year = super.getYear() + 1900; 159 int month = super.getMonth() + 1; 160 int day = super.getDate(); 161 162 char buf[] = "2000-00-00".toCharArray(); 163 buf[0] = Character.forDigit(year/1000,10); 164 buf[1] = Character.forDigit((year/100)%10,10); 165 buf[2] = Character.forDigit((year/10)%10,10); 166 buf[3] = Character.forDigit(year%10,10); 167 buf[5] = Character.forDigit(month/10,10); 168 buf[6] = Character.forDigit(month%10,10); 169 buf[8] = Character.forDigit(day/10,10); 170 buf[9] = Character.forDigit(day%10,10); 171 172 return new String(buf); 173 } 174 175 // Override all the time operations inherited from java.util.Date; 176 177 /** 178 * This method is deprecated and should not be used because SQL Date 179 * values do not have a time component. 180 * 181 * @deprecated 182 * @exception java.lang.IllegalArgumentException if this method is invoked 183 * @see #setHours 184 */ 185 @Deprecated getHours()186 public int getHours() { 187 throw new java.lang.IllegalArgumentException(); 188 } 189 190 /** 191 * This method is deprecated and should not be used because SQL Date 192 * values do not have a time component. 193 * 194 * @deprecated 195 * @exception java.lang.IllegalArgumentException if this method is invoked 196 * @see #setMinutes 197 */ 198 @Deprecated getMinutes()199 public int getMinutes() { 200 throw new java.lang.IllegalArgumentException(); 201 } 202 203 /** 204 * This method is deprecated and should not be used because SQL Date 205 * values do not have a time component. 206 * 207 * @deprecated 208 * @exception java.lang.IllegalArgumentException if this method is invoked 209 * @see #setSeconds 210 */ 211 @Deprecated getSeconds()212 public int getSeconds() { 213 throw new java.lang.IllegalArgumentException(); 214 } 215 216 /** 217 * This method is deprecated and should not be used because SQL Date 218 * values do not have a time component. 219 * 220 * @deprecated 221 * @exception java.lang.IllegalArgumentException if this method is invoked 222 * @see #getHours 223 */ 224 @Deprecated setHours(int i)225 public void setHours(int i) { 226 throw new java.lang.IllegalArgumentException(); 227 } 228 229 /** 230 * This method is deprecated and should not be used because SQL Date 231 * values do not have a time component. 232 * 233 * @deprecated 234 * @exception java.lang.IllegalArgumentException if this method is invoked 235 * @see #getMinutes 236 */ 237 @Deprecated setMinutes(int i)238 public void setMinutes(int i) { 239 throw new java.lang.IllegalArgumentException(); 240 } 241 242 /** 243 * This method is deprecated and should not be used because SQL Date 244 * values do not have a time component. 245 * 246 * @deprecated 247 * @exception java.lang.IllegalArgumentException if this method is invoked 248 * @see #getSeconds 249 */ 250 @Deprecated setSeconds(int i)251 public void setSeconds(int i) { 252 throw new java.lang.IllegalArgumentException(); 253 } 254 255 /** 256 * Private serial version unique ID to ensure serialization 257 * compatibility. 258 */ 259 static final long serialVersionUID = 1511598038487230103L; 260 261 /** 262 * Obtains an instance of {@code Date} from a {@link LocalDate} object 263 * with the same year, month and day of month value as the given 264 * {@code LocalDate}. 265 * <p> 266 * The provided {@code LocalDate} is interpreted as the local date 267 * in the local time zone. 268 * 269 * @param date a {@code LocalDate} to convert 270 * @return a {@code Date} object 271 * @exception NullPointerException if {@code date} is null 272 * @since 1.8 273 */ 274 @SuppressWarnings("deprecation") valueOf(LocalDate date)275 public static Date valueOf(LocalDate date) { 276 return new Date(date.getYear() - 1900, date.getMonthValue() -1, 277 date.getDayOfMonth()); 278 } 279 280 /** 281 * Converts this {@code Date} object to a {@code LocalDate} 282 * <p> 283 * The conversion creates a {@code LocalDate} that represents the same 284 * date value as this {@code Date} in local time zone 285 * 286 * @return a {@code LocalDate} object representing the same date value 287 * 288 * @since 1.8 289 */ 290 @SuppressWarnings("deprecation") toLocalDate()291 public LocalDate toLocalDate() { 292 return LocalDate.of(getYear() + 1900, getMonth() + 1, getDate()); 293 } 294 295 /** 296 * This method always throws an UnsupportedOperationException and should 297 * not be used because SQL {@code Date} values do not have a time 298 * component. 299 * 300 * @exception java.lang.UnsupportedOperationException if this method is invoked 301 */ 302 @Override toInstant()303 public Instant toInstant() { 304 throw new java.lang.UnsupportedOperationException(); 305 } 306 } 307