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

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