1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * Copyright (c) 1996, 2004, Oracle and/or its affiliates. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. Oracle designates this 9 * particular file as subject to the "Classpath" exception as provided 10 * by Oracle in the LICENSE file that accompanied this code. 11 * 12 * This code is distributed in the hope that it will be useful, but WITHOUT 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15 * version 2 for more details (a copy is included in the LICENSE file that 16 * accompanied this code). 17 * 18 * You should have received a copy of the GNU General Public License version 19 * 2 along with this work; if not, write to the Free Software Foundation, 20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 21 * 22 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 23 * or visit www.oracle.com if you need additional information or have any 24 * questions. 25 */ 26 27 package java.sql; 28 29 /** 30 * <P>A thin wrapper around the <code>java.util.Date</code> class that allows the JDBC 31 * API to identify this as an SQL <code>TIME</code> value. The <code>Time</code> 32 * class adds formatting and 33 * parsing operations to support the JDBC escape syntax for time 34 * values. 35 * <p>The date components should be set to the "zero epoch" 36 * value of January 1, 1970 and should not be accessed. 37 */ 38 public class Time extends java.util.Date { 39 40 /** 41 * Constructs a <code>Time</code> object initialized with the 42 * given values for the hour, minute, and second. 43 * The driver sets the date components to January 1, 1970. 44 * Any method that attempts to access the date components of a 45 * <code>Time</code> object will throw a 46 * <code>java.lang.IllegalArgumentException</code>. 47 * <P> 48 * The result is undefined if a given argument is out of bounds. 49 * 50 * @param hour 0 to 23 51 * @param minute 0 to 59 52 * @param second 0 to 59 53 * 54 * @deprecated Use the constructor that takes a milliseconds value 55 * in place of this constructor 56 */ 57 @Deprecated Time(int hour, int minute, int second)58 public Time(int hour, int minute, int second) { 59 super(70, 0, 1, hour, minute, second); 60 } 61 62 /** 63 * Constructs a <code>Time</code> object using a milliseconds time value. 64 * 65 * @param time milliseconds since January 1, 1970, 00:00:00 GMT; 66 * a negative number is milliseconds before 67 * January 1, 1970, 00:00:00 GMT 68 */ Time(long time)69 public Time(long time) { 70 super(time); 71 } 72 73 /** 74 * Sets a <code>Time</code> object using a milliseconds time value. 75 * 76 * @param time milliseconds since January 1, 1970, 00:00:00 GMT; 77 * a negative number is milliseconds before 78 * January 1, 1970, 00:00:00 GMT 79 */ setTime(long time)80 public void setTime(long time) { 81 super.setTime(time); 82 } 83 84 /** 85 * Converts a string in JDBC time escape format to a <code>Time</code> value. 86 * 87 * @param s time in format "hh:mm:ss" 88 * @return a corresponding <code>Time</code> object 89 */ valueOf(String s)90 public static Time valueOf(String s) { 91 int hour; 92 int minute; 93 int second; 94 int firstColon; 95 int secondColon; 96 97 if (s == null) throw new java.lang.IllegalArgumentException(); 98 99 firstColon = s.indexOf(':'); 100 secondColon = s.indexOf(':', firstColon+1); 101 if ((firstColon > 0) & (secondColon > 0) & 102 (secondColon < s.length()-1)) { 103 hour = Integer.parseInt(s.substring(0, firstColon)); 104 minute = 105 Integer.parseInt(s.substring(firstColon+1, secondColon)); 106 second = Integer.parseInt(s.substring(secondColon+1)); 107 } else { 108 throw new java.lang.IllegalArgumentException(); 109 } 110 111 return new Time(hour, minute, second); 112 } 113 114 /** 115 * Formats a time in JDBC time escape format. 116 * 117 * @return a <code>String</code> in hh:mm:ss format 118 */ toString()119 public String toString () { 120 int hour = super.getHours(); 121 int minute = super.getMinutes(); 122 int second = super.getSeconds(); 123 String hourString; 124 String minuteString; 125 String secondString; 126 127 if (hour < 10) { 128 hourString = "0" + hour; 129 } else { 130 hourString = Integer.toString(hour); 131 } 132 if (minute < 10) { 133 minuteString = "0" + minute; 134 } else { 135 minuteString = Integer.toString(minute); 136 } 137 if (second < 10) { 138 secondString = "0" + second; 139 } else { 140 secondString = Integer.toString(second); 141 } 142 return (hourString + ":" + minuteString + ":" + secondString); 143 } 144 145 // Override all the date operations inherited from java.util.Date; 146 147 /** 148 * @deprecated This method is deprecated and should not be used because SQL <code>TIME</code> 149 * values do not have a year component. 150 * 151 * @exception java.lang.IllegalArgumentException if this 152 * method is invoked 153 * @see #setYear 154 */ 155 // Android-changed javadoc, @deprecated tag now has a reason. 156 @Deprecated getYear()157 public int getYear() { 158 throw new java.lang.IllegalArgumentException(); 159 } 160 161 /** 162 * @deprecated This method is deprecated and should not be used because SQL <code>TIME</code> 163 * values do not have a month component. 164 * 165 * @exception java.lang.IllegalArgumentException if this 166 * method is invoked 167 * @see #setMonth 168 */ 169 // Android-changed javadoc, @deprecated tag now has a reason. 170 @Deprecated getMonth()171 public int getMonth() { 172 throw new java.lang.IllegalArgumentException(); 173 } 174 175 /** 176 * @deprecated This method is deprecated and should not be used because SQL <code>TIME</code> 177 * values do not have a day component. 178 * 179 * @exception java.lang.IllegalArgumentException if this 180 * method is invoked 181 */ 182 // Android-changed javadoc, @deprecated tag now has a reason. 183 @Deprecated getDay()184 public int getDay() { 185 throw new java.lang.IllegalArgumentException(); 186 } 187 188 /** 189 * @deprecated This method is deprecated and should not be used because SQL <code>TIME</code> 190 * values do not have a date component. 191 * 192 * @exception java.lang.IllegalArgumentException if this 193 * method is invoked 194 * @see #setDate 195 */ 196 // Android-changed javadoc, @deprecated tag now has a reason. 197 @Deprecated getDate()198 public int getDate() { 199 throw new java.lang.IllegalArgumentException(); 200 } 201 202 /** 203 * @deprecated This method is deprecated and should not be used because SQL <code>TIME</code> 204 * values do not have a year component. 205 * 206 * @exception java.lang.IllegalArgumentException if this 207 * method is invoked 208 * @see #getYear 209 */ 210 // Android-changed javadoc, @deprecated tag now has a reason. 211 @Deprecated setYear(int i)212 public void setYear(int i) { 213 throw new java.lang.IllegalArgumentException(); 214 } 215 216 /** 217 * @deprecated This method is deprecated and should not be used because SQL <code>TIME</code> 218 * values do not have a month component. 219 * 220 * @exception java.lang.IllegalArgumentException if this 221 * method is invoked 222 * @see #getMonth 223 */ 224 // Android-changed javadoc, @deprecated tag now has a reason. 225 @Deprecated setMonth(int i)226 public void setMonth(int i) { 227 throw new java.lang.IllegalArgumentException(); 228 } 229 230 /** 231 * @deprecated This method is deprecated and should not be used because SQL <code>TIME</code> 232 * values do not have a date component. 233 * 234 * @exception java.lang.IllegalArgumentException if this 235 * method is invoked 236 * @see #getDate 237 */ 238 @Deprecated setDate(int i)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 } 249