Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/sql/Date.java
38829 views
/*1* Copyright (c) 1996, 2013, 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</code> 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</code>, the37* millisecond values wrapped by a <code>java.sql.Date</code> 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*/42public class Date extends java.util.Date {4344/**45* Constructs a <code>Date</code> object initialized with the given46* year, month, and day.47* <P>48* The result is undefined if a given argument is out of bounds.49*50* @param year the year minus 1900; must be 0 to 8099. (Note that51* 8099 is 9999 minus 1900.)52* @param month 0 to 1153* @param day 1 to 3154* @deprecated instead use the constructor <code>Date(long date)</code>55*/56@Deprecated57public Date(int year, int month, int day) {58super(year, month, day);59}6061/**62* Constructs a <code>Date</code> object using the given milliseconds63* time value. If the given milliseconds value contains time64* information, the driver will set the time components to the65* time in the default time zone (the time zone of the Java virtual66* machine running the application) that corresponds to zero GMT.67*68* @param date milliseconds since January 1, 1970, 00:00:00 GMT not69* to exceed the milliseconds representation for the year 8099.70* A negative number indicates the number of milliseconds71* before January 1, 1970, 00:00:00 GMT.72*/73public Date(long date) {74// If the millisecond date value contains time info, mask it out.75super(date);7677}7879/**80* Sets an existing <code>Date</code> object81* using the given milliseconds time value.82* If the given milliseconds value contains time information,83* the driver will set the time components to the84* time in the default time zone (the time zone of the Java virtual85* machine running the application) that corresponds to zero GMT.86*87* @param date milliseconds since January 1, 1970, 00:00:00 GMT not88* to exceed the milliseconds representation for the year 8099.89* A negative number indicates the number of milliseconds90* before January 1, 1970, 00:00:00 GMT.91*/92public void setTime(long date) {93// If the millisecond date value contains time info, mask it out.94super.setTime(date);95}9697/**98* Converts a string in JDBC date escape format to99* a <code>Date</code> value.100*101* @param s a <code>String</code> object representing a date in102* 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 the105* given date106* @throws IllegalArgumentException if the date given is not in the107* JDBC date escape format (yyyy-[m]m-[d]d)108*/109public static Date valueOf(String s) {110final int YEAR_LENGTH = 4;111final int MONTH_LENGTH = 2;112final int DAY_LENGTH = 2;113final int MAX_MONTH = 12;114final int MAX_DAY = 31;115int firstDash;116int secondDash;117Date d = null;118if (s == null) {119throw new java.lang.IllegalArgumentException();120}121122firstDash = s.indexOf('-');123secondDash = s.indexOf('-', firstDash + 1);124125if ((firstDash > 0) && (secondDash > 0) && (secondDash < s.length() - 1)) {126String yyyy = s.substring(0, firstDash);127String mm = s.substring(firstDash + 1, secondDash);128String dd = s.substring(secondDash + 1);129if (yyyy.length() == YEAR_LENGTH &&130(mm.length() >= 1 && mm.length() <= MONTH_LENGTH) &&131(dd.length() >= 1 && dd.length() <= DAY_LENGTH)) {132int year = Integer.parseInt(yyyy);133int month = Integer.parseInt(mm);134int day = Integer.parseInt(dd);135136if ((month >= 1 && month <= MAX_MONTH) && (day >= 1 && day <= MAX_DAY)) {137d = new Date(year - 1900, month - 1, day);138}139}140}141if (d == null) {142throw new java.lang.IllegalArgumentException();143}144145return d;146147}148149150/**151* Formats a date in the date escape format yyyy-mm-dd.152* <P>153* @return a String in yyyy-mm-dd format154*/155@SuppressWarnings("deprecation")156public String toString () {157int year = super.getYear() + 1900;158int month = super.getMonth() + 1;159int day = super.getDate();160161char buf[] = "2000-00-00".toCharArray();162buf[0] = Character.forDigit(year/1000,10);163buf[1] = Character.forDigit((year/100)%10,10);164buf[2] = Character.forDigit((year/10)%10,10);165buf[3] = Character.forDigit(year%10,10);166buf[5] = Character.forDigit(month/10,10);167buf[6] = Character.forDigit(month%10,10);168buf[8] = Character.forDigit(day/10,10);169buf[9] = Character.forDigit(day%10,10);170171return new String(buf);172}173174// Override all the time operations inherited from java.util.Date;175176/**177* This method is deprecated and should not be used because SQL Date178* values do not have a time component.179*180* @deprecated181* @exception java.lang.IllegalArgumentException if this method is invoked182* @see #setHours183*/184@Deprecated185public int getHours() {186throw new java.lang.IllegalArgumentException();187}188189/**190* This method is deprecated and should not be used because SQL Date191* values do not have a time component.192*193* @deprecated194* @exception java.lang.IllegalArgumentException if this method is invoked195* @see #setMinutes196*/197@Deprecated198public int getMinutes() {199throw new java.lang.IllegalArgumentException();200}201202/**203* This method is deprecated and should not be used because SQL Date204* values do not have a time component.205*206* @deprecated207* @exception java.lang.IllegalArgumentException if this method is invoked208* @see #setSeconds209*/210@Deprecated211public int getSeconds() {212throw new java.lang.IllegalArgumentException();213}214215/**216* This method is deprecated and should not be used because SQL Date217* values do not have a time component.218*219* @deprecated220* @exception java.lang.IllegalArgumentException if this method is invoked221* @see #getHours222*/223@Deprecated224public void setHours(int i) {225throw new java.lang.IllegalArgumentException();226}227228/**229* This method is deprecated and should not be used because SQL Date230* values do not have a time component.231*232* @deprecated233* @exception java.lang.IllegalArgumentException if this method is invoked234* @see #getMinutes235*/236@Deprecated237public void setMinutes(int i) {238throw new java.lang.IllegalArgumentException();239}240241/**242* This method is deprecated and should not be used because SQL Date243* values do not have a time component.244*245* @deprecated246* @exception java.lang.IllegalArgumentException if this method is invoked247* @see #getSeconds248*/249@Deprecated250public void setSeconds(int i) {251throw new java.lang.IllegalArgumentException();252}253254/**255* Private serial version unique ID to ensure serialization256* compatibility.257*/258static final long serialVersionUID = 1511598038487230103L;259260/**261* Obtains an instance of {@code Date} from a {@link LocalDate} object262* with the same year, month and day of month value as the given263* {@code LocalDate}.264* <p>265* The provided {@code LocalDate} is interpreted as the local date266* in the local time zone.267*268* @param date a {@code LocalDate} to convert269* @return a {@code Date} object270* @exception NullPointerException if {@code date} is null271* @since 1.8272*/273@SuppressWarnings("deprecation")274public static Date valueOf(LocalDate date) {275return new Date(date.getYear() - 1900, date.getMonthValue() -1,276date.getDayOfMonth());277}278279/**280* Converts this {@code Date} object to a {@code LocalDate}281* <p>282* The conversion creates a {@code LocalDate} that represents the same283* date value as this {@code Date} in local time zone284*285* @return a {@code LocalDate} object representing the same date value286*287* @since 1.8288*/289@SuppressWarnings("deprecation")290public LocalDate toLocalDate() {291return LocalDate.of(getYear() + 1900, getMonth() + 1, getDate());292}293294/**295* This method always throws an UnsupportedOperationException and should296* not be used because SQL {@code Date} values do not have a time297* component.298*299* @exception java.lang.UnsupportedOperationException if this method is invoked300*/301@Override302public Instant toInstant() {303throw new java.lang.UnsupportedOperationException();304}305}306307308