home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / sql / Date.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  5.8 KB  |  203 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)Date.java    1.18 98/09/30
  3.  *
  4.  * Copyright 1996-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.sql;
  16.  
  17. /**
  18.  * <P>A thin wrapper around a millisecond value that allows
  19.  * JDBC to identify this as a SQL DATE.  A milliseconds value represents
  20.  * the number of milliseconds that have passed since January 1, 1970
  21.  * 00:00:00.000 GMT.
  22.  * <p>
  23.  * To conform with the definition of SQL DATE, the millisecond values 
  24.  * wrapped by a java.sql.Date instance must be 'normalized' by setting the 
  25.  * hours, minutes, seconds, and milliseconds to zero in the particular
  26.  * time zone with which the instance is associated.
  27.  */
  28. public class Date extends java.util.Date {
  29.  
  30.     /**
  31.      * Constructs a <code>Date</code> object initialized with the given
  32.      * year, month, and day.
  33.      *
  34.      * @param year year-1900
  35.      * @param month 0 to 11 
  36.      * @param day 1 to 31
  37.      * @deprecated instead use the constructor <code>Date(long date)</code>
  38.      */
  39.     public Date(int year, int month, int day) {
  40.     super(year, month, day);
  41.     }
  42.  
  43.     /**
  44.      * Constructs a <code>Date</code> object 
  45.      * using a milliseconds time value.  If the given millisecond 
  46.      * value contains time information, the driver will set the
  47.      * time components to zero.
  48.      *
  49.      * @param date milliseconds since January 1, 1970, 00:00:00 GMT.
  50.                    A negative number indicates the number of milliseconds
  51.                    before January 1, 1970, 00:00:00 GMT.
  52.      */
  53.     public Date(long date) {
  54.     // If the millisecond date value contains time info, mask it out.
  55.     super(date);
  56.     
  57.     }
  58.  
  59.     /**
  60.      * Sets an existing <code>Date</code> object 
  61.      * using the given milliseconds time value.  If the given milliseconds 
  62.      * value contains time information, the driver will set the
  63.      * time components to zero.
  64.      *
  65.      * @param date milliseconds since January 1, 1970, 00:00:00 GMT.
  66.                    A negative number indicates the number of milliseconds
  67.                    before January 1, 1970, 00:00:00 GMT.
  68.      */
  69.     public void setTime(long date) {
  70.     // If the millisecond date value contains time info, mask it out.
  71.     super.setTime(date);     
  72.     }
  73.  
  74.     /**
  75.      * Converts a string in JDBC date escape format to
  76.      * a <code>Date</code> value.
  77.      *
  78.      * @param s date in format "yyyy-mm-dd"
  79.      * @return a <code>Date</code> object representing the given date
  80.      */
  81.     public static Date valueOf(String s) {
  82.     int year;
  83.     int month;
  84.     int day;
  85.     int firstDash;
  86.     int secondDash;
  87.  
  88.     if (s == null) throw new java.lang.IllegalArgumentException();
  89.  
  90.     firstDash = s.indexOf('-');
  91.     secondDash = s.indexOf('-', firstDash+1);
  92.     if ((firstDash > 0) & (secondDash > 0) & (secondDash < s.length()-1)) {
  93.         year = Integer.parseInt(s.substring(0, firstDash)) - 1900;
  94.         month = Integer.parseInt(s.substring(firstDash+1, secondDash)) - 1;
  95.         day = Integer.parseInt(s.substring(secondDash+1));     
  96.     } else {
  97.         throw new java.lang.IllegalArgumentException();
  98.     }
  99.             
  100.     return new Date(year, month, day);
  101.     }
  102.  
  103.     /**
  104.      * Formats a date in JDBC date escape format.  
  105.      *
  106.      * @return a String in yyyy-mm-dd format
  107.      */
  108.     public String toString () {
  109.     int year = super.getYear() + 1900;
  110.     int month = super.getMonth() + 1;
  111.     int day = super.getDate();
  112.     String yearString;
  113.     String monthString;
  114.     String dayString;
  115.  
  116.         
  117.     yearString = Integer.toString(year);
  118.  
  119.     if (month < 10) {
  120.         monthString = "0" + month;
  121.     } else {        
  122.         monthString = Integer.toString(month);
  123.     }
  124.  
  125.     if (day < 10) {
  126.         dayString = "0" + day;
  127.     } else {        
  128.         dayString = Integer.toString(day);
  129.     }
  130.  
  131.     return ( yearString + "-" + monthString + "-" + dayString);
  132.     }
  133.  
  134.     // Override all the time operations inherited from java.util.Date;
  135.  
  136.    /**
  137.     * This method is deprecated and should not be used because SQL Date 
  138.     * values do not have a time component.
  139.     *
  140.     * @deprecated
  141.     * @exception java.lang.IllegalArgumentException if this method is invoked
  142.     */
  143.     public int getHours() {
  144.     throw new java.lang.IllegalArgumentException();
  145.     }
  146.  
  147.    /**
  148.     * This method is deprecated and should not be used because SQL Date 
  149.     * values do not have a time component.
  150.     *
  151.     * @deprecated
  152.     * @exception java.lang.IllegalArgumentException if this method is invoked
  153.     */
  154.     public int getMinutes() {
  155.     throw new java.lang.IllegalArgumentException();
  156.     }
  157.     
  158.    /**
  159.     * This method is deprecated and should not be used because SQL Date 
  160.     * values do not have a time component.
  161.     *
  162.     * @deprecated
  163.     * @exception java.lang.IllegalArgumentException if this method is invoked
  164.     */
  165.     public int getSeconds() {
  166.     throw new java.lang.IllegalArgumentException();
  167.     }
  168.  
  169.    /**
  170.     * This method is deprecated and should not be used because SQL Date 
  171.     * values do not have a time component.
  172.     *
  173.     * @deprecated
  174.     * @exception java.lang.IllegalArgumentException if this method is invoked
  175.     */
  176.     public void setHours(int i) {
  177.     throw new java.lang.IllegalArgumentException();
  178.     }
  179.  
  180.    /**
  181.     * This method is deprecated and should not be used because SQL Date 
  182.     * values do not have a time component.
  183.     *
  184.     * @deprecated
  185.     * @exception java.lang.IllegalArgumentException if this method is invoked
  186.     */
  187.     public void setMinutes(int i) {
  188.     throw new java.lang.IllegalArgumentException();
  189.     }
  190.  
  191.    /**
  192.     * This method is deprecated and should not be used because SQL Date 
  193.     * values do not have a time component.
  194.     *
  195.     * @deprecated
  196.     * @exception java.lang.IllegalArgumentException if this method is invoked
  197.     */
  198.     public void setSeconds(int i) {
  199.     throw new java.lang.IllegalArgumentException();
  200.     }
  201. }
  202.  
  203.