Time.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* Time.java -- Wrapper around java.util.Date
  2. Copyright (C) 1999, 2000, 2002, 2003, 2005, 2006
  3. Free Software Foundation, Inc.
  4. This file is part of GNU Classpath.
  5. GNU Classpath is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. GNU Classpath is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU Classpath; see the file COPYING. If not, write to the
  15. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301 USA.
  17. Linking this library statically or dynamically with other modules is
  18. making a combined work based on this library. Thus, the terms and
  19. conditions of the GNU General Public License cover the whole
  20. combination.
  21. As a special exception, the copyright holders of this library give you
  22. permission to link this library with independent modules to produce an
  23. executable, regardless of the license terms of these independent
  24. modules, and to copy and distribute the resulting executable under
  25. terms of your choice, provided that you also meet, for each linked
  26. independent module, the terms and conditions of the license of that
  27. module. An independent module is a module which is not derived from
  28. or based on this library. If you modify this library, you may extend
  29. this exception to your version of the library, but you are not
  30. obligated to do so. If you do not wish to do so, delete this
  31. exception statement from your version. */
  32. package java.sql;
  33. import java.text.ParseException;
  34. import java.text.SimpleDateFormat;
  35. /**
  36. * This class is a wrapper around java.util.Date to allow the JDBC
  37. * driver to identify the value as a SQL Time.
  38. *
  39. * @author Aaron M. Renn (arenn@urbanophile.com)
  40. */
  41. public class Time extends java.util.Date
  42. {
  43. static final long serialVersionUID = 8397324403548013681L;
  44. /**
  45. * Used for parsing and formatting this date.
  46. */
  47. private static SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
  48. /**
  49. * This method always throws an IllegalArgumentException.
  50. *
  51. * @throws IllegalArgumentException when it's called.
  52. * @deprecated
  53. */
  54. public int getDate() throws IllegalArgumentException
  55. {
  56. throw new IllegalArgumentException();
  57. }
  58. /**
  59. * This method always throws an IllegalArgumentException.
  60. *
  61. * @throws IllegalArgumentException when it's called.
  62. * @deprecated
  63. */
  64. public int getDay() throws IllegalArgumentException
  65. {
  66. throw new IllegalArgumentException();
  67. }
  68. /**
  69. * This method always throws an IllegalArgumentException.
  70. *
  71. * @throws IllegalArgumentException when it's called.
  72. * @deprecated
  73. */
  74. public int getMonth() throws IllegalArgumentException
  75. {
  76. throw new IllegalArgumentException();
  77. }
  78. /**
  79. * This method always throws an IllegalArgumentException.
  80. *
  81. * @throws IllegalArgumentException when it's called.
  82. * @deprecated
  83. */
  84. public int getYear() throws IllegalArgumentException
  85. {
  86. throw new IllegalArgumentException();
  87. }
  88. /**
  89. * This method always throws an IllegalArgumentException.
  90. *
  91. * @throws IllegalArgumentException when it's called.
  92. * @deprecated
  93. */
  94. public void setDate(int newValue) throws IllegalArgumentException
  95. {
  96. throw new IllegalArgumentException();
  97. }
  98. /**
  99. * This method always throws an IllegalArgumentException.
  100. *
  101. * @throws IllegalArgumentException when it's called.
  102. * @deprecated
  103. */
  104. public void setMonth(int newValue) throws IllegalArgumentException
  105. {
  106. throw new IllegalArgumentException();
  107. }
  108. /**
  109. * This method always throws an IllegalArgumentException.
  110. *
  111. * @throws IllegalArgumentException when it's called.
  112. * @deprecated
  113. */
  114. public void setYear(int newValue) throws IllegalArgumentException
  115. {
  116. throw new IllegalArgumentException();
  117. }
  118. /**
  119. * This method returns a new instance of this class by parsing a
  120. * date in JDBC format into a Java date.
  121. *
  122. * @param str The string to parse.
  123. * @return The resulting <code>java.sql.Time</code> value.
  124. */
  125. public static Time valueOf (String str)
  126. {
  127. try
  128. {
  129. java.util.Date d = (java.util.Date) sdf.parseObject(str);
  130. if (d == null)
  131. throw new IllegalArgumentException(str);
  132. else
  133. return new Time(d.getTime());
  134. }
  135. catch (ParseException e)
  136. {
  137. throw new IllegalArgumentException(str);
  138. }
  139. }
  140. /**
  141. * This method initializes a new instance of this class with the
  142. * specified year, month, and day.
  143. *
  144. * @param hour The hour for this Time (0-23)
  145. * @param minute The minute for this time (0-59)
  146. * @param second The second for this time (0-59)
  147. * @deprecated
  148. */
  149. public Time(int hour, int minute, int second)
  150. {
  151. super(System.currentTimeMillis());
  152. setHours(hour);
  153. setMinutes(minute);
  154. setSeconds(second);
  155. }
  156. /**
  157. * This method initializes a new instance of this class with the
  158. * specified time value representing the number of milliseconds since
  159. * Jan 1, 1970 at 12:00 midnight GMT.
  160. *
  161. * @param date The time value to intialize this <code>Time</code> to.
  162. */
  163. public Time(long date)
  164. {
  165. super(date);
  166. }
  167. /**
  168. * This method returns this date in JDBC format.
  169. *
  170. * @return This date as a string.
  171. */
  172. public String toString ()
  173. {
  174. return sdf.format (this);
  175. }
  176. }