2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / sql / Timestamp.java
blob22108f58f93c313bbbf92f8e42be657578183dad
1 /* Time.java -- Wrapper around java.util.Date
2 Copyright (C) 1999, 2000, 2003 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.ParseException;
42 import java.text.SimpleDateFormat;
44 /**
45 * This class is a wrapper around java.util.Date to allow the JDBC
46 * driver to identify the value as a SQL Timestamp. Note that this
47 * class also adds an additional field for nano-seconds, and so
48 * is not completely identical to <code>java.util.Date</code> as
49 * the <code>java.sql.Date</code> and <code>java.sql.Time</code>
50 * classes are.
52 * @author Aaron M. Renn (arenn@urbanophile.com)
54 public class Timestamp extends java.util.Date
56 static final long serialVersionUID = 2745179027874758501L;
58 /**
59 * Used for parsing and formatting this date.
61 private static SimpleDateFormat sdf =
62 new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
64 /**
65 * The nanosecond value for this object
67 private int nanos;
69 /**
70 * This method returns a new instance of this class by parsing a
71 * date in JDBC format into a Java date.
73 * @param str The string to parse.
74 * @return The resulting <code>java.sql.Timestamp</code> value.
76 public static Timestamp valueOf(String str)
78 int nanos = 0;
79 int dot = str.indexOf('.');
80 if (dot != -1)
82 if (str.lastIndexOf('.') != dot)
83 throw new IllegalArgumentException(str);
85 int len = str.length() - dot - 1;
86 if (len < 1 || len > 9)
87 throw new IllegalArgumentException(str);
89 nanos = Integer.parseInt(str.substring(dot + 1));
90 for (int i = len; i < 9; i++)
91 nanos *= 10;
93 str = str.substring(0, dot);
97 try
99 java.util.Date d = (java.util.Date)sdf.parseObject(str);
101 if (d == null)
102 throw new IllegalArgumentException(str);
104 Timestamp ts = new Timestamp(d.getTime() + nanos / 1000000);
105 ts.nanos = nanos;
106 return ts;
108 catch (ParseException e)
110 throw new IllegalArgumentException(str);
115 * This method initializes a new instance of this class with the
116 * specified year, month, and day.
118 * @param year The year for this Timestamp (year - 1900)
119 * @param month The month for this Timestamp (0-11)
120 * @param day The day for this Timestamp (1-31)
121 * @param hour The hour for this Timestamp (0-23)
122 * @param minute The minute for this Timestamp (0-59)
123 * @param second The second for this Timestamp (0-59)
124 * @param nanos The nanosecond value for this Timestamp (0 to 999,999,9999)
125 * @deprecated
127 public Timestamp(int year, int month, int day, int hour, int minute,
128 int second, int nanos)
130 super(year, month, day, hour, minute, second);
131 this.nanos = nanos;
135 * This method initializes a new instance of this class with the
136 * specified time value representing the number of seconds since
137 * Jan 1, 1970 at 12:00 midnight GMT.
139 * @param time The time value to intialize this <code>Time</code> to.
141 public Timestamp(long date)
143 super(date);
147 * This method returns this date in JDBC format.
149 * @return This date as a string.
151 public String toString()
153 return sdf.format(this) + "." + getNanos();
157 * This method returns the nanosecond value for this object.
158 * @return The nanosecond value for this object.
160 public int getNanos()
162 return nanos;
166 * This method sets the nanosecond value for this object.
168 * @param nanos The nanosecond value for this object.
170 public void setNanos(int nanos)
172 this.nanos = nanos;
176 * This methods tests whether this object is earlier than the specified
177 * object.
179 * @param ts The other <code>Timestamp</code> to test against.
180 * @return <code>true</code> if this object is earlier than the other object,
181 * <code>false</code> otherwise.
183 public boolean before(Timestamp ts)
185 if (ts.getTime() > getTime())
186 return true;
188 if (ts.getNanos() > getNanos())
189 return true;
191 return false;
195 * This methods tests whether this object is later than the specified
196 * object.
198 * @param ts The other <code>Timestamp</code> to test against.
200 * @return <code>true</code> if this object is later than the other object,
201 * <code>false</code> otherwise.
203 public boolean after(Timestamp ts)
205 if (ts.getTime() < getTime())
206 return true;
208 if (ts.getNanos() < getNanos())
209 return true;
211 return false;
215 * This method these the specified <code>Object</code> for equality
216 * against this object. This will be true if an only if the specified
217 * object is an instance of <code>Timestamp</code> and has the same
218 * time value fields.
220 * @param obj The object to test against for equality.
222 * @return <code>true</code> if the specified object is equal to this
223 * object, <code>false</code> otherwise.
225 public boolean equals(Object obj)
227 if (!(obj instanceof Timestamp))
228 return false;
230 return equals((Timestamp) obj);
234 * This method tests the specified timestamp for equality against this
235 * object. This will be true if and only if the specified object is
236 * not <code>null</code> and contains all the same time value fields
237 * as this object.
239 * @param ts The <code>Timestamp</code> to test against for equality.
241 * @return <code>true</code> if the specified object is equal to this
242 * object, <code>false</code> otherwise.
244 public boolean equals(Timestamp ts)
246 if (ts == null)
247 return false;
249 if (ts.getTime() != getTime())
250 return false;
252 if (ts.getNanos() != getNanos())
253 return false;
255 return true;
259 * Compare two Timestamp
260 * @param when the other Timestamp.
261 * @return 0, if the date represented
262 * by obj is exactly the same as the time represented by this
263 * object, a negative if this Timestamp is before the other Timestamp, and
264 * a positive value otherwise.
265 * @since 1.2
267 public int compareTo(Timestamp ts)
269 int s = super.compareTo((java.util.Date) ts);
270 if (s != 0)
271 return s;
272 // If Date components were equal, then we check the nanoseconds.
273 return nanos - ts.nanos;
277 * Compares this Timestamp to another. This behaves like
278 * <code>compareTo(Timestamp)</code>, but it may throw a
279 * <code>ClassCastException</code>
280 * @param obj the other Timestamp.
281 * @return 0, if the Timestamp represented
282 * by obj is exactly the same as the time represented by this
283 * object, a negative if this Timestamp is before the other Timestamp, and
284 * a positive value otherwise.
285 * @exception ClassCastException if obj is not of type Timestamp.
286 * @since 1.2
288 public int compareTo(Object obj)
290 return compareTo((Timestamp) obj);