KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > sql > Date


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
8 package java.sql;
9
10 /**
11  * <P>A thin wrapper around a millisecond value that allows
12  * JDBC to identify this as an SQL <code>DATE</code> value. A
13  * milliseconds value represents the number of milliseconds that
14  * have passed since January 1, 1970 00:00:00.000 GMT.
15  * <p>
16  * To conform with the definition of SQL <code>DATE</code>, the
17  * millisecond values wrapped by a <code>java.sql.Date</code> instance
18  * 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 JavaDoc {
23
24     /**
25      * Constructs a <code>Date</code> object initialized with the given
26      * year, month, and day.
27      * <P>
28      * The result is undefined if a given argument is out of bounds.
29      *
30      * @param year the year minus 1900; must be 0 to 8099. (Note that
31      * 8099 is 9999 minus 1900.)
32      * @param month 0 to 11
33      * @param day 1 to 31
34      * @deprecated instead use the constructor <code>Date(long date)</code>
35      */

36     @Deprecated JavaDoc
37     public Date(int year, int month, int day) {
38     super(year, month, day);
39     }
40
41     /**
42      * Constructs a <code>Date</code> object using the given milliseconds
43      * time value. If the given milliseconds value contains time
44      * information, the driver will set the time components to the
45      * time in the default time zone (the time zone of the Java virtual
46      * machine running the application) that corresponds to zero GMT.
47      *
48      * @param date milliseconds since January 1, 1970, 00:00:00 GMT not
49      * to exceed the milliseconds representation for the year 8099.
50      * A negative number indicates the number of milliseconds
51      * before January 1, 1970, 00:00:00 GMT.
52      */

53     public Date(long date) {
54     // If the millisecond date value contains time info, mask it out.
55
super(date);
56     
57     }
58
59     /**
60      * Sets an existing <code>Date</code> object
61      * using the given milliseconds time value.
62      * If the given milliseconds value contains time information,
63      * the driver will set the time components to the
64      * time in the default time zone (the time zone of the Java virtual
65      * machine running the application) that corresponds to zero GMT.
66      *
67      * @param date milliseconds since January 1, 1970, 00:00:00 GMT not
68      * to exceed the milliseconds representation for the year 8099.
69      * A negative number indicates the number of milliseconds
70      * before January 1, 1970, 00:00:00 GMT.
71      */

72     public void setTime(long date) {
73     // If the millisecond date value contains time info, mask it out.
74
super.setTime(date);
75     }
76
77     /**
78      * Converts a string in JDBC date escape format to
79      * a <code>Date</code> value.
80      *
81      * @param s a <code>String</code> object representing a date in
82      * in the format "yyyy-mm-dd"
83      * @return a <code>java.sql.Date</code> object representing the
84      * given date
85      * @throws IllegalArgumentException if the date given is not in the
86      * JDBC date escape format (yyyy-mm-dd)
87      */

88     public static Date JavaDoc valueOf(String JavaDoc s) {
89     int year;
90     int month;
91     int day;
92     int firstDash;
93     int secondDash;
94
95     if (s == null) throw new java.lang.IllegalArgumentException JavaDoc();
96
97     firstDash = s.indexOf('-');
98     secondDash = s.indexOf('-', firstDash+1);
99     if ((firstDash > 0) & (secondDash > 0) & (secondDash < s.length()-1)) {
100         year = Integer.parseInt(s.substring(0, firstDash)) - 1900;
101         month = Integer.parseInt(s.substring(firstDash+1, secondDash)) - 1;
102         day = Integer.parseInt(s.substring(secondDash+1));
103     } else {
104         throw new java.lang.IllegalArgumentException JavaDoc();
105     }
106             
107     return new Date JavaDoc(year, month, day);
108     }
109
110     /**
111      * Formats a date in the date escape format yyyy-mm-dd.
112      * <P>
113      * NOTE: To specify a date format for the class
114      * <code>SimpleDateFormat</code>, use "yyyy.MM.dd" rather than
115      * "yyyy-mm-dd". In the context of <code>SimpleDateFormat</code>,
116      * "mm" indicates minutes rather than the month.
117      * For example:
118      * <PRE>
119      *
120      * Format Pattern Result
121      * -------------- -------
122      * "yyyy.MM.dd G 'at' hh:mm:ss z" ->> 1996.07.10 AD at 15:08:56 PDT
123      * </PRE>
124      * @return a String in yyyy-mm-dd format
125      */

126     public String JavaDoc toString () {
127     int year = super.getYear() + 1900;
128     int month = super.getMonth() + 1;
129     int day = super.getDate();
130
131         char buf[] = "2000-00-00".toCharArray();
132         buf[0] = Character.forDigit(year/1000,10);
133         buf[1] = Character.forDigit((year/100)%10,10);
134         buf[2] = Character.forDigit((year/10)%10,10);
135         buf[3] = Character.forDigit(year%10,10);
136         buf[5] = Character.forDigit(month/10,10);
137         buf[6] = Character.forDigit(month%10,10);
138         buf[8] = Character.forDigit(day/10,10);
139         buf[9] = Character.forDigit(day%10,10);
140         
141     return new String JavaDoc(buf);
142     }
143
144     // Override all the time operations inherited from java.util.Date;
145

146    /**
147     * This method is deprecated and should not be used because SQL Date
148     * values do not have a time component.
149     *
150     * @deprecated
151     * @exception java.lang.IllegalArgumentException if this method is invoked
152     * @see #setHours
153     */

154     @Deprecated JavaDoc
155     public int getHours() {
156     throw new java.lang.IllegalArgumentException JavaDoc();
157     }
158
159    /**
160     * This method is deprecated and should not be used because SQL Date
161     * values do not have a time component.
162     *
163     * @deprecated
164     * @exception java.lang.IllegalArgumentException if this method is invoked
165     * @see #setMinutes
166     */

167     @Deprecated JavaDoc
168     public int getMinutes() {
169     throw new java.lang.IllegalArgumentException JavaDoc();
170     }
171     
172    /**
173     * This method is deprecated and should not be used because SQL Date
174     * values do not have a time component.
175     *
176     * @deprecated
177     * @exception java.lang.IllegalArgumentException if this method is invoked
178     * @see #setSeconds
179     */

180     @Deprecated JavaDoc
181     public int getSeconds() {
182     throw new java.lang.IllegalArgumentException JavaDoc();
183     }
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 #getHours
192     */

193     @Deprecated JavaDoc
194     public void setHours(int i) {
195     throw new java.lang.IllegalArgumentException JavaDoc();
196     }
197
198    /**
199     * This method is deprecated and should not be used because SQL Date
200     * values do not have a time component.
201     *
202     * @deprecated
203     * @exception java.lang.IllegalArgumentException if this method is invoked
204     * @see #getMinutes
205     */

206     @Deprecated JavaDoc
207     public void setMinutes(int i) {
208     throw new java.lang.IllegalArgumentException JavaDoc();
209     }
210
211    /**
212     * This method is deprecated and should not be used because SQL Date
213     * values do not have a time component.
214     *
215     * @deprecated
216     * @exception java.lang.IllegalArgumentException if this method is invoked
217     * @see #getSeconds
218     */

219     @Deprecated JavaDoc
220     public void setSeconds(int i) {
221     throw new java.lang.IllegalArgumentException JavaDoc();
222     }
223
224    /**
225     * Private serial version unique ID to ensure serialization
226     * compatibility.
227     */

228     static final long serialVersionUID = 1511598038487230103L;
229 }
230
231
Popular Tags