home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / sql / Date.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  3.9 KB  |  176 lines

  1. /*
  2.  * @(#)Date.java    1.10 98/03/18
  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>This class is a thin wrapper around java.util.Date that allows
  19.  * JDBC to identify this as a SQL DATE value. It adds formatting and
  20.  * parsing operations to support the JDBC escape syntax for date
  21.  * values.
  22.  */
  23. public class Date extends java.util.Date {
  24.  
  25.     /**
  26.      * Construct a Date  
  27.      *
  28.      * @param year year-1900
  29.      * @param month 0 to 11 
  30.      * @param day 1 to 31
  31.      * @deprecated
  32.      */
  33.     public Date(int year, int month, int day) {
  34.     super(year, month, day);
  35.     }
  36.  
  37.     /**
  38.      * Construct a Date using a milliseconds time value
  39.      *
  40.      * @param date milliseconds since January 1, 1970, 00:00:00 GMT
  41.      */
  42.     public Date(long date) {
  43.     // If the millisecond date value contains time info, mask it out.
  44.     super(date);
  45.     int year = getYear();
  46.     int month = getMonth();
  47.     int day = getDate();
  48.     super.setTime(0);
  49.     setYear(year);
  50.     setMonth(month);
  51.     setDate(day);
  52.     }
  53.  
  54.    /**
  55.      * Set a Date using a milliseconds time value
  56.      *
  57.      * @param date milliseconds since January 1, 1970, 00:00:00 GMT
  58.      */
  59.     public void setTime(long date) {
  60.     // If the millisecond date value contains time info, mask it out.
  61.     super.setTime(date);
  62.     int year = getYear();
  63.     int month = getMonth();
  64.     int day = getDate();
  65.     super.setTime(0);
  66.     setYear(year);
  67.     setMonth(month);
  68.     setDate(day);
  69.     }
  70.  
  71.     /**
  72.      * Convert a string in JDBC date escape format to a Date value
  73.      *
  74.      * @param s date in format "yyyy-mm-dd"
  75.      * @return corresponding Date
  76.      */
  77.     public static Date valueOf(String s) {
  78.     int year;
  79.     int month;
  80.     int day;
  81.     int firstDash;
  82.     int secondDash;
  83.  
  84.     if (s == null) throw new java.lang.IllegalArgumentException();
  85.  
  86.     firstDash = s.indexOf('-');
  87.     secondDash = s.indexOf('-', firstDash+1);
  88.     if ((firstDash > 0) & (secondDash > 0) & (secondDash < s.length()-1)) {
  89.         year = Integer.parseInt(s.substring(0, firstDash)) - 1900;
  90.         month = Integer.parseInt(s.substring(firstDash+1, secondDash)) - 1;
  91.         day = Integer.parseInt(s.substring(secondDash+1));     
  92.     } else {
  93.         throw new java.lang.IllegalArgumentException();
  94.     }
  95.             
  96.     return new Date(year, month, day);
  97.     }
  98.  
  99.     /**
  100.      * Format a date in JDBC date escape format  
  101.      *
  102.      * @return a String in yyyy-mm-dd format
  103.      */
  104.     public String toString () {
  105.     int year = super.getYear() + 1900;
  106.     int month = super.getMonth() + 1;
  107.     int day = super.getDate();
  108.     String yearString;
  109.     String monthString;
  110.     String dayString;
  111.  
  112.         
  113.     yearString = Integer.toString(year);
  114.  
  115.     if (month < 10) {
  116.         monthString = "0" + month;
  117.     } else {        
  118.         monthString = Integer.toString(month);
  119.     }
  120.  
  121.     if (day < 10) {
  122.         dayString = "0" + day;
  123.     } else {        
  124.         dayString = Integer.toString(day);
  125.     }
  126.  
  127.     return ( yearString + "-" + monthString + "-" + dayString);
  128.     }
  129.  
  130.     // Override all the time operations inherited from java.util.Date;
  131.  
  132.    /**
  133.     * @deprecated
  134.     */
  135.     public int getHours() {
  136.     throw new java.lang.IllegalArgumentException();
  137.     }
  138.  
  139.    /**
  140.     * @deprecated
  141.     */
  142.     public int getMinutes() {
  143.     throw new java.lang.IllegalArgumentException();
  144.     }
  145.     
  146.    /**
  147.     * @deprecated
  148.     */
  149.     public int getSeconds() {
  150.     throw new java.lang.IllegalArgumentException();
  151.     }
  152.  
  153.    /**
  154.     * @deprecated
  155.     */
  156.     public void setHours(int i) {
  157.     throw new java.lang.IllegalArgumentException();
  158.     }
  159.  
  160.    /**
  161.     * @deprecated
  162.     */
  163.     public void setMinutes(int i) {
  164.     throw new java.lang.IllegalArgumentException();
  165.     }
  166.  
  167.    /**
  168.     * @deprecated
  169.     */
  170.     public void setSeconds(int i) {
  171.     throw new java.lang.IllegalArgumentException();
  172.     }
  173.  
  174. }
  175.  
  176.