Path: blob/master/src/java.sql/share/classes/java/sql/Date.java
41153 views
/*1* Copyright (c) 1996, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.sql;2627import java.time.Instant;28import java.time.LocalDate;2930/**31* <P>A thin wrapper around a millisecond value that allows32* JDBC to identify this as an SQL {@code DATE} value. A33* milliseconds value represents the number of milliseconds that34* have passed since January 1, 1970 00:00:00.000 GMT.35* <p>36* To conform with the definition of SQL {@code DATE}, the37* millisecond values wrapped by a {@code java.sql.Date} instance38* must be 'normalized' by setting the39* hours, minutes, seconds, and milliseconds to zero in the particular40* time zone with which the instance is associated.41*42* @since 1.143*/44public class Date extends java.util.Date {4546/**47* Constructs a {@code Date} object initialized with the given48* year, month, and day.49* <P>50* The result is undefined if a given argument is out of bounds.51*52* @param year the year minus 1900; must be 0 to 8099. (Note that53* 8099 is 9999 minus 1900.)54* @param month 0 to 1155* @param day 1 to 3156* @deprecated instead use the constructor {@code Date(long date)}57*/58@Deprecated(since="1.2")59public Date(int year, int month, int day) {60super(year, month, day);61}6263/**64* Constructs a {@code Date} object using the given milliseconds65* time value. If the given milliseconds value contains time66* information, the driver will set the time components to the67* time in the default time zone (the time zone of the Java virtual68* machine running the application) that corresponds to zero GMT.69*70* @param date milliseconds since January 1, 1970, 00:00:00 GMT not71* to exceed the milliseconds representation for the year 8099.72* A negative number indicates the number of milliseconds73* before January 1, 1970, 00:00:00 GMT.74*/75public Date(long date) {76// If the millisecond date value contains time info, mask it out.77super(date);7879}8081/**82* Sets an existing {@code Date} object83* using the given milliseconds time value.84* If the given milliseconds value contains time information,85* the driver will set the time components to the86* time in the default time zone (the time zone of the Java virtual87* machine running the application) that corresponds to zero GMT.88*89* @param date milliseconds since January 1, 1970, 00:00:00 GMT not90* to exceed the milliseconds representation for the year 8099.91* A negative number indicates the number of milliseconds92* before January 1, 1970, 00:00:00 GMT.93*/94public void setTime(long date) {95// If the millisecond date value contains time info, mask it out.96super.setTime(date);97}9899/**100* Converts a string in JDBC date escape format to101* a {@code Date} value.102*103* @param s a {@code String} object representing a date in104* in the format "yyyy-[m]m-[d]d". The leading zero for {@code mm}105* and {@code dd} may also be omitted.106* @return a {@code java.sql.Date} object representing the107* given date108* @throws IllegalArgumentException if the date given is not in the109* JDBC date escape format (yyyy-[m]m-[d]d)110*/111public static Date valueOf(String s) {112if (s == null) {113throw new java.lang.IllegalArgumentException();114}115final int YEAR_LENGTH = 4;116final int MONTH_LENGTH = 2;117final int DAY_LENGTH = 2;118final int MAX_MONTH = 12;119final int MAX_DAY = 31;120Date d = null;121122int firstDash = s.indexOf('-');123int secondDash = s.indexOf('-', firstDash + 1);124int len = s.length();125126if ((firstDash > 0) && (secondDash > 0) && (secondDash < len - 1)) {127if (firstDash == YEAR_LENGTH &&128(secondDash - firstDash > 1 && secondDash - firstDash <= MONTH_LENGTH + 1) &&129(len - secondDash > 1 && len - secondDash <= DAY_LENGTH + 1)) {130int year = Integer.parseInt(s, 0, firstDash, 10);131int month = Integer.parseInt(s, firstDash + 1, secondDash, 10);132int day = Integer.parseInt(s, secondDash + 1, len, 10);133134if ((month >= 1 && month <= MAX_MONTH) && (day >= 1 && day <= MAX_DAY)) {135d = new Date(year - 1900, month - 1, day);136}137}138}139if (d == null) {140throw new java.lang.IllegalArgumentException();141}142143return d;144145}146147148/**149* Formats a date in the date escape format yyyy-mm-dd.150*151* @return a String in yyyy-mm-dd format152*/153@SuppressWarnings("deprecation")154public String toString () {155int year = super.getYear() + 1900;156int month = super.getMonth() + 1;157int day = super.getDate();158159char buf[] = new char[10];160formatDecimalInt(year, buf, 0, 4);161buf[4] = '-';162Date.formatDecimalInt(month, buf, 5, 2);163buf[7] = '-';164Date.formatDecimalInt(day, buf, 8, 2);165166return new String(buf);167}168169/**170* Formats an unsigned integer into a char array in decimal output format.171* Numbers will be zero-padded or truncated if the string representation172* of the integer is smaller than or exceeds len, respectively.173*174* Should consider moving this to Integer and expose it through175* JavaLangAccess similar to Integer::formatUnsignedInt176* @param val Value to convert177* @param buf Array containing converted value178* @param offset Starting pos in buf179* @param len length of output value180*/181static void formatDecimalInt(int val, char[] buf, int offset, int len) {182int charPos = offset + len;183do {184buf[--charPos] = (char)('0' + (val % 10));185val /= 10;186} while (charPos > offset);187}188189// Override all the time operations inherited from java.util.Date;190191/**192* This method is deprecated and should not be used because SQL Date193* values do not have a time component.194*195* @deprecated196* @throws java.lang.IllegalArgumentException if this method is invoked197* @see #setHours198*/199@Deprecated(since="1.2")200public int getHours() {201throw new java.lang.IllegalArgumentException();202}203204/**205* This method is deprecated and should not be used because SQL Date206* values do not have a time component.207*208* @deprecated209* @throws java.lang.IllegalArgumentException if this method is invoked210* @see #setMinutes211*/212@Deprecated(since="1.2")213public int getMinutes() {214throw new java.lang.IllegalArgumentException();215}216217/**218* This method is deprecated and should not be used because SQL Date219* values do not have a time component.220*221* @deprecated222* @throws java.lang.IllegalArgumentException if this method is invoked223* @see #setSeconds224*/225@Deprecated(since="1.2")226public int getSeconds() {227throw new java.lang.IllegalArgumentException();228}229230/**231* This method is deprecated and should not be used because SQL Date232* values do not have a time component.233*234* @deprecated235* @throws java.lang.IllegalArgumentException if this method is invoked236* @see #getHours237*/238@Deprecated(since="1.2")239public void setHours(int i) {240throw new java.lang.IllegalArgumentException();241}242243/**244* This method is deprecated and should not be used because SQL Date245* values do not have a time component.246*247* @deprecated248* @throws java.lang.IllegalArgumentException if this method is invoked249* @see #getMinutes250*/251@Deprecated(since="1.2")252public void setMinutes(int i) {253throw new java.lang.IllegalArgumentException();254}255256/**257* This method is deprecated and should not be used because SQL Date258* values do not have a time component.259*260* @deprecated261* @throws java.lang.IllegalArgumentException if this method is invoked262* @see #getSeconds263*/264@Deprecated(since="1.2")265public void setSeconds(int i) {266throw new java.lang.IllegalArgumentException();267}268269/**270* Private serial version unique ID to ensure serialization271* compatibility.272*/273static final long serialVersionUID = 1511598038487230103L;274275/**276* Obtains an instance of {@code Date} from a {@link LocalDate} object277* with the same year, month and day of month value as the given278* {@code LocalDate}.279* <p>280* The provided {@code LocalDate} is interpreted as the local date281* in the local time zone.282*283* @param date a {@code LocalDate} to convert284* @return a {@code Date} object285* @throws NullPointerException if {@code date} is null286* @since 1.8287*/288@SuppressWarnings("deprecation")289public static Date valueOf(LocalDate date) {290return new Date(date.getYear() - 1900, date.getMonthValue() -1,291date.getDayOfMonth());292}293294/**295* Creates a {@code LocalDate} instance using the year, month and day296* from this {@code Date} object.297* @return a {@code LocalDate} object representing the same date value298*299* @since 1.8300*/301@SuppressWarnings("deprecation")302public LocalDate toLocalDate() {303return LocalDate.of(getYear() + 1900, getMonth() + 1, getDate());304}305306/**307* This method always throws an UnsupportedOperationException and should308* not be used because SQL {@code Date} values do not have a time309* component.310*311* @throws java.lang.UnsupportedOperationException if this method is invoked312*/313@Override314public Instant toInstant() {315throw new java.lang.UnsupportedOperationException();316}317}318319320