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