home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / JDBC / JDBC_011 / JAVA / SQL / TIME.JAV < prev    next >
Encoding:
Text File  |  1996-11-10  |  4.9 KB  |  173 lines

  1. /*
  2.  * Copyright (c) 1996 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "LICENSE"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  * 
  17.  * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
  18.  * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
  19.  * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
  20.  * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
  21.  * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
  22.  * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
  23.  * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES").  SUN
  24.  * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
  25.  * HIGH RISK ACTIVITIES.
  26.  */
  27.  
  28. package java.sql;
  29.  
  30. /**
  31.  * <P>This class is a thin wrapper around java.util.Date that allows
  32.  * JDBC to identify this as a SQL TIME value. It adds formatting and
  33.  * parsing operations to support the JDBC escape syntax for time
  34.  * values.
  35.  */
  36. public class Time extends java.util.Date {
  37.  
  38.     /**
  39.      * Construct a Time Object
  40.      *
  41.      * @param hour 0 to 23
  42.      * @param minute 0 to 59
  43.      * @param second 0 to 59
  44.      */
  45.     public Time(int hour, int minute, int second) {
  46.     super(70, 0, 1, hour, minute, second);
  47.     }
  48.    
  49.     /**
  50.      * Construct a Time using a milliseconds time value
  51.      *
  52.      * @param time milliseconds since January 1, 1970, 00:00:00 GMT
  53.      */
  54.     public Time(long time) {
  55.     // If the millisecond time value contains date info, mask it out.
  56.     super(time);
  57.     int hours = getHours();
  58.     int minutes = getMinutes();
  59.     int seconds = getSeconds();
  60.     super.setTime(0);
  61.     setHours(hours);
  62.     setMinutes(minutes);
  63.     setSeconds(seconds);
  64.     }
  65.  
  66.     /**
  67.      * Set a Time using a milliseconds time value
  68.      *
  69.      * @param time milliseconds since January 1, 1970, 00:00:00 GMT
  70.      */
  71.     public void setTime(long time) {
  72.     // If the millisecond time value contains date info, mask it out.
  73.     super.setTime(time);
  74.     int hours = getHours();
  75.     int minutes = getMinutes();
  76.     int seconds = getSeconds();
  77.     super.setTime(0);
  78.     setHours(hours);
  79.     setMinutes(minutes);
  80.     setSeconds(seconds);
  81.     }
  82.  
  83.     /**
  84.      * Convert a string in JDBC time escape format to a Time value
  85.      *
  86.      * @param s time in format "hh:mm:ss"
  87.      * @return corresponding Time
  88.      */
  89.     public static Time valueOf(String s) {
  90.     int hour;
  91.     int minute;
  92.     int second;
  93.     int firstColon;
  94.     int secondColon;
  95.  
  96.     if (s == null) throw new java.lang.IllegalArgumentException();
  97.  
  98.     firstColon = s.indexOf(':');
  99.     secondColon = s.indexOf(':', firstColon+1);
  100.     if ((firstColon > 0) & (secondColon > 0) & 
  101.         (secondColon < s.length()-1)) {
  102.         hour = Integer.parseInt(s.substring(0, firstColon));
  103.         minute = 
  104.         Integer.parseInt(s.substring(firstColon+1, secondColon));
  105.         second = Integer.parseInt(s.substring(secondColon+1));        
  106.     } else {
  107.         throw new java.lang.IllegalArgumentException();
  108.     }
  109.  
  110.     return new Time(hour, minute, second);
  111.     }
  112.    
  113.     /**
  114.      * Format a time in JDBC date escape format  
  115.      *
  116.      * @return a String in hh:mm:ss format
  117.      */
  118.     public String toString () {
  119.     int hour = super.getHours();
  120.     int minute = super.getMinutes();
  121.     int second = super.getSeconds();
  122.     String hourString;
  123.     String minuteString;
  124.     String secondString;
  125.  
  126.     if (hour < 10) {
  127.         hourString = "0" + hour;
  128.     } else {        
  129.         hourString = Integer.toString(hour);
  130.     }
  131.     if (minute < 10) {
  132.         minuteString = "0" + minute;
  133.     } else {        
  134.         minuteString = Integer.toString(minute);
  135.     }
  136.     if (second < 10) {
  137.         secondString = "0" + second;
  138.     } else {        
  139.         secondString = Integer.toString(second);
  140.     }
  141.     return (hourString + ":" + minuteString + ":" + secondString);
  142.     }
  143.  
  144.     // Override all the date operations inherited from java.util.Date;
  145.     public int getYear() {
  146.     throw new java.lang.IllegalArgumentException();
  147.     }
  148.  
  149.     public int getMonth() {
  150.     throw new java.lang.IllegalArgumentException();
  151.     }
  152.     
  153.     public int getDay() {
  154.     throw new java.lang.IllegalArgumentException();
  155.     }
  156.  
  157.     public int getDate() {
  158.     throw new java.lang.IllegalArgumentException();
  159.     }
  160.  
  161.     public void setYear(int i) {
  162.     throw new java.lang.IllegalArgumentException();
  163.     }
  164.  
  165.     public void setMonth(int i) {
  166.     throw new java.lang.IllegalArgumentException();
  167.     }
  168.  
  169.     public void setDate(int i) {
  170.     throw new java.lang.IllegalArgumentException();
  171.     }
  172. }
  173.