1. /*
  2. * @(#)Time.java 1.22 00/02/02
  3. *
  4. * Copyright 1996-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.sql;
  11. /**
  12. * <P>A thin wrapper around <code>java.util.Date</code> that allows
  13. * JDBC to identify this as a SQL <code>TIME</code> value. The <code>Time</code>
  14. * class adds formatting and
  15. * parsing operations to support the JDBC escape syntax for time
  16. * values.
  17. * <p>The date components should be set to the "zero epoch"
  18. * value of January 1, 1970 and should not be accessed.
  19. */
  20. public class Time extends java.util.Date {
  21. /**
  22. * Constructs a <code>Time</code> object initialized with the
  23. * given values for the hour, minute, and second.
  24. * The driver sets the date components to January 1, 1970.
  25. * Any method that attempts to access the date components of a
  26. * <code>Time</code> object will throw a
  27. * <code>java.lang.IllegalArgumentException</code>.
  28. *
  29. * @param hour 0 to 23
  30. * @param minute 0 to 59
  31. * @param second 0 to 59
  32. *
  33. * @deprecated Use the constructor that takes a milliseconds value
  34. * in place of this constructor
  35. */
  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 date 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. */
  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. */
  134. public int getMonth() {
  135. throw new java.lang.IllegalArgumentException();
  136. }
  137. /**
  138. * This method is deprecated and should not be used because SQL <code>TIME</code>
  139. * values do not have a day component.
  140. *
  141. * @deprecated
  142. * @exception java.lang.IllegalArgumentException if this
  143. * method is invoked
  144. */
  145. public int getDay() {
  146. throw new java.lang.IllegalArgumentException();
  147. }
  148. /**
  149. * This method is deprecated and should not be used because SQL <code>TIME</code>
  150. * values do not have a date component.
  151. *
  152. * @deprecated
  153. * @exception java.lang.IllegalArgumentException if this
  154. * method is invoked
  155. */
  156. public int getDate() {
  157. throw new java.lang.IllegalArgumentException();
  158. }
  159. /**
  160. * This method is deprecated and should not be used because SQL <code>TIME</code>
  161. * values do not have a year component.
  162. *
  163. * @deprecated
  164. * @exception java.lang.IllegalArgumentException if this
  165. * method is invoked
  166. */
  167. public void setYear(int i) {
  168. throw new java.lang.IllegalArgumentException();
  169. }
  170. /**
  171. * This method is deprecated and should not be used because SQL <code>TIME</code>
  172. * values do not have a month component.
  173. *
  174. * @deprecated
  175. * @exception java.lang.IllegalArgumentException if this
  176. * method is invoked
  177. */
  178. public void setMonth(int i) {
  179. throw new java.lang.IllegalArgumentException();
  180. }
  181. /**
  182. * This method is deprecated and should not be used because SQL <code>TIME</code>
  183. * values do not have a date component.
  184. *
  185. * @deprecated
  186. * @exception java.lang.IllegalArgumentException if this
  187. * method is invoked
  188. */
  189. public void setDate(int i) {
  190. throw new java.lang.IllegalArgumentException();
  191. }
  192. }