/android/upstream/java/sql/Time.java

https://bitbucket.org/festevezga/xobotos · Java · 247 lines · 94 code · 20 blank · 133 comment · 9 complexity · b0e06fc388510869762352a348d99ed3 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.util.Date;
  19. /**
  20. * Java representation of an SQL {@code TIME} value. Provides utilities to
  21. * format and parse the time's representation as a String in JDBC escape format.
  22. */
  23. public class Time extends Date {
  24. private static final long serialVersionUID = 8397324403548013681L;
  25. /**
  26. * Constructs a {@code Time} object using the supplied values for <i>Hour</i>,
  27. * <i>Minute</i> and <i>Second</i>. The <i>Year</i>, <i>Month</i> and
  28. * <i>Day</i> elements of the {@code Time} object are set to the date
  29. * of the Epoch (January 1, 1970).
  30. * <p>
  31. * Any attempt to access the <i>Year</i>, <i>Month</i> or <i>Day</i>
  32. * elements of a {@code Time} object will result in an {@code
  33. * IllegalArgumentException}.
  34. * <p>
  35. * The result is undefined if any argument is out of bounds.
  36. *
  37. * @deprecated Use the constructor {@link #Time(long)}.
  38. * @param theHour
  39. * a value in the range {@code [0,23]}.
  40. * @param theMinute
  41. * a value in the range {@code [0,59]}.
  42. * @param theSecond
  43. * a value in the range {@code [0,59]}.
  44. */
  45. @SuppressWarnings("deprecation")
  46. @Deprecated
  47. public Time(int theHour, int theMinute, int theSecond) {
  48. super(70, 0, 1, theHour, theMinute, theSecond);
  49. }
  50. /**
  51. * Constructs a {@code Time} object using a supplied time specified in
  52. * milliseconds.
  53. *
  54. * @param theTime
  55. * a {@code Time} specified in milliseconds since the
  56. * <i>Epoch</i> (January 1st 1970, 00:00:00.000).
  57. */
  58. public Time(long theTime) {
  59. super(theTime);
  60. }
  61. /**
  62. * @deprecated This method is deprecated and must not be used. An SQL
  63. * {@code Time} object does not have a {@code Date} component.
  64. * @return does not return anything.
  65. * @throws IllegalArgumentException
  66. * if this method is called.
  67. */
  68. @SuppressWarnings("deprecation")
  69. @Deprecated
  70. @Override
  71. public int getDate() {
  72. throw new IllegalArgumentException();
  73. }
  74. /**
  75. * @deprecated This method is deprecated and must not be used. An SQL
  76. * {@code Time} object does not have a <i>Day</i> component.
  77. * @return does not return anything.
  78. * @throws IllegalArgumentException
  79. * if this method is called.
  80. */
  81. @SuppressWarnings("deprecation")
  82. @Deprecated
  83. @Override
  84. public int getDay() {
  85. throw new IllegalArgumentException();
  86. }
  87. /**
  88. * @deprecated This method is deprecated and must not be used. An SQL
  89. * {@code Time} object does not have a <i>Month</i> component.
  90. * @return does not return anything.
  91. * @throws IllegalArgumentException
  92. * if this method is called.
  93. */
  94. @SuppressWarnings("deprecation")
  95. @Deprecated
  96. @Override
  97. public int getMonth() {
  98. throw new IllegalArgumentException();
  99. }
  100. /**
  101. * @deprecated This method is deprecated and must not be used. An SQL
  102. * {@code Time} object does not have a <i>Year</i> component.
  103. * @return does not return anything.
  104. * @throws IllegalArgumentException
  105. * if this method is called.
  106. */
  107. @SuppressWarnings("deprecation")
  108. @Deprecated
  109. @Override
  110. public int getYear() {
  111. throw new IllegalArgumentException();
  112. }
  113. /**
  114. * @deprecated This method is deprecated and must not be used. An SQL
  115. * {@code Time} object does not have a {@code Date} component.
  116. * @throws IllegalArgumentException
  117. * if this method is called.
  118. */
  119. @SuppressWarnings("deprecation")
  120. @Deprecated
  121. @Override
  122. public void setDate(int i) {
  123. throw new IllegalArgumentException();
  124. }
  125. /**
  126. * @deprecated This method is deprecated and must not be used. An SQL
  127. * {@code Time} object does not have a <i>Month</i> component.
  128. * @throws IllegalArgumentException
  129. * if this method is called.
  130. */
  131. @SuppressWarnings("deprecation")
  132. @Deprecated
  133. @Override
  134. public void setMonth(int i) {
  135. throw new IllegalArgumentException();
  136. }
  137. /**
  138. * @deprecated This method is deprecated and must not be used. An SQL
  139. * {@code Time} object does not have a <i>Year</i> component.
  140. * @throws IllegalArgumentException
  141. * if this method is called.
  142. */
  143. @SuppressWarnings("deprecation")
  144. @Deprecated
  145. @Override
  146. public void setYear(int i) {
  147. throw new IllegalArgumentException();
  148. }
  149. /**
  150. * Sets the time for this {@code Time} object to the supplied milliseconds
  151. * value.
  152. *
  153. * @param time
  154. * A time value expressed as milliseconds since the <i>Epoch</i>.
  155. * Negative values are milliseconds before the Epoch. The Epoch
  156. * is January 1 1970, 00:00:00.000.
  157. */
  158. @Override
  159. public void setTime(long time) {
  160. super.setTime(time);
  161. }
  162. /**
  163. * Formats the {@code Time} as a String in JDBC escape format: {@code
  164. * hh:mm:ss}.
  165. *
  166. * @return A String representing the {@code Time} value in JDBC escape
  167. * format: {@code HH:mm:ss}
  168. */
  169. @Override
  170. public String toString() {
  171. StringBuilder sb = new StringBuilder(8);
  172. format(getHours(), 2, sb);
  173. sb.append(':');
  174. format(getMinutes(), 2, sb);
  175. sb.append(':');
  176. format(getSeconds(), 2, sb);
  177. return sb.toString();
  178. }
  179. private static final String PADDING = "00";
  180. /*
  181. * Private method to format the time
  182. */
  183. private void format(int date, int digits, StringBuilder sb) {
  184. String str = String.valueOf(date);
  185. if (digits - str.length() > 0) {
  186. sb.append(PADDING.substring(0, digits - str.length()));
  187. }
  188. sb.append(str);
  189. }
  190. /**
  191. * Creates a {@code Time} object from a string holding a time represented in
  192. * JDBC escape format: {@code hh:mm:ss}.
  193. * <p>
  194. * An exception occurs if the input string does not comply with this format.
  195. *
  196. * @param timeString
  197. * A String representing the time value in JDBC escape format:
  198. * {@code hh:mm:ss}.
  199. * @return The {@code Time} object set to a time corresponding to the given
  200. * time.
  201. * @throws IllegalArgumentException
  202. * if the supplied time string is not in JDBC escape format.
  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
  211. // found.
  212. // The string is separated into three parts by two separator characters,
  213. // if the first or the third part is null string, we should throw
  214. // IllegalArgumentException to follow RI
  215. if (secondIndex == -1 || firstIndex == 0
  216. || secondIndex + 1 == timeString.length()) {
  217. throw new IllegalArgumentException();
  218. }
  219. // parse each part of the string
  220. int hour = Integer.parseInt(timeString.substring(0, firstIndex));
  221. int minute = Integer.parseInt(timeString.substring(firstIndex + 1,
  222. secondIndex));
  223. int second = Integer.parseInt(timeString.substring(secondIndex + 1,
  224. timeString.length()));
  225. return new Time(hour, minute, second);
  226. }
  227. }