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