Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / java / sql / Timestamp.java
blob501810054f29e0a38a68689cc7a9e49fa7e08fc7
1 /* Time.java -- Wrapper around java.util.Date
2 Copyright (C) 1999, 2000, 2003, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.sql;
41 import java.text.DecimalFormat;
42 import java.text.ParseException;
43 import java.text.SimpleDateFormat;
45 /**
46 * This class is a wrapper around java.util.Date to allow the JDBC
47 * driver to identify the value as a SQL Timestamp. Note that this
48 * class also adds an additional field for nano-seconds, and so
49 * is not completely identical to <code>java.util.Date</code> as
50 * the <code>java.sql.Date</code> and <code>java.sql.Time</code>
51 * classes are.
53 * @author Aaron M. Renn (arenn@urbanophile.com)
55 public class Timestamp extends java.util.Date
57 static final long serialVersionUID = 2745179027874758501L;
59 /**
60 * Used for parsing and formatting this date.
62 private static SimpleDateFormat dateFormat =
63 new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
64 private static DecimalFormat decimalFormat = new DecimalFormat("000000000");
65 private static StringBuffer sbuf = new StringBuffer(29);
67 /**
68 * The nanosecond value for this object
70 private int nanos;
72 /**
73 * This method returns a new instance of this class by parsing a
74 * date in JDBC format into a Java date.
76 * @param str The string to parse.
77 * @return The resulting <code>java.sql.Timestamp</code> value.
79 public static Timestamp valueOf(String str)
81 int nanos = 0;
82 int dot = str.indexOf('.');
83 if (dot != -1)
85 if (str.lastIndexOf('.') != dot)
86 throw new IllegalArgumentException(str);
88 int len = str.length() - dot - 1;
89 if (len < 1 || len > 9)
90 throw new IllegalArgumentException(str);
92 nanos = Integer.parseInt(str.substring(dot + 1));
93 for (int i = len; i < 9; i++)
94 nanos *= 10;
96 str = str.substring(0, dot);
102 java.util.Date d;
103 synchronized (dateFormat)
105 d = (java.util.Date) dateFormat.parseObject(str);
108 if (d == null)
109 throw new IllegalArgumentException(str);
111 Timestamp ts = new Timestamp(d.getTime() + nanos / 1000000);
112 ts.nanos = nanos;
113 return ts;
115 catch (ParseException e)
117 throw new IllegalArgumentException(str);
122 * This method initializes a new instance of this class with the
123 * specified year, month, and day.
125 * @param year The year for this Timestamp (year - 1900)
126 * @param month The month for this Timestamp (0-11)
127 * @param day The day for this Timestamp (1-31)
128 * @param hour The hour for this Timestamp (0-23)
129 * @param minute The minute for this Timestamp (0-59)
130 * @param second The second for this Timestamp (0-59)
131 * @param nanos The nanosecond value for this Timestamp (0 to 999,999,9999)
132 * @deprecated
134 public Timestamp(int year, int month, int day, int hour, int minute,
135 int second, int nanos)
137 super(year, month, day, hour, minute, second);
138 this.nanos = nanos;
142 * This method initializes a new instance of this class with the
143 * specified time value representing the number of milliseconds since
144 * Jan 1, 1970 at 12:00 midnight GMT.
146 * @param time The time value to intialize this <code>Time</code> to.
148 public Timestamp(long date)
150 super(date - (date % 1000));
151 nanos = (int) (date % 1000) * 1000000;
155 * Return the value of this Timestamp as the number of milliseconds
156 * since Jan 1, 1970 at 12:00 midnight GMT.
158 public long getTime()
160 return super.getTime() + (nanos / 1000000);
164 * This method returns this date in JDBC format.
166 * @return This date as a string.
168 public String toString()
170 synchronized (dateFormat)
172 sbuf.setLength(0);
173 dateFormat.format(this, sbuf, null);
174 sbuf.append('.');
175 decimalFormat.format(nanos, sbuf, null);
176 int end = sbuf.length() - 1;
177 while (end > 20 && sbuf.charAt(end) == '0')
178 end--;
179 return sbuf.substring(0, end + 1);
184 * This method returns the nanosecond value for this object.
185 * @return The nanosecond value for this object.
187 public int getNanos()
189 return nanos;
193 * This method sets the nanosecond value for this object.
195 * @param nanos The nanosecond value for this object.
197 public void setNanos(int nanos)
199 this.nanos = nanos;
203 * This methods tests whether this object is earlier than the specified
204 * object.
206 * @param ts The other <code>Timestamp</code> to test against.
207 * @return <code>true</code> if this object is earlier than the other object,
208 * <code>false</code> otherwise.
210 public boolean before(Timestamp ts)
212 long time1 = getTime();
213 long time2 = ts.getTime();
214 if (time1 < time2 || (time1 == time2 && getNanos() < ts.getNanos()))
215 return true;
216 return false;
220 * This methods tests whether this object is later than the specified
221 * object.
223 * @param ts The other <code>Timestamp</code> to test against.
225 * @return <code>true</code> if this object is later than the other object,
226 * <code>false</code> otherwise.
228 public boolean after(Timestamp ts)
230 long time1 = getTime();
231 long time2 = ts.getTime();
232 if (time1 > time2 || (time1 == time2 && getNanos() > ts.getNanos()))
233 return true;
234 return false;
238 * This method these the specified <code>Object</code> for equality
239 * against this object. This will be true if an only if the specified
240 * object is an instance of <code>Timestamp</code> and has the same
241 * time value fields.
243 * @param obj The object to test against for equality.
245 * @return <code>true</code> if the specified object is equal to this
246 * object, <code>false</code> otherwise.
248 public boolean equals(Object obj)
250 if (!(obj instanceof Timestamp))
251 return false;
253 return equals((Timestamp) obj);
257 * This method tests the specified timestamp for equality against this
258 * object. This will be true if and only if the specified object is
259 * not <code>null</code> and contains all the same time value fields
260 * as this object.
262 * @param ts The <code>Timestamp</code> to test against for equality.
264 * @return <code>true</code> if the specified object is equal to this
265 * object, <code>false</code> otherwise.
267 public boolean equals(Timestamp ts)
269 if (ts == null)
270 return false;
272 if (ts.getTime() != getTime())
273 return false;
275 if (ts.getNanos() != getNanos())
276 return false;
278 return true;
282 * Compare two Timestamp
283 * @param when the other Timestamp.
284 * @return 0, if the date represented
285 * by obj is exactly the same as the time represented by this
286 * object, a negative if this Timestamp is before the other Timestamp, and
287 * a positive value otherwise.
288 * @since 1.2
290 public int compareTo(Timestamp ts)
292 int s = super.compareTo((java.util.Date) ts);
293 if (s != 0)
294 return s;
295 // If Date components were equal, then we check the nanoseconds.
296 return nanos - ts.nanos;
300 * Compares this Timestamp to another. This behaves like
301 * <code>compareTo(Timestamp)</code>, but it may throw a
302 * <code>ClassCastException</code>
303 * @param obj the other Timestamp.
304 * @return 0, if the Timestamp represented
305 * by obj is exactly the same as the time represented by this
306 * object, a negative if this Timestamp is before the other Timestamp, and
307 * a positive value otherwise.
308 * @exception ClassCastException if obj is not of type Timestamp.
309 * @since 1.2
311 public int compareTo(Object obj)
313 return compareTo((Timestamp) obj);