1. /*
  2. * @(#)Date.java 1.22 00/02/02
  3. *
  4. * Copyright 1996-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.sql;
  11. /**
  12. * <P>A thin wrapper around a millisecond value that allows
  13. * JDBC to identify this as a SQL DATE. A milliseconds value represents
  14. * the number of milliseconds that have passed since January 1, 1970
  15. * 00:00:00.000 GMT.
  16. * <p>
  17. * To conform with the definition of SQL DATE, the millisecond values
  18. * wrapped by a java.sql.Date instance must be 'normalized' by setting the
  19. * hours, minutes, seconds, and milliseconds to zero in the particular
  20. * time zone with which the instance is associated.
  21. */
  22. public class Date extends java.util.Date {
  23. /**
  24. * Constructs a <code>Date</code> object initialized with the given
  25. * year, month, and day.
  26. *
  27. * @param year year-1900
  28. * @param month 0 to 11
  29. * @param day 1 to 31
  30. * @deprecated instead use the constructor <code>Date(long date)</code>
  31. */
  32. public Date(int year, int month, int day) {
  33. super(year, month, day);
  34. }
  35. /**
  36. * Constructs a <code>Date</code> object
  37. * using a milliseconds time value. If the given millisecond
  38. * value contains time information, the driver will set the
  39. * time components to zero.
  40. *
  41. * @param date milliseconds since January 1, 1970, 00:00:00 GMT.
  42. A negative number indicates the number of milliseconds
  43. before January 1, 1970, 00:00:00 GMT.
  44. */
  45. public Date(long date) {
  46. // If the millisecond date value contains time info, mask it out.
  47. super(date);
  48. }
  49. /**
  50. * Sets an existing <code>Date</code> object
  51. * using the given milliseconds time value. If the given milliseconds
  52. * value contains time information, the driver will set the
  53. * time components to zero.
  54. *
  55. * @param date milliseconds since January 1, 1970, 00:00:00 GMT.
  56. A negative number indicates the number of milliseconds
  57. before January 1, 1970, 00:00:00 GMT.
  58. */
  59. public void setTime(long date) {
  60. // If the millisecond date value contains time info, mask it out.
  61. super.setTime(date);
  62. }
  63. /**
  64. * Converts a string in JDBC date escape format to
  65. * a <code>Date</code> value.
  66. *
  67. * @param s date in format "yyyy-mm-dd"
  68. * @return a <code>Date</code> object representing the given date
  69. */
  70. public static Date valueOf(String s) {
  71. int year;
  72. int month;
  73. int day;
  74. int firstDash;
  75. int secondDash;
  76. if (s == null) throw new java.lang.IllegalArgumentException();
  77. firstDash = s.indexOf('-');
  78. secondDash = s.indexOf('-', firstDash+1);
  79. if ((firstDash > 0) & (secondDash > 0) & (secondDash < s.length()-1)) {
  80. year = Integer.parseInt(s.substring(0, firstDash)) - 1900;
  81. month = Integer.parseInt(s.substring(firstDash+1, secondDash)) - 1;
  82. day = Integer.parseInt(s.substring(secondDash+1));
  83. } else {
  84. throw new java.lang.IllegalArgumentException();
  85. }
  86. return new Date(year, month, day);
  87. }
  88. /**
  89. * Formats a date in the date escape format yyyy-mm-dd.
  90. * <P>
  91. * NOTE: To specify a date format for the class
  92. * <code>SimpleDateFormat</code>, use "yyyy.MM.dd" rather than
  93. * "yyyy-mm-dd". In the context of <code>SimpleDateFormat</code>,
  94. * "mm" indicates minutes rather than the month.
  95. * For example:
  96. * <PRE>
  97. *
  98. * Format Pattern Result
  99. * -------------- -------
  100. * "yyyy.MM.dd G 'at' hh:mm:ss z" ->> 1996.07.10 AD at 15:08:56 PDT
  101. * </PRE>
  102. * @return a String in yyyy-mm-dd format
  103. */
  104. public String toString () {
  105. int year = super.getYear() + 1900;
  106. int month = super.getMonth() + 1;
  107. int day = super.getDate();
  108. String yearString;
  109. String monthString;
  110. String dayString;
  111. yearString = Integer.toString(year);
  112. if (month < 10) {
  113. monthString = "0" + month;
  114. } else {
  115. monthString = Integer.toString(month);
  116. }
  117. if (day < 10) {
  118. dayString = "0" + day;
  119. } else {
  120. dayString = Integer.toString(day);
  121. }
  122. return ( yearString + "-" + monthString + "-" + dayString);
  123. }
  124. // Override all the time operations inherited from java.util.Date;
  125. /**
  126. * This method is deprecated and should not be used because SQL Date
  127. * values do not have a time component.
  128. *
  129. * @deprecated
  130. * @exception java.lang.IllegalArgumentException if this method is invoked
  131. */
  132. public int getHours() {
  133. throw new java.lang.IllegalArgumentException();
  134. }
  135. /**
  136. * This method is deprecated and should not be used because SQL Date
  137. * values do not have a time component.
  138. *
  139. * @deprecated
  140. * @exception java.lang.IllegalArgumentException if this method is invoked
  141. */
  142. public int getMinutes() {
  143. throw new java.lang.IllegalArgumentException();
  144. }
  145. /**
  146. * This method is deprecated and should not be used because SQL Date
  147. * values do not have a time component.
  148. *
  149. * @deprecated
  150. * @exception java.lang.IllegalArgumentException if this method is invoked
  151. */
  152. public int getSeconds() {
  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. */
  162. public void setHours(int i) {
  163. throw new java.lang.IllegalArgumentException();
  164. }
  165. /**
  166. * This method is deprecated and should not be used because SQL Date
  167. * values do not have a time component.
  168. *
  169. * @deprecated
  170. * @exception java.lang.IllegalArgumentException if this method is invoked
  171. */
  172. public void setMinutes(int i) {
  173. throw new java.lang.IllegalArgumentException();
  174. }
  175. /**
  176. * This method is deprecated and should not be used because SQL Date
  177. * values do not have a time component.
  178. *
  179. * @deprecated
  180. * @exception java.lang.IllegalArgumentException if this method is invoked
  181. */
  182. public void setSeconds(int i) {
  183. throw new java.lang.IllegalArgumentException();
  184. }
  185. }