0N/A/*
2751N/A * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.sql;
0N/A
0N/A/**
0N/A * <P>A thin wrapper around a millisecond value that allows
0N/A * JDBC to identify this as an SQL <code>DATE</code> value. A
0N/A * milliseconds value represents the number of milliseconds that
0N/A * have passed since January 1, 1970 00:00:00.000 GMT.
0N/A * <p>
0N/A * To conform with the definition of SQL <code>DATE</code>, the
0N/A * millisecond values wrapped by a <code>java.sql.Date</code> instance
0N/A * must be 'normalized' by setting the
0N/A * hours, minutes, seconds, and milliseconds to zero in the particular
0N/A * time zone with which the instance is associated.
0N/A */
0N/Apublic class Date extends java.util.Date {
0N/A
0N/A /**
0N/A * Constructs a <code>Date</code> object initialized with the given
0N/A * year, month, and day.
0N/A * <P>
0N/A * The result is undefined if a given argument is out of bounds.
0N/A *
0N/A * @param year the year minus 1900; must be 0 to 8099. (Note that
0N/A * 8099 is 9999 minus 1900.)
0N/A * @param month 0 to 11
0N/A * @param day 1 to 31
0N/A * @deprecated instead use the constructor <code>Date(long date)</code>
0N/A */
0N/A public Date(int year, int month, int day) {
0N/A super(year, month, day);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a <code>Date</code> object using the given milliseconds
0N/A * time value. If the given milliseconds value contains time
0N/A * information, the driver will set the time components to the
0N/A * time in the default time zone (the time zone of the Java virtual
0N/A * machine running the application) that corresponds to zero GMT.
0N/A *
0N/A * @param date milliseconds since January 1, 1970, 00:00:00 GMT not
0N/A * to exceed the milliseconds representation for the year 8099.
0N/A * A negative number indicates the number of milliseconds
0N/A * before January 1, 1970, 00:00:00 GMT.
0N/A */
0N/A public Date(long date) {
0N/A // If the millisecond date value contains time info, mask it out.
0N/A super(date);
0N/A
0N/A }
0N/A
0N/A /**
0N/A * Sets an existing <code>Date</code> object
0N/A * using the given milliseconds time value.
0N/A * If the given milliseconds value contains time information,
0N/A * the driver will set the time components to the
0N/A * time in the default time zone (the time zone of the Java virtual
0N/A * machine running the application) that corresponds to zero GMT.
0N/A *
0N/A * @param date milliseconds since January 1, 1970, 00:00:00 GMT not
0N/A * to exceed the milliseconds representation for the year 8099.
0N/A * A negative number indicates the number of milliseconds
0N/A * before January 1, 1970, 00:00:00 GMT.
0N/A */
0N/A public void setTime(long date) {
0N/A // If the millisecond date value contains time info, mask it out.
0N/A super.setTime(date);
0N/A }
0N/A
0N/A /**
0N/A * Converts a string in JDBC date escape format to
0N/A * a <code>Date</code> value.
0N/A *
0N/A * @param s a <code>String</code> object representing a date in
2751N/A * in the format "yyyy-[m]m-[d]d". The leading zero for <code>mm</code>
2751N/A * and <code>dd</code> may also be omitted.
0N/A * @return a <code>java.sql.Date</code> object representing the
0N/A * given date
0N/A * @throws IllegalArgumentException if the date given is not in the
2751N/A * JDBC date escape format (yyyy-[m]m-[d]d)
0N/A */
0N/A public static Date valueOf(String s) {
2652N/A final int YEAR_LENGTH = 4;
2652N/A final int MONTH_LENGTH = 2;
2652N/A final int DAY_LENGTH = 2;
2652N/A final int MAX_MONTH = 12;
2652N/A final int MAX_DAY = 31;
0N/A int firstDash;
0N/A int secondDash;
2652N/A Date d = null;
0N/A
2652N/A if (s == null) {
0N/A throw new java.lang.IllegalArgumentException();
0N/A }
0N/A
2652N/A firstDash = s.indexOf('-');
2652N/A secondDash = s.indexOf('-', firstDash + 1);
2652N/A
2652N/A if ((firstDash > 0) && (secondDash > 0) && (secondDash < s.length() - 1)) {
2652N/A String yyyy = s.substring(0, firstDash);
2652N/A String mm = s.substring(firstDash + 1, secondDash);
2652N/A String dd = s.substring(secondDash + 1);
2751N/A if (yyyy.length() == YEAR_LENGTH &&
2751N/A (mm.length() >= 1 && mm.length() <= MONTH_LENGTH) &&
2751N/A (dd.length() >= 1 && dd.length() <= DAY_LENGTH)) {
2652N/A int year = Integer.parseInt(yyyy);
2652N/A int month = Integer.parseInt(mm);
2652N/A int day = Integer.parseInt(dd);
2652N/A
2652N/A if ((month >= 1 && month <= MAX_MONTH) && (day >= 1 && day <= MAX_DAY)) {
2652N/A d = new Date(year - 1900, month - 1, day);
2652N/A }
2652N/A }
2652N/A }
2652N/A if (d == null) {
2652N/A throw new java.lang.IllegalArgumentException();
2652N/A }
2652N/A
2652N/A return d;
2652N/A
0N/A }
0N/A
2652N/A
0N/A /**
0N/A * Formats a date in the date escape format yyyy-mm-dd.
0N/A * <P>
0N/A * @return a String in yyyy-mm-dd format
0N/A */
0N/A public String toString () {
0N/A int year = super.getYear() + 1900;
0N/A int month = super.getMonth() + 1;
0N/A int day = super.getDate();
0N/A
0N/A char buf[] = "2000-00-00".toCharArray();
0N/A buf[0] = Character.forDigit(year/1000,10);
0N/A buf[1] = Character.forDigit((year/100)%10,10);
0N/A buf[2] = Character.forDigit((year/10)%10,10);
0N/A buf[3] = Character.forDigit(year%10,10);
0N/A buf[5] = Character.forDigit(month/10,10);
0N/A buf[6] = Character.forDigit(month%10,10);
0N/A buf[8] = Character.forDigit(day/10,10);
0N/A buf[9] = Character.forDigit(day%10,10);
0N/A
0N/A return new String(buf);
0N/A }
0N/A
0N/A // Override all the time operations inherited from java.util.Date;
0N/A
0N/A /**
0N/A * This method is deprecated and should not be used because SQL Date
0N/A * values do not have a time component.
0N/A *
0N/A * @deprecated
0N/A * @exception java.lang.IllegalArgumentException if this method is invoked
0N/A * @see #setHours
0N/A */
0N/A public int getHours() {
0N/A throw new java.lang.IllegalArgumentException();
0N/A }
0N/A
0N/A /**
0N/A * This method is deprecated and should not be used because SQL Date
0N/A * values do not have a time component.
0N/A *
0N/A * @deprecated
0N/A * @exception java.lang.IllegalArgumentException if this method is invoked
0N/A * @see #setMinutes
0N/A */
0N/A public int getMinutes() {
0N/A throw new java.lang.IllegalArgumentException();
0N/A }
0N/A
0N/A /**
0N/A * This method is deprecated and should not be used because SQL Date
0N/A * values do not have a time component.
0N/A *
0N/A * @deprecated
0N/A * @exception java.lang.IllegalArgumentException if this method is invoked
0N/A * @see #setSeconds
0N/A */
0N/A public int getSeconds() {
0N/A throw new java.lang.IllegalArgumentException();
0N/A }
0N/A
0N/A /**
0N/A * This method is deprecated and should not be used because SQL Date
0N/A * values do not have a time component.
0N/A *
0N/A * @deprecated
0N/A * @exception java.lang.IllegalArgumentException if this method is invoked
0N/A * @see #getHours
0N/A */
0N/A public void setHours(int i) {
0N/A throw new java.lang.IllegalArgumentException();
0N/A }
0N/A
0N/A /**
0N/A * This method is deprecated and should not be used because SQL Date
0N/A * values do not have a time component.
0N/A *
0N/A * @deprecated
0N/A * @exception java.lang.IllegalArgumentException if this method is invoked
0N/A * @see #getMinutes
0N/A */
0N/A public void setMinutes(int i) {
0N/A throw new java.lang.IllegalArgumentException();
0N/A }
0N/A
0N/A /**
0N/A * This method is deprecated and should not be used because SQL Date
0N/A * values do not have a time component.
0N/A *
0N/A * @deprecated
0N/A * @exception java.lang.IllegalArgumentException if this method is invoked
0N/A * @see #getSeconds
0N/A */
0N/A public void setSeconds(int i) {
0N/A throw new java.lang.IllegalArgumentException();
0N/A }
0N/A
0N/A /**
0N/A * Private serial version unique ID to ensure serialization
0N/A * compatibility.
0N/A */
0N/A static final long serialVersionUID = 1511598038487230103L;
0N/A}