diff options
author | Bryce McKinlay <mckinlay@redhat.com> | 2004-07-15 22:14:45 +0000 |
---|---|---|
committer | Bryce McKinlay <bryce@gcc.gnu.org> | 2004-07-15 23:14:45 +0100 |
commit | cbd7a79bc312c93c7081209c2e65024d016f662e (patch) | |
tree | e8016dfc420b4b3ca5e5ace062e88d794c5d3c8a /libjava/java/sql/Timestamp.java | |
parent | e4f78bd4af1ac824139f05da29ef6c6c4366aa0b (diff) | |
download | gcc-cbd7a79bc312c93c7081209c2e65024d016f662e.zip gcc-cbd7a79bc312c93c7081209c2e65024d016f662e.tar.gz gcc-cbd7a79bc312c93c7081209c2e65024d016f662e.tar.bz2 |
re PR libgcj/16574 (java.sql.Timestamp#toString() does not deal with fractional seconds correctly)
2004-07-15 Bryce McKinlay <mckinlay@redhat.com>
PR libgcj/16574
* java/sql/Timestamp.java (dateFormat): Renamed from sdf.
(decimalFormat): New static variable.
(sbuf): Likewise.
(getTime): New. Override Date.getTime().
(toString): Synchronize. Use decimalFormat to format nanos value
correctly. Truncate extra zeros.
(before): Compare getNanos() only if getTime() is equal.
(after): Likewise.
From-SVN: r84784
Diffstat (limited to 'libjava/java/sql/Timestamp.java')
-rw-r--r-- | libjava/java/sql/Timestamp.java | 49 |
1 files changed, 34 insertions, 15 deletions
diff --git a/libjava/java/sql/Timestamp.java b/libjava/java/sql/Timestamp.java index 22108f5..e480fff 100644 --- a/libjava/java/sql/Timestamp.java +++ b/libjava/java/sql/Timestamp.java @@ -39,6 +39,7 @@ exception statement from your version. */ package java.sql; import java.text.ParseException; +import java.text.DecimalFormat; import java.text.SimpleDateFormat; /** @@ -58,8 +59,10 @@ public class Timestamp extends java.util.Date /** * Used for parsing and formatting this date. */ - private static SimpleDateFormat sdf = + private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + private static DecimalFormat decimalFormat = new DecimalFormat("000000000"); + private static StringBuffer sbuf = new StringBuffer(29); /** * The nanosecond value for this object @@ -96,7 +99,7 @@ public class Timestamp extends java.util.Date try { - java.util.Date d = (java.util.Date)sdf.parseObject(str); + java.util.Date d = (java.util.Date) dateFormat.parseObject(str); if (d == null) throw new IllegalArgumentException(str); @@ -133,14 +136,24 @@ public class Timestamp extends java.util.Date /** * This method initializes a new instance of this class with the - * specified time value representing the number of seconds since + * specified time value representing the number of milliseconds since * Jan 1, 1970 at 12:00 midnight GMT. * * @param time The time value to intialize this <code>Time</code> to. */ public Timestamp(long date) { - super(date); + super(date - (date % 1000)); + nanos = (int) (date % 1000) * 1000000; + } + + /** + * Return the value of this Timestamp as the number of milliseconds + * since Jan 1, 1970 at 12:00 midnight GMT. + */ + public long getTime() + { + return super.getTime() + (nanos / 1000000); } /** @@ -150,7 +163,17 @@ public class Timestamp extends java.util.Date */ public String toString() { - return sdf.format(this) + "." + getNanos(); + synchronized (dateFormat) + { + sbuf.setLength(0); + dateFormat.format(this, sbuf, null); + sbuf.append('.'); + decimalFormat.format(nanos, sbuf, null); + int end = sbuf.length() - 1; + while (end > 20 && sbuf.charAt(end) == '0') + end--; + return sbuf.substring(0, end + 1); + } } /** @@ -182,12 +205,10 @@ public class Timestamp extends java.util.Date */ public boolean before(Timestamp ts) { - if (ts.getTime() > getTime()) + long time1 = getTime(); + long time2 = ts.getTime(); + if (time1 < time2 || (time1 == time2 && getNanos() < ts.getNanos())) return true; - - if (ts.getNanos() > getNanos()) - return true; - return false; } @@ -202,12 +223,10 @@ public class Timestamp extends java.util.Date */ public boolean after(Timestamp ts) { - if (ts.getTime() < getTime()) + long time1 = getTime(); + long time2 = ts.getTime(); + if (time1 > time2 || (time1 == time2 && getNanos() > ts.getNanos())) return true; - - if (ts.getNanos() < getNanos()) - return true; - return false; } |