1 /* 2 * Copyright (c) 1996, 2004, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.sql; 27 28 /** 29 * <P>A thin wrapper around the <code>java.util.Date</code> class that allows the JDBC 30 * API to identify this as an SQL <code>TIME</code> value. The <code>Time</code> 31 * class adds formatting and 32 * parsing operations to support the JDBC escape syntax for time 33 * values. 34 * <p>The date components should be set to the "zero epoch" 35 * value of January 1, 1970 and should not be accessed. 36 */ 37 public class Time extends java.util.Date { 38 39 /** 40 * Constructs a <code>Time</code> object initialized with the 41 * given values for the hour, minute, and second. 42 * The driver sets the date components to January 1, 1970. 43 * Any method that attempts to access the date components of a 44 * <code>Time</code> object will throw a 45 * <code>java.lang.IllegalArgumentException</code>. 46 * <P> 47 * The result is undefined if a given argument is out of bounds. 48 * 49 * @param hour 0 to 23 50 * @param minute 0 to 59 51 * @param second 0 to 59 52 * 53 * @deprecated Use the constructor that takes a milliseconds value 54 * in place of this constructor 55 */ 56 @Deprecated 57 public Time(int hour, int minute, int second) { 58 super(70, 0, 1, hour, minute, second); 59 } 60 61 /** 62 * Constructs a <code>Time</code> object using a milliseconds time value. 63 * 64 * @param time milliseconds since January 1, 1970, 00:00:00 GMT; 65 * a negative number is milliseconds before 66 * January 1, 1970, 00:00:00 GMT 67 */ 68 public Time(long time) { 69 super(time); 70 } 71 72 /** 73 * Sets a <code>Time</code> object using a milliseconds time value. 74 * 75 * @param time milliseconds since January 1, 1970, 00:00:00 GMT; 76 * a negative number is milliseconds before 77 * January 1, 1970, 00:00:00 GMT 78 */ 79 public void setTime(long time) { 80 super.setTime(time); 81 } 82 83 /** 84 * Converts a string in JDBC time escape format to a <code>Time</code> value. 85 * 86 * @param s time in format "hh:mm:ss" 87 * @return a corresponding <code>Time</code> object 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 * Formats a time in JDBC time escape format. 115 * 116 * @return a <code>String</code> 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 146 /** 147 * This method is deprecated and should not be used because SQL <code>TIME</code> 148 * values do not have a year component. 149 * 150 * @deprecated 151 * @exception java.lang.IllegalArgumentException if this 152 * method is invoked 153 * @see #setYear 154 */ 155 @Deprecated 156 public int getYear() { 157 throw new java.lang.IllegalArgumentException(); 158 } 159 160 /** 161 * This method is deprecated and should not be used because SQL <code>TIME</code> 162 * values do not have a month component. 163 * 164 * @deprecated 165 * @exception java.lang.IllegalArgumentException if this 166 * method is invoked 167 * @see #setMonth 168 */ 169 @Deprecated 170 public int getMonth() { 171 throw new java.lang.IllegalArgumentException(); 172 } 173 174 /** 175 * This method is deprecated and should not be used because SQL <code>TIME</code> 176 * values do not have a day component. 177 * 178 * @deprecated 179 * @exception java.lang.IllegalArgumentException if this 180 * method is invoked 181 */ 182 @Deprecated 183 public int getDay() { 184 throw new java.lang.IllegalArgumentException(); 185 } 186 187 /** 188 * This method is deprecated and should not be used because SQL <code>TIME</code> 189 * values do not have a date component. 190 * 191 * @deprecated 192 * @exception java.lang.IllegalArgumentException if this 193 * method is invoked 194 * @see #setDate 195 */ 196 @Deprecated 197 public int getDate() { 198 throw new java.lang.IllegalArgumentException(); 199 } 200 201 /** 202 * This method is deprecated and should not be used because SQL <code>TIME</code> 203 * values do not have a year component. 204 * 205 * @deprecated 206 * @exception java.lang.IllegalArgumentException if this 207 * method is invoked 208 * @see #getYear 209 */ 210 @Deprecated 211 public void setYear(int i) { 212 throw new java.lang.IllegalArgumentException(); 213 } 214 215 /** 216 * This method is deprecated and should not be used because SQL <code>TIME</code> 217 * values do not have a month component. 218 * 219 * @deprecated 220 * @exception java.lang.IllegalArgumentException if this 221 * method is invoked 222 * @see #getMonth 223 */ 224 @Deprecated 225 public void setMonth(int i) { 226 throw new java.lang.IllegalArgumentException(); 227 } 228 229 /** 230 * This method is deprecated and should not be used because SQL <code>TIME</code> 231 * values do not have a date component. 232 * 233 * @deprecated 234 * @exception java.lang.IllegalArgumentException if this 235 * method is invoked 236 * @see #getDate 237 */ 238 @Deprecated 239 public void setDate(int i) { 240 throw new java.lang.IllegalArgumentException(); 241 } 242 243 /** 244 * Private serial version unique ID to ensure serialization 245 * compatibility. 246 */ 247 static final long serialVersionUID = 8397324403548013681L; 248 }