1. /*
  2. * @(#)Time.java 1.32 04/05/18
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.sql;
  8. /**
  9. * <P>A thin wrapper around the <code>java.util.Date</code> class that allows the JDBC
  10. * API to identify this as an SQL <code>TIME</code> value. The <code>Time</code>
  11. * class adds formatting and
  12. * parsing operations to support the JDBC escape syntax for time
  13. * values.
  14. * <p>The date components should be set to the "zero epoch"
  15. * value of January 1, 1970 and should not be accessed.
  16. */
  17. public class Time extends java.util.Date {
  18. /**
  19. * Constructs a <code>Time</code> object initialized with the
  20. * given values for the hour, minute, and second.
  21. * The driver sets the date components to January 1, 1970.
  22. * Any method that attempts to access the date components of a
  23. * <code>Time</code> object will throw a
  24. * <code>java.lang.IllegalArgumentException</code>.
  25. * <P>
  26. * The result is undefined if a given argument is out of bounds.
  27. *
  28. * @param hour 0 to 23
  29. * @param minute 0 to 59
  30. * @param second 0 to 59
  31. *
  32. * @deprecated Use the constructor that takes a milliseconds value
  33. * in place of this constructor
  34. */
  35. @Deprecated
  36. public Time(int hour, int minute, int second) {
  37. super(70, 0, 1, hour, minute, second);
  38. }
  39. /**
  40. * Constructs a <code>Time</code> object using a milliseconds time value.
  41. *
  42. * @param time milliseconds since January 1, 1970, 00:00:00 GMT;
  43. * a negative number is milliseconds before
  44. * January 1, 1970, 00:00:00 GMT
  45. */
  46. public Time(long time) {
  47. super(time);
  48. }
  49. /**
  50. * Sets a <code>Time</code> object using a milliseconds time value.
  51. *
  52. * @param time milliseconds since January 1, 1970, 00:00:00 GMT;
  53. * a negative number is milliseconds before
  54. * January 1, 1970, 00:00:00 GMT
  55. */
  56. public void setTime(long time) {
  57. super.setTime(time);
  58. }
  59. /**
  60. * Converts a string in JDBC time escape format to a <code>Time</code> value.
  61. *
  62. * @param s time in format "hh:mm:ss"
  63. * @return a corresponding <code>Time</code> object
  64. */
  65. public static Time valueOf(String s) {
  66. int hour;
  67. int minute;
  68. int second;
  69. int firstColon;
  70. int secondColon;
  71. if (s == null) throw new java.lang.IllegalArgumentException();
  72. firstColon = s.indexOf(':');
  73. secondColon = s.indexOf(':', firstColon+1);
  74. if ((firstColon > 0) & (secondColon > 0) &
  75. (secondColon < s.length()-1)) {
  76. hour = Integer.parseInt(s.substring(0, firstColon));
  77. minute =
  78. Integer.parseInt(s.substring(firstColon+1, secondColon));
  79. second = Integer.parseInt(s.substring(secondColon+1));
  80. } else {
  81. throw new java.lang.IllegalArgumentException();
  82. }
  83. return new Time(hour, minute, second);
  84. }
  85. /**
  86. * Formats a time in JDBC time escape format.
  87. *
  88. * @return a <code>String</code> in hh:mm:ss format
  89. */
  90. public String toString () {
  91. int hour = super.getHours();
  92. int minute = super.getMinutes();
  93. int second = super.getSeconds();
  94. String hourString;
  95. String minuteString;
  96. String secondString;
  97. if (hour < 10) {
  98. hourString = "0" + hour;
  99. } else {
  100. hourString = Integer.toString(hour);
  101. }
  102. if (minute < 10) {
  103. minuteString = "0" + minute;
  104. } else {
  105. minuteString = Integer.toString(minute);
  106. }
  107. if (second < 10) {
  108. secondString = "0" + second;
  109. } else {
  110. secondString = Integer.toString(second);
  111. }
  112. return (hourString + ":" + minuteString + ":" + secondString);
  113. }
  114. // Override all the date operations inherited from java.util.Date;
  115. /**
  116. * This method is deprecated and should not be used because SQL <code>TIME</code>
  117. * values do not have a year component.
  118. *
  119. * @deprecated
  120. * @exception java.lang.IllegalArgumentException if this
  121. * method is invoked
  122. * @see #setYear
  123. */
  124. @Deprecated
  125. public int getYear() {
  126. throw new java.lang.IllegalArgumentException();
  127. }
  128. /**
  129. * This method is deprecated and should not be used because SQL <code>TIME</code>
  130. * values do not have a month component.
  131. *
  132. * @deprecated
  133. * @exception java.lang.IllegalArgumentException if this
  134. * method is invoked
  135. * @see #setMonth
  136. */
  137. @Deprecated
  138. public int getMonth() {
  139. throw new java.lang.IllegalArgumentException();
  140. }
  141. /**
  142. * This method is deprecated and should not be used because SQL <code>TIME</code>
  143. * values do not have a day component.
  144. *
  145. * @deprecated
  146. * @exception java.lang.IllegalArgumentException if this
  147. * method is invoked
  148. */
  149. @Deprecated
  150. public int getDay() {
  151. throw new java.lang.IllegalArgumentException();
  152. }
  153. /**
  154. * This method is deprecated and should not be used because SQL <code>TIME</code>
  155. * values do not have a date component.
  156. *
  157. * @deprecated
  158. * @exception java.lang.IllegalArgumentException if this
  159. * method is invoked
  160. * @see #setDate
  161. */
  162. @Deprecated
  163. public int getDate() {
  164. throw new java.lang.IllegalArgumentException();
  165. }
  166. /**
  167. * This method is deprecated and should not be used because SQL <code>TIME</code>
  168. * values do not have a year component.
  169. *
  170. * @deprecated
  171. * @exception java.lang.IllegalArgumentException if this
  172. * method is invoked
  173. * @see #getYear
  174. */
  175. @Deprecated
  176. public void setYear(int i) {
  177. throw new java.lang.IllegalArgumentException();
  178. }
  179. /**
  180. * This method is deprecated and should not be used because SQL <code>TIME</code>
  181. * values do not have a month component.
  182. *
  183. * @deprecated
  184. * @exception java.lang.IllegalArgumentException if this
  185. * method is invoked
  186. * @see #getMonth
  187. */
  188. @Deprecated
  189. public void setMonth(int i) {
  190. throw new java.lang.IllegalArgumentException();
  191. }
  192. /**
  193. * This method is deprecated and should not be used because SQL <code>TIME</code>
  194. * values do not have a date component.
  195. *
  196. * @deprecated
  197. * @exception java.lang.IllegalArgumentException if this
  198. * method is invoked
  199. * @see #getDate
  200. */
  201. @Deprecated
  202. public void setDate(int i) {
  203. throw new java.lang.IllegalArgumentException();
  204. }
  205. /**
  206. * Private serial version unique ID to ensure serialization
  207. * compatibility.
  208. */
  209. static final long serialVersionUID = 8397324403548013681L;
  210. }