mirror of
git://gcc.gnu.org/git/gcc.git
synced 2025-01-12 05:44:26 +08:00
Timestamp.java (valueOf): Correctly handle nanoseconds.
2003-11-10 Gary Benson <gbenson@redhat.com> * java/sql/Timestamp.java (valueOf): Correctly handle nanoseconds. From-SVN: r73437
This commit is contained in:
parent
b1e0ae3eaa
commit
84083f51be
@ -1,3 +1,8 @@
|
||||
2003-11-10 Gary Benson <gbenson@redhat.com>
|
||||
|
||||
* java/sql/Timestamp.java (valueOf): Correctly handle
|
||||
nanoseconds.
|
||||
|
||||
2003-11-09 Tom Tromey <tromey@redhat.com>
|
||||
|
||||
* java/net/Inet4Address.java (serialVersionUID): Updated.
|
||||
|
@ -58,11 +58,7 @@ public class Timestamp extends java.util.Date
|
||||
/**
|
||||
* Used for parsing and formatting this date.
|
||||
*/
|
||||
// Millisecond will have to be close enough for now.
|
||||
private static SimpleDateFormat parse_sdf =
|
||||
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSS");
|
||||
|
||||
private static SimpleDateFormat format_sdf =
|
||||
private static SimpleDateFormat sdf =
|
||||
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
|
||||
/**
|
||||
@ -79,14 +75,35 @@ public class Timestamp extends java.util.Date
|
||||
*/
|
||||
public static Timestamp valueOf(String str)
|
||||
{
|
||||
int nanos = 0;
|
||||
int dot = str.indexOf('.');
|
||||
if (dot != -1)
|
||||
{
|
||||
if (str.lastIndexOf('.') != dot)
|
||||
throw new IllegalArgumentException(str);
|
||||
|
||||
int len = str.length() - dot - 1;
|
||||
if (len < 1 || len > 9)
|
||||
throw new IllegalArgumentException(str);
|
||||
|
||||
nanos = Integer.parseInt(str.substring(dot + 1));
|
||||
for (int i = len; i < 9; i++)
|
||||
nanos *= 10;
|
||||
|
||||
str = str.substring(0, dot);
|
||||
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
java.util.Date d = (java.util.Date)parse_sdf.parseObject(str);
|
||||
java.util.Date d = (java.util.Date)sdf.parseObject(str);
|
||||
|
||||
if (d == null)
|
||||
throw new IllegalArgumentException(str);
|
||||
else
|
||||
return new Timestamp(d.getTime());
|
||||
|
||||
Timestamp ts = new Timestamp(d.getTime() + nanos / 1000000);
|
||||
ts.nanos = nanos;
|
||||
return ts;
|
||||
}
|
||||
catch (ParseException e)
|
||||
{
|
||||
@ -133,7 +150,7 @@ public class Timestamp extends java.util.Date
|
||||
*/
|
||||
public String toString()
|
||||
{
|
||||
return format_sdf.format(this) + "." + getNanos();
|
||||
return sdf.format(this) + "." + getNanos();
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user