1. /*
  2. * @(#)Date.java 1.33 04/05/18
  3. *
  4. * Copyright 2004 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 a millisecond value that allows
  10. * JDBC to identify this as an SQL <code>DATE</code> value. A
  11. * milliseconds value represents the number of milliseconds that
  12. * have passed since January 1, 1970 00:00:00.000 GMT.
  13. * <p>
  14. * To conform with the definition of SQL <code>DATE</code>, the
  15. * millisecond values wrapped by a <code>java.sql.Date</code> instance
  16. * must be 'normalized' by setting the
  17. * hours, minutes, seconds, and milliseconds to zero in the particular
  18. * time zone with which the instance is associated.
  19. */
  20. public class Date extends java.util.Date {
  21. /**
  22. * Constructs a <code>Date</code> object initialized with the given
  23. * year, month, and day.
  24. * <P>
  25. * The result is undefined if a given argument is out of bounds.
  26. *
  27. * @param year the year minus 1900; must be 0 to 8099. (Note that
  28. * 8099 is 9999 minus 1900.)
  29. * @param month 0 to 11
  30. * @param day 1 to 31
  31. * @deprecated instead use the constructor <code>Date(long date)</code>
  32. */
  33. @Deprecated
  34. public Date(int year, int month, int day) {
  35. super(year, month, day);
  36. }
  37. /**
  38. * Constructs a <code>Date</code> object using the given milliseconds
  39. * time value. If the given milliseconds value contains time
  40. * information, the driver will set the time components to the
  41. * time in the default time zone (the time zone of the Java virtual
  42. * machine running the application) that corresponds to zero GMT.
  43. *
  44. * @param date milliseconds since January 1, 1970, 00:00:00 GMT not
  45. * to exceed the milliseconds representation for the year 8099.
  46. * A negative number indicates the number of milliseconds
  47. * before January 1, 1970, 00:00:00 GMT.
  48. */
  49. public Date(long date) {
  50. // If the millisecond date value contains time info, mask it out.
  51. super(date);
  52. }
  53. /**
  54. * Sets an existing <code>Date</code> object
  55. * using the given milliseconds time value.
  56. * If the given milliseconds value contains time information,
  57. * the driver will set the time components to the
  58. * time in the default time zone (the time zone of the Java virtual
  59. * machine running the application) that corresponds to zero GMT.
  60. *
  61. * @param date milliseconds since January 1, 1970, 00:00:00 GMT not
  62. * to exceed the milliseconds representation for the year 8099.
  63. * A negative number indicates the number of milliseconds
  64. * before January 1, 1970, 00:00:00 GMT.
  65. */
  66. public void setTime(long date) {
  67. // If the millisecond date value contains time info, mask it out.
  68. super.setTime(date);
  69. }
  70. /**
  71. * Converts a string in JDBC date escape format to
  72. * a <code>Date</code> value.
  73. *
  74. * @param s a <code>String</code> object representing a date in
  75. * in the format "yyyy-mm-dd"
  76. * @return a <code>java.sql.Date</code> object representing the
  77. * given date
  78. * @throws IllegalArgumentException if the date given is not in the
  79. * JDBC date escape format (yyyy-mm-dd)
  80. */
  81. public static Date valueOf(String s) {
  82. int year;
  83. int month;
  84. int day;
  85. int firstDash;
  86. int secondDash;
  87. if (s == null) throw new java.lang.IllegalArgumentException();
  88. firstDash = s.indexOf('-');
  89. secondDash = s.indexOf('-', firstDash+1);
  90. if ((firstDash > 0) & (secondDash > 0) & (secondDash < s.length()-1)) {
  91. year = Integer.parseInt(s.substring(0, firstDash)) - 1900;
  92. month = Integer.parseInt(s.substring(firstDash+1, secondDash)) - 1;
  93. day = Integer.parseInt(s.substring(secondDash+1));
  94. } else {
  95. throw new java.lang.IllegalArgumentException();
  96. }
  97. return new Date(year, month, day);
  98. }
  99. /**
  100. * Formats a date in the date escape format yyyy-mm-dd.
  101. * <P>
  102. * NOTE: To specify a date format for the class
  103. * <code>SimpleDateFormat</code>, use "yyyy.MM.dd" rather than
  104. * "yyyy-mm-dd". In the context of <code>SimpleDateFormat</code>,
  105. * "mm" indicates minutes rather than the month.
  106. * For example:
  107. * <PRE>
  108. *
  109. * Format Pattern Result
  110. * -------------- -------
  111. * "yyyy.MM.dd G 'at' hh:mm:ss z" ->> 1996.07.10 AD at 15:08:56 PDT
  112. * </PRE>
  113. * @return a String in yyyy-mm-dd format
  114. */
  115. public String toString () {
  116. int year = super.getYear() + 1900;
  117. int month = super.getMonth() + 1;
  118. int day = super.getDate();
  119. char buf[] = "2000-00-00".toCharArray();
  120. buf[0] = Character.forDigit(year1000,10);
  121. buf[1] = Character.forDigit((year100)%10,10);
  122. buf[2] = Character.forDigit((year10)%10,10);
  123. buf[3] = Character.forDigit(year%10,10);
  124. buf[5] = Character.forDigit(month10,10);
  125. buf[6] = Character.forDigit(month%10,10);
  126. buf[8] = Character.forDigit(day10,10);
  127. buf[9] = Character.forDigit(day%10,10);
  128. return new String(buf);
  129. }
  130. // Override all the time operations inherited from java.util.Date;
  131. /**
  132. * This method is deprecated and should not be used because SQL Date
  133. * values do not have a time component.
  134. *
  135. * @deprecated
  136. * @exception java.lang.IllegalArgumentException if this method is invoked
  137. * @see #setHours
  138. */
  139. @Deprecated
  140. public int getHours() {
  141. throw new java.lang.IllegalArgumentException();
  142. }
  143. /**
  144. * This method is deprecated and should not be used because SQL Date
  145. * values do not have a time component.
  146. *
  147. * @deprecated
  148. * @exception java.lang.IllegalArgumentException if this method is invoked
  149. * @see #setMinutes
  150. */
  151. @Deprecated
  152. public int getMinutes() {
  153. throw new java.lang.IllegalArgumentException();
  154. }
  155. /**
  156. * This method is deprecated and should not be used because SQL Date
  157. * values do not have a time component.
  158. *
  159. * @deprecated
  160. * @exception java.lang.IllegalArgumentException if this method is invoked
  161. * @see #setSeconds
  162. */
  163. @Deprecated
  164. public int getSeconds() {
  165. throw new java.lang.IllegalArgumentException();
  166. }
  167. /**
  168. * This method is deprecated and should not be used because SQL Date
  169. * values do not have a time component.
  170. *
  171. * @deprecated
  172. * @exception java.lang.IllegalArgumentException if this method is invoked
  173. * @see #getHours
  174. */
  175. @Deprecated
  176. public void setHours(int i) {
  177. throw new java.lang.IllegalArgumentException();
  178. }
  179. /**
  180. * This method is deprecated and should not be used because SQL Date
  181. * values do not have a time component.
  182. *
  183. * @deprecated
  184. * @exception java.lang.IllegalArgumentException if this method is invoked
  185. * @see #getMinutes
  186. */
  187. @Deprecated
  188. public void setMinutes(int i) {
  189. throw new java.lang.IllegalArgumentException();
  190. }
  191. /**
  192. * This method is deprecated and should not be used because SQL Date
  193. * values do not have a time component.
  194. *
  195. * @deprecated
  196. * @exception java.lang.IllegalArgumentException if this method is invoked
  197. * @see #getSeconds
  198. */
  199. @Deprecated
  200. public void setSeconds(int i) {
  201. throw new java.lang.IllegalArgumentException();
  202. }
  203. /**
  204. * Private serial version unique ID to ensure serialization
  205. * compatibility.
  206. */
  207. static final long serialVersionUID = 1511598038487230103L;
  208. }