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