1. /*
  2. * @(#)Date.java 1.19 01/11/29
  3. *
  4. * Copyright 2002 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 a SQL DATE. A milliseconds value represents
  11. * the number of milliseconds that have passed since January 1, 1970
  12. * 00:00:00.000 GMT.
  13. * <p>
  14. * To conform with the definition of SQL DATE, the millisecond values
  15. * wrapped by a java.sql.Date instance must be 'normalized' by setting the
  16. * hours, minutes, seconds, and milliseconds to zero in the particular
  17. * time zone with which the instance is associated.
  18. */
  19. public class Date extends java.util.Date {
  20. /**
  21. * Constructs a <code>Date</code> object initialized with the given
  22. * year, month, and day.
  23. *
  24. * @param year year-1900
  25. * @param month 0 to 11
  26. * @param day 1 to 31
  27. * @deprecated instead use the constructor <code>Date(long date)</code>
  28. */
  29. public Date(int year, int month, int day) {
  30. super(year, month, day);
  31. }
  32. /**
  33. * Constructs a <code>Date</code> object
  34. * using a milliseconds time value. If the given millisecond
  35. * value contains time information, the driver will set the
  36. * time components to zero.
  37. *
  38. * @param date milliseconds since January 1, 1970, 00:00:00 GMT.
  39. A negative number indicates the number of milliseconds
  40. before January 1, 1970, 00:00:00 GMT.
  41. */
  42. public Date(long date) {
  43. // If the millisecond date value contains time info, mask it out.
  44. super(date);
  45. }
  46. /**
  47. * Sets an existing <code>Date</code> object
  48. * using the given milliseconds time value. If the given milliseconds
  49. * value contains time information, the driver will set the
  50. * time components to zero.
  51. *
  52. * @param date milliseconds since January 1, 1970, 00:00:00 GMT.
  53. A negative number indicates the number of milliseconds
  54. before January 1, 1970, 00:00:00 GMT.
  55. */
  56. public void setTime(long date) {
  57. // If the millisecond date value contains time info, mask it out.
  58. super.setTime(date);
  59. }
  60. /**
  61. * Converts a string in JDBC date escape format to
  62. * a <code>Date</code> value.
  63. *
  64. * @param s date in format "yyyy-mm-dd"
  65. * @return a <code>Date</code> object representing the given date
  66. */
  67. public static Date valueOf(String s) {
  68. int year;
  69. int month;
  70. int day;
  71. int firstDash;
  72. int secondDash;
  73. if (s == null) throw new java.lang.IllegalArgumentException();
  74. firstDash = s.indexOf('-');
  75. secondDash = s.indexOf('-', firstDash+1);
  76. if ((firstDash > 0) & (secondDash > 0) & (secondDash < s.length()-1)) {
  77. year = Integer.parseInt(s.substring(0, firstDash)) - 1900;
  78. month = Integer.parseInt(s.substring(firstDash+1, secondDash)) - 1;
  79. day = Integer.parseInt(s.substring(secondDash+1));
  80. } else {
  81. throw new java.lang.IllegalArgumentException();
  82. }
  83. return new Date(year, month, day);
  84. }
  85. /**
  86. * Formats a date in JDBC date escape format.
  87. *
  88. * @return a String in yyyy-mm-dd format
  89. */
  90. public String toString () {
  91. int year = super.getYear() + 1900;
  92. int month = super.getMonth() + 1;
  93. int day = super.getDate();
  94. String yearString;
  95. String monthString;
  96. String dayString;
  97. yearString = Integer.toString(year);
  98. if (month < 10) {
  99. monthString = "0" + month;
  100. } else {
  101. monthString = Integer.toString(month);
  102. }
  103. if (day < 10) {
  104. dayString = "0" + day;
  105. } else {
  106. dayString = Integer.toString(day);
  107. }
  108. return ( yearString + "-" + monthString + "-" + dayString);
  109. }
  110. // Override all the time operations inherited from java.util.Date;
  111. /**
  112. * This method is deprecated and should not be used because SQL Date
  113. * values do not have a time component.
  114. *
  115. * @deprecated
  116. * @exception java.lang.IllegalArgumentException if this method is invoked
  117. */
  118. public int getHours() {
  119. throw new java.lang.IllegalArgumentException();
  120. }
  121. /**
  122. * This method is deprecated and should not be used because SQL Date
  123. * values do not have a time component.
  124. *
  125. * @deprecated
  126. * @exception java.lang.IllegalArgumentException if this method is invoked
  127. */
  128. public int getMinutes() {
  129. throw new java.lang.IllegalArgumentException();
  130. }
  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. */
  138. public int getSeconds() {
  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. */
  148. public void setHours(int i) {
  149. throw new java.lang.IllegalArgumentException();
  150. }
  151. /**
  152. * This method is deprecated and should not be used because SQL Date
  153. * values do not have a time component.
  154. *
  155. * @deprecated
  156. * @exception java.lang.IllegalArgumentException if this method is invoked
  157. */
  158. public void setMinutes(int i) {
  159. throw new java.lang.IllegalArgumentException();
  160. }
  161. /**
  162. * This method is deprecated and should not be used because SQL Date
  163. * values do not have a time component.
  164. *
  165. * @deprecated
  166. * @exception java.lang.IllegalArgumentException if this method is invoked
  167. */
  168. public void setSeconds(int i) {
  169. throw new java.lang.IllegalArgumentException();
  170. }
  171. }