39 |
package java.sql; |
package java.sql; |
40 |
|
|
41 |
import java.text.ParseException; |
import java.text.ParseException; |
42 |
|
import java.text.DecimalFormat; |
43 |
import java.text.SimpleDateFormat; |
import java.text.SimpleDateFormat; |
44 |
|
|
45 |
/** |
/** |
59 |
/** |
/** |
60 |
* Used for parsing and formatting this date. |
* Used for parsing and formatting this date. |
61 |
*/ |
*/ |
62 |
private static SimpleDateFormat sdf = |
private static SimpleDateFormat dateFormat = |
63 |
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
64 |
|
private static DecimalFormat decimalFormat = new DecimalFormat("000000000"); |
65 |
|
private static StringBuffer sbuf = new StringBuffer(29); |
66 |
|
|
67 |
/** |
/** |
68 |
* The nanosecond value for this object |
* The nanosecond value for this object |
99 |
|
|
100 |
try |
try |
101 |
{ |
{ |
102 |
java.util.Date d = (java.util.Date)sdf.parseObject(str); |
java.util.Date d = (java.util.Date) dateFormat.parseObject(str); |
103 |
|
|
104 |
if (d == null) |
if (d == null) |
105 |
throw new IllegalArgumentException(str); |
throw new IllegalArgumentException(str); |
136 |
|
|
137 |
/** |
/** |
138 |
* This method initializes a new instance of this class with the |
* This method initializes a new instance of this class with the |
139 |
* specified time value representing the number of seconds since |
* specified time value representing the number of milliseconds since |
140 |
* Jan 1, 1970 at 12:00 midnight GMT. |
* Jan 1, 1970 at 12:00 midnight GMT. |
141 |
* |
* |
142 |
* @param time The time value to intialize this <code>Time</code> to. |
* @param time The time value to intialize this <code>Time</code> to. |
143 |
*/ |
*/ |
144 |
public Timestamp(long date) |
public Timestamp(long date) |
145 |
{ |
{ |
146 |
super(date); |
super(date - (date % 1000)); |
147 |
|
nanos = (int) (date % 1000) * 1000000; |
148 |
|
} |
149 |
|
|
150 |
|
/** |
151 |
|
* Return the value of this Timestamp as the number of milliseconds |
152 |
|
* since Jan 1, 1970 at 12:00 midnight GMT. |
153 |
|
*/ |
154 |
|
public long getTime() |
155 |
|
{ |
156 |
|
return super.getTime() + (nanos / 1000000); |
157 |
} |
} |
158 |
|
|
159 |
/** |
/** |
163 |
*/ |
*/ |
164 |
public String toString() |
public String toString() |
165 |
{ |
{ |
166 |
return sdf.format(this) + "." + getNanos(); |
synchronized (dateFormat) |
167 |
|
{ |
168 |
|
sbuf.setLength(0); |
169 |
|
dateFormat.format(this, sbuf, null); |
170 |
|
sbuf.append('.'); |
171 |
|
decimalFormat.format(nanos, sbuf, null); |
172 |
|
int end = sbuf.length() - 1; |
173 |
|
while (end > 20 && sbuf.charAt(end) == '0') |
174 |
|
end--; |
175 |
|
return sbuf.substring(0, end + 1); |
176 |
|
} |
177 |
} |
} |
178 |
|
|
179 |
/** |
/** |
205 |
*/ |
*/ |
206 |
public boolean before(Timestamp ts) |
public boolean before(Timestamp ts) |
207 |
{ |
{ |
208 |
if (ts.getTime() > getTime()) |
long time1 = getTime(); |
209 |
|
long time2 = ts.getTime(); |
210 |
|
if (time1 < time2 || (time1 == time2 && getNanos() < ts.getNanos())) |
211 |
return true; |
return true; |
|
|
|
|
if (ts.getNanos() > getNanos()) |
|
|
return true; |
|
|
|
|
212 |
return false; |
return false; |
213 |
} |
} |
214 |
|
|
223 |
*/ |
*/ |
224 |
public boolean after(Timestamp ts) |
public boolean after(Timestamp ts) |
225 |
{ |
{ |
226 |
if (ts.getTime() < getTime()) |
long time1 = getTime(); |
227 |
|
long time2 = ts.getTime(); |
228 |
|
if (time1 > time2 || (time1 == time2 && getNanos() > ts.getNanos())) |
229 |
return true; |
return true; |
|
|
|
|
if (ts.getNanos() < getNanos()) |
|
|
return true; |
|
|
|
|
230 |
return false; |
return false; |
231 |
} |
} |
232 |
|
|