/libcore/sql/src/main/java/java/sql/Time.java

https://github.com/truedat101/fiskidagur · Java · 240 lines · 80 code · 16 blank · 144 comment · 7 complexity · ed4275d48d98398b5a652ad2cfabc7a2 MD5 · raw file

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package java.sql;
  18. import java.text.SimpleDateFormat;
  19. import java.util.Date;
  20. /**
  21. * Java representation of an SQL {@code TIME} value. Provides utilities to
  22. * format and parse the time's representation as a String in JDBC escape format.
  23. *
  24. * @since Android 1.0
  25. */
  26. public class Time extends Date {
  27. private static final long serialVersionUID = 8397324403548013681L;
  28. /**
  29. * Constructs a {@code Time} object using the supplied values for <i>Hour</i>,
  30. * <i>Minute</i> and <i>Second</i>. The <i>Year</i>, <i>Month</i> and
  31. * <i>Day</i> elements of the {@code Time} object are set to the date
  32. * of the Epoch (January 1, 1970).
  33. * <p>
  34. * Any attempt to access the <i>Year</i>, <i>Month</i> or <i>Day</i>
  35. * elements of a {@code Time} object will result in an {@code
  36. * IllegalArgumentException}.
  37. * </p><p>
  38. * The result is undefined if any argument is out of bounds.
  39. * </p>
  40. *
  41. * @deprecated Please use the constructor {@link #Time(long)}.
  42. * @param theHour
  43. * a value in the range {@code [0,23]}.
  44. * @param theMinute
  45. * a value in the range {@code [0,59]}.
  46. * @param theSecond
  47. * a value in the range {@code [0,59]}.
  48. * @since Android 1.0
  49. */
  50. @SuppressWarnings("deprecation")
  51. @Deprecated
  52. public Time(int theHour, int theMinute, int theSecond) {
  53. super(70, 0, 1, theHour, theMinute, theSecond);
  54. }
  55. /**
  56. * Constructs a {@code Time} object using a supplied time specified in
  57. * milliseconds.
  58. *
  59. * @param theTime
  60. * a {@code Time} specified in milliseconds since the
  61. * <i>Epoch</i> (January 1st 1970, 00:00:00.000).
  62. * @since Android 1.0
  63. */
  64. public Time(long theTime) {
  65. super(theTime);
  66. }
  67. /**
  68. * @deprecated This method is deprecated and must not be used. An SQL
  69. * {@code Time} object does not have a {@code Date} component.
  70. * @return does not return anything.
  71. * @throws IllegalArgumentException
  72. * if this method is called.
  73. * @since Android 1.0
  74. */
  75. @SuppressWarnings("deprecation")
  76. @Deprecated
  77. @Override
  78. public int getDate() {
  79. throw new IllegalArgumentException();
  80. }
  81. /**
  82. * @deprecated This method is deprecated and must not be used. An SQL
  83. * {@code Time} object does not have a <i>Day</i> component.
  84. * @return does not return anything.
  85. * @throws IllegalArgumentException
  86. * if this method is called.
  87. * @since Android 1.0
  88. */
  89. @SuppressWarnings("deprecation")
  90. @Deprecated
  91. @Override
  92. public int getDay() {
  93. throw new IllegalArgumentException();
  94. }
  95. /**
  96. * @deprecated This method is deprecated and must not be used. An SQL
  97. * {@code Time} object does not have a <i>Month</i> component.
  98. * @return does not return anything.
  99. * @throws IllegalArgumentException
  100. * if this method is called.
  101. * @since Android 1.0
  102. */
  103. @SuppressWarnings("deprecation")
  104. @Deprecated
  105. @Override
  106. public int getMonth() {
  107. throw new IllegalArgumentException();
  108. }
  109. /**
  110. * @deprecated This method is deprecated and must not be used. An SQL
  111. * {@code Time} object does not have a <i>Year</i> component.
  112. * @return does not return anything.
  113. * @throws IllegalArgumentException
  114. * if this method is called.
  115. * @since Android 1.0
  116. */
  117. @SuppressWarnings("deprecation")
  118. @Deprecated
  119. @Override
  120. public int getYear() {
  121. throw new IllegalArgumentException();
  122. }
  123. /**
  124. * @deprecated This method is deprecated and must not be used. An SQL
  125. * {@code Time} object does not have a {@code Date} component.
  126. * @throws IllegalArgumentException
  127. * if this method is called.
  128. * @since Android 1.0
  129. */
  130. @SuppressWarnings("deprecation")
  131. @Deprecated
  132. @Override
  133. public void setDate(int i) {
  134. throw new IllegalArgumentException();
  135. }
  136. /**
  137. * @deprecated This method is deprecated and must not be used. An SQL
  138. * {@code Time} object does not have a <i>Month</i> component.
  139. * @throws IllegalArgumentException
  140. * if this method is called.
  141. * @since Android 1.0
  142. */
  143. @SuppressWarnings("deprecation")
  144. @Deprecated
  145. @Override
  146. public void setMonth(int i) {
  147. throw new IllegalArgumentException();
  148. }
  149. /**
  150. * @deprecated This method is deprecated and must not be used. An SQL
  151. * {@code Time} object does not have a <i>Year</i> component.
  152. * @throws IllegalArgumentException
  153. * if this method is called.
  154. */
  155. @SuppressWarnings("deprecation")
  156. @Deprecated
  157. @Override
  158. public void setYear(int i) {
  159. throw new IllegalArgumentException();
  160. }
  161. /**
  162. * Sets the time for this {@code Time} object to the supplied milliseconds
  163. * value.
  164. *
  165. * @param time
  166. * A time value expressed as milliseconds since the <i>Epoch</i>.
  167. * Negative values are milliseconds before the Epoch. The Epoch
  168. * is January 1 1970, 00:00:00.000.
  169. * @since Android 1.0
  170. */
  171. @Override
  172. public void setTime(long time) {
  173. super.setTime(time);
  174. }
  175. /**
  176. * Formats the {@code Time} as a String in JDBC escape format: {@code
  177. * hh:mm:ss}.
  178. *
  179. * @return A String representing the {@code Time} value in JDBC escape
  180. * format: {@code HH:mm:ss}
  181. * @since Android 1.0
  182. */
  183. @Override
  184. public String toString() {
  185. SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); //$NON-NLS-1$
  186. return dateFormat.format(this);
  187. }
  188. /**
  189. * Creates a {@code Time} object from a string holding a time represented in
  190. * JDBC escape format: {@code hh:mm:ss}.
  191. * <p>
  192. * An exception occurs if the input string does not comply with this format.
  193. * </p>
  194. *
  195. * @param timeString
  196. * A String representing the time value in JDBC escape format:
  197. * {@code hh:mm:ss}.
  198. * @return The {@code Time} object set to a time corresponding to the given
  199. * time.
  200. * @throws IllegalArgumentException
  201. * if the supplied time string is not in JDBC escape format.
  202. * @since Android 1.0
  203. */
  204. public static Time valueOf(String timeString) {
  205. if (timeString == null) {
  206. throw new IllegalArgumentException();
  207. }
  208. int firstIndex = timeString.indexOf(':');
  209. int secondIndex = timeString.indexOf(':', firstIndex + 1);
  210. // secondIndex == -1 means none or only one separator '-' has been found.
  211. // The string is separated into three parts by two separator characters,
  212. // if the first or the third part is null string, we should throw
  213. // IllegalArgumentException to follow RI
  214. if (secondIndex == -1|| firstIndex == 0 || secondIndex + 1 == timeString.length()) {
  215. throw new IllegalArgumentException();
  216. }
  217. // parse each part of the string
  218. int hour = Integer.parseInt(timeString.substring(0, firstIndex));
  219. int minute = Integer.parseInt(timeString.substring(firstIndex + 1, secondIndex));
  220. int second = Integer.parseInt(timeString.substring(secondIndex + 1, timeString
  221. .length()));
  222. return new Time(hour, minute, second);
  223. }
  224. }