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 / Time.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  5.8 KB  |  209 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)Time.java    1.17 98/09/27
  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 <code>java.util.Date</code> that allows
  19.  * JDBC to identify this as a SQL TIME value. The <code>Time</code>
  20.  * class adds formatting and
  21.  * parsing operations to support the JDBC escape syntax for time
  22.  * values. 
  23.  * <p>The date components should be set to the "zero epoch"
  24.  * value of January 1, 1970 and should not be accessed. 
  25.  */
  26. public class Time extends java.util.Date {
  27.  
  28.     /**
  29.      * Constructs a <code>Time</code> object initialized with the 
  30.      * given values for the hour, minute, and second.
  31.      * The driver sets the date components to January 1, 1970.
  32.      * Any method that attempts to access the date components of a
  33.      * <code>Time</code> object will throw a
  34.      * <code>java.lang.IllegalArgumentException</code>.
  35.      *
  36.      * @param hour 0 to 23
  37.      * @param minute 0 to 59
  38.      * @param second 0 to 59
  39.      */
  40.     public Time(int hour, int minute, int second) {
  41.     super(70, 0, 1, hour, minute, second);
  42.     }
  43.    
  44.     /**
  45.      * Constructs a <code>Time</code> object using a milliseconds time value.
  46.      *
  47.      * @param time milliseconds since January 1, 1970, 00:00:00 GMT;
  48.      *             a negative number is milliseconds before
  49.      *               January 1, 1970, 00:00:00 GMT
  50.      */
  51.     public Time(long time) {
  52.     super(time);
  53.     }
  54.  
  55.     /**
  56.      * Sets a <code>Time</code> object using a milliseconds time value.
  57.      *
  58.      * @param time milliseconds since January 1, 1970, 00:00:00 GMT;
  59.      *             a negative number is milliseconds before
  60.      *               January 1, 1970, 00:00:00 GMT
  61.      */
  62.     public void setTime(long time) {
  63.     super.setTime(time);
  64.     }
  65.  
  66.     /**
  67.      * Converts a string in JDBC time escape format to a <code>Time</code> value.
  68.      *
  69.      * @param s time in format "hh:mm:ss"
  70.      * @return a corresponding <code>Time</code> object
  71.      */
  72.     public static Time valueOf(String s) {
  73.     int hour;
  74.     int minute;
  75.     int second;
  76.     int firstColon;
  77.     int secondColon;
  78.  
  79.     if (s == null) throw new java.lang.IllegalArgumentException();
  80.  
  81.     firstColon = s.indexOf(':');
  82.     secondColon = s.indexOf(':', firstColon+1);
  83.     if ((firstColon > 0) & (secondColon > 0) & 
  84.         (secondColon < s.length()-1)) {
  85.         hour = Integer.parseInt(s.substring(0, firstColon));
  86.         minute = 
  87.         Integer.parseInt(s.substring(firstColon+1, secondColon));
  88.         second = Integer.parseInt(s.substring(secondColon+1));        
  89.     } else {
  90.         throw new java.lang.IllegalArgumentException();
  91.     }
  92.  
  93.     return new Time(hour, minute, second);
  94.     }
  95.    
  96.     /**
  97.      * Format a time in JDBC date escape format  
  98.      *
  99.      * @return a String in hh:mm:ss format
  100.      */
  101.     public String toString () {
  102.     int hour = super.getHours();
  103.     int minute = super.getMinutes();
  104.     int second = super.getSeconds();
  105.     String hourString;
  106.     String minuteString;
  107.     String secondString;
  108.  
  109.     if (hour < 10) {
  110.         hourString = "0" + hour;
  111.     } else {        
  112.         hourString = Integer.toString(hour);
  113.     }
  114.     if (minute < 10) {
  115.         minuteString = "0" + minute;
  116.     } else {        
  117.         minuteString = Integer.toString(minute);
  118.     }
  119.     if (second < 10) {
  120.         secondString = "0" + second;
  121.     } else {        
  122.         secondString = Integer.toString(second);
  123.     }
  124.     return (hourString + ":" + minuteString + ":" + secondString);
  125.     }
  126.  
  127.     // Override all the date operations inherited from java.util.Date;
  128.  
  129.    /**
  130.     * This method is deprecated and should not be used because SQL Time 
  131.     * values do not have a year component.
  132.     *
  133.     * @deprecated
  134.     * @exception <code>java.lang.IllegalArgumentException</code> if this
  135.     */
  136.     public int getYear() {
  137.     throw new java.lang.IllegalArgumentException();
  138.     }
  139.  
  140.    /**
  141.     * This method is deprecated and should not be used because SQL Time 
  142.     * values do not have a month component.
  143.     *
  144.     * @deprecated
  145.     * @exception <code>java.lang.IllegalArgumentException</code> if this
  146.     */
  147.     public int getMonth() {
  148.     throw new java.lang.IllegalArgumentException();
  149.     }
  150.     
  151.    /**
  152.     * This method is deprecated and should not be used because SQL Time 
  153.     * values do not have a day component.
  154.     *
  155.     * @deprecated
  156.     * @exception <code>java.lang.IllegalArgumentException</code> if this
  157.     */
  158.     public int getDay() {
  159.     throw new java.lang.IllegalArgumentException();
  160.     }
  161.  
  162.    /**
  163.     * This method is deprecated and should not be used because SQL Time 
  164.     * values do not have a date component.
  165.     *
  166.     * @deprecated
  167.     * @exception <code>java.lang.IllegalArgumentException</code> if this
  168.     */
  169.     public int getDate() {
  170.     throw new java.lang.IllegalArgumentException();
  171.     }
  172.  
  173.    /**
  174.     * This method is deprecated and should not be used because SQL Time 
  175.     * values do not have a year component.
  176.     *
  177.     * @deprecated
  178.     * @exception <code>java.lang.IllegalArgumentException</code> if this
  179.     */
  180.     public void setYear(int i) {
  181.     throw new java.lang.IllegalArgumentException();
  182.     }
  183.  
  184.    /**
  185.     * This method is deprecated and should not be used because SQL Time 
  186.     * values do not have a month component.
  187.     *
  188.     * @deprecated
  189.     * @exception <code>java.lang.IllegalArgumentException</code> if this
  190.     */
  191.     public void setMonth(int i) {
  192.     throw new java.lang.IllegalArgumentException();
  193.     }
  194.  
  195.    /**
  196.     * This method is deprecated and should not be used because SQL Time 
  197.     * values do not have a date component.
  198.     *
  199.     * @deprecated
  200.     * @exception <code>java.lang.IllegalArgumentException</code> if this
  201.     */
  202.     public void setDate(int i) {
  203.     throw new java.lang.IllegalArgumentException();
  204.     }
  205. }
  206.  
  207.  
  208.  
  209.