1. /*
  2. * @(#)Time.java 1.30 03/01/27
  3. *
  4. * Copyright 2003 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. public Time(int hour, int minute, int second) {
  36. super(70, 0, 1, hour, minute, second);
  37. }
  38. /**
  39. * Constructs a <code>Time</code> object using a milliseconds time value.
  40. *
  41. * @param time milliseconds since January 1, 1970, 00:00:00 GMT;
  42. * a negative number is milliseconds before
  43. * January 1, 1970, 00:00:00 GMT
  44. */
  45. public Time(long time) {
  46. super(time);
  47. }
  48. /**
  49. * Sets a <code>Time</code> object using a milliseconds time value.
  50. *
  51. * @param time milliseconds since January 1, 1970, 00:00:00 GMT;
  52. * a negative number is milliseconds before
  53. * January 1, 1970, 00:00:00 GMT
  54. */
  55. public void setTime(long time) {
  56. super.setTime(time);
  57. }
  58. /**
  59. * Converts a string in JDBC time escape format to a <code>Time</code> value.
  60. *
  61. * @param s time in format "hh:mm:ss"
  62. * @return a corresponding <code>Time</code> object
  63. */
  64. public static Time valueOf(String s) {
  65. int hour;
  66. int minute;
  67. int second;
  68. int firstColon;
  69. int secondColon;
  70. if (s == null) throw new java.lang.IllegalArgumentException();
  71. firstColon = s.indexOf(':');
  72. secondColon = s.indexOf(':', firstColon+1);
  73. if ((firstColon > 0) & (secondColon > 0) &
  74. (secondColon < s.length()-1)) {
  75. hour = Integer.parseInt(s.substring(0, firstColon));
  76. minute =
  77. Integer.parseInt(s.substring(firstColon+1, secondColon));
  78. second = Integer.parseInt(s.substring(secondColon+1));
  79. } else {
  80. throw new java.lang.IllegalArgumentException();
  81. }
  82. return new Time(hour, minute, second);
  83. }
  84. /**
  85. * Formats a time in JDBC time escape format.
  86. *
  87. * @return a <code>String</code> in hh:mm:ss format
  88. */
  89. public String toString () {
  90. int hour = super.getHours();
  91. int minute = super.getMinutes();
  92. int second = super.getSeconds();
  93. String hourString;
  94. String minuteString;
  95. String secondString;
  96. if (hour < 10) {
  97. hourString = "0" + hour;
  98. } else {
  99. hourString = Integer.toString(hour);
  100. }
  101. if (minute < 10) {
  102. minuteString = "0" + minute;
  103. } else {
  104. minuteString = Integer.toString(minute);
  105. }
  106. if (second < 10) {
  107. secondString = "0" + second;
  108. } else {
  109. secondString = Integer.toString(second);
  110. }
  111. return (hourString + ":" + minuteString + ":" + secondString);
  112. }
  113. // Override all the date operations inherited from java.util.Date;
  114. /**
  115. * This method is deprecated and should not be used because SQL <code>TIME</code>
  116. * values do not have a year component.
  117. *
  118. * @deprecated
  119. * @exception java.lang.IllegalArgumentException if this
  120. * method is invoked
  121. * @see #setYear
  122. */
  123. public int getYear() {
  124. throw new java.lang.IllegalArgumentException();
  125. }
  126. /**
  127. * This method is deprecated and should not be used because SQL <code>TIME</code>
  128. * values do not have a month component.
  129. *
  130. * @deprecated
  131. * @exception java.lang.IllegalArgumentException if this
  132. * method is invoked
  133. * @see #setMonth
  134. */
  135. public int getMonth() {
  136. throw new java.lang.IllegalArgumentException();
  137. }
  138. /**
  139. * This method is deprecated and should not be used because SQL <code>TIME</code>
  140. * values do not have a day component.
  141. *
  142. * @deprecated
  143. * @exception java.lang.IllegalArgumentException if this
  144. * method is invoked
  145. */
  146. public int getDay() {
  147. throw new java.lang.IllegalArgumentException();
  148. }
  149. /**
  150. * This method is deprecated and should not be used because SQL <code>TIME</code>
  151. * values do not have a date component.
  152. *
  153. * @deprecated
  154. * @exception java.lang.IllegalArgumentException if this
  155. * method is invoked
  156. * @see #setDate
  157. */
  158. public int getDate() {
  159. throw new java.lang.IllegalArgumentException();
  160. }
  161. /**
  162. * This method is deprecated and should not be used because SQL <code>TIME</code>
  163. * values do not have a year component.
  164. *
  165. * @deprecated
  166. * @exception java.lang.IllegalArgumentException if this
  167. * method is invoked
  168. * @see #getYear
  169. */
  170. public void setYear(int i) {
  171. throw new java.lang.IllegalArgumentException();
  172. }
  173. /**
  174. * This method is deprecated and should not be used because SQL <code>TIME</code>
  175. * values do not have a month component.
  176. *
  177. * @deprecated
  178. * @exception java.lang.IllegalArgumentException if this
  179. * method is invoked
  180. * @see #getMonth
  181. */
  182. public void setMonth(int i) {
  183. throw new java.lang.IllegalArgumentException();
  184. }
  185. /**
  186. * This method is deprecated and should not be used because SQL <code>TIME</code>
  187. * values do not have a date component.
  188. *
  189. * @deprecated
  190. * @exception java.lang.IllegalArgumentException if this
  191. * method is invoked
  192. * @see #getDate
  193. */
  194. public void setDate(int i) {
  195. throw new java.lang.IllegalArgumentException();
  196. }
  197. /**
  198. * Private serial version unique ID to ensure serialization
  199. * compatibility.
  200. */
  201. static final long serialVersionUID = 8397324403548013681L;
  202. }