home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / Time.java < prev    next >
Text File  |  1997-05-20  |  5KB  |  168 lines

  1. /*
  2.  * @(#)Time.java    1.6 96/11/23
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  * 
  21.  */
  22.  
  23. package java.sql;
  24.  
  25. /**
  26.  * <P>This class is a thin wrapper around java.util.Date that allows
  27.  * JDBC to identify this as a SQL TIME value. It adds formatting and
  28.  * parsing operations to support the JDBC escape syntax for time
  29.  * values.
  30.  */
  31. public class Time extends java.util.Date {
  32.  
  33.     /**
  34.      * Construct a Time Object
  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.      * Construct a Time using a milliseconds time value
  46.      *
  47.      * @param time milliseconds since January 1, 1970, 00:00:00 GMT
  48.      */
  49.     public Time(long time) {
  50.     // If the millisecond time value contains date info, mask it out.
  51.     super(time);
  52.     int hours = getHours();
  53.     int minutes = getMinutes();
  54.     int seconds = getSeconds();
  55.     super.setTime(0);
  56.     setHours(hours);
  57.     setMinutes(minutes);
  58.     setSeconds(seconds);
  59.     }
  60.  
  61.     /**
  62.      * Set a Time using a milliseconds time value
  63.      *
  64.      * @param time milliseconds since January 1, 1970, 00:00:00 GMT
  65.      */
  66.     public void setTime(long time) {
  67.     // If the millisecond time value contains date info, mask it out.
  68.     super.setTime(time);
  69.     int hours = getHours();
  70.     int minutes = getMinutes();
  71.     int seconds = getSeconds();
  72.     super.setTime(0);
  73.     setHours(hours);
  74.     setMinutes(minutes);
  75.     setSeconds(seconds);
  76.     }
  77.  
  78.     /**
  79.      * Convert a string in JDBC time escape format to a Time value
  80.      *
  81.      * @param s time in format "hh:mm:ss"
  82.      * @return corresponding Time
  83.      */
  84.     public static Time valueOf(String s) {
  85.     int hour;
  86.     int minute;
  87.     int second;
  88.     int firstColon;
  89.     int secondColon;
  90.  
  91.     if (s == null) throw new java.lang.IllegalArgumentException();
  92.  
  93.     firstColon = s.indexOf(':');
  94.     secondColon = s.indexOf(':', firstColon+1);
  95.     if ((firstColon > 0) & (secondColon > 0) & 
  96.         (secondColon < s.length()-1)) {
  97.         hour = Integer.parseInt(s.substring(0, firstColon));
  98.         minute = 
  99.         Integer.parseInt(s.substring(firstColon+1, secondColon));
  100.         second = Integer.parseInt(s.substring(secondColon+1));        
  101.     } else {
  102.         throw new java.lang.IllegalArgumentException();
  103.     }
  104.  
  105.     return new Time(hour, minute, second);
  106.     }
  107.    
  108.     /**
  109.      * Format a time in JDBC date escape format  
  110.      *
  111.      * @return a String in hh:mm:ss format
  112.      */
  113.     public String toString () {
  114.     int hour = super.getHours();
  115.     int minute = super.getMinutes();
  116.     int second = super.getSeconds();
  117.     String hourString;
  118.     String minuteString;
  119.     String secondString;
  120.  
  121.     if (hour < 10) {
  122.         hourString = "0" + hour;
  123.     } else {        
  124.         hourString = Integer.toString(hour);
  125.     }
  126.     if (minute < 10) {
  127.         minuteString = "0" + minute;
  128.     } else {        
  129.         minuteString = Integer.toString(minute);
  130.     }
  131.     if (second < 10) {
  132.         secondString = "0" + second;
  133.     } else {        
  134.         secondString = Integer.toString(second);
  135.     }
  136.     return (hourString + ":" + minuteString + ":" + secondString);
  137.     }
  138.  
  139.     // Override all the date operations inherited from java.util.Date;
  140.     public int getYear() {
  141.     throw new java.lang.IllegalArgumentException();
  142.     }
  143.  
  144.     public int getMonth() {
  145.     throw new java.lang.IllegalArgumentException();
  146.     }
  147.     
  148.     public int getDay() {
  149.     throw new java.lang.IllegalArgumentException();
  150.     }
  151.  
  152.     public int getDate() {
  153.     throw new java.lang.IllegalArgumentException();
  154.     }
  155.  
  156.     public void setYear(int i) {
  157.     throw new java.lang.IllegalArgumentException();
  158.     }
  159.  
  160.     public void setMonth(int i) {
  161.     throw new java.lang.IllegalArgumentException();
  162.     }
  163.  
  164.     public void setDate(int i) {
  165.     throw new java.lang.IllegalArgumentException();
  166.     }
  167. }
  168.