Timestamppublic class Timestamp extends Date A thin wrapper around java.util.Date that allows
the JDBC API to identify this as an SQL TIMESTAMP value.
It adds the ability
to hold the SQL TIMESTAMP fractional seconds value, by allowing
the specification of fractional seconds to a precision of nanoseconds.
A Timestamp also provides formatting and
parsing operations to support the JDBC escape syntax for timestamp values.
The precision of a Timestamp object is calculated to be either:
19 , which is the number of characters in yyyy-mm-dd hh:mm:ss
-
20 + s , which is the number
of characters in the yyyy-mm-dd hh:mm:ss.[fff...] and s represents the scale of the given Timestamp,
its fractional seconds precision.
Note: This type is a composite of a java.util.Date and a
separate nanoseconds value. Only integral seconds are stored in the
java.util.Date component. The fractional seconds - the nanos - are
separate. The Timestamp.equals(Object) method never returns
true when passed an object
that isn't an instance of java.sql.Timestamp ,
because the nanos component of a date is unknown.
As a result, the Timestamp.equals(Object)
method is not symmetric with respect to the
java.util.Date.equals(Object)
method. Also, the hashcode method uses the underlying
java.util.Date
implementation and therefore does not include nanos in its computation.
Due to the differences between the Timestamp class
and the java.util.Date
class mentioned above, it is recommended that code not view
Timestamp values generically as an instance of
java.util.Date . The
inheritance relationship between Timestamp
and java.util.Date really
denotes implementation inheritance, and not type inheritance. |
Fields Summary |
---|
private int | nanos | static final long | serialVersionUID |
Constructors Summary |
---|
public Timestamp(int year, int month, int date, int hour, int minute, int second, int nano)Constructs a Timestamp object initialized
with the given values.
super(year, month, date, hour, minute, second);
if (nano > 999999999 || nano < 0) {
throw new IllegalArgumentException("nanos > 999999999 or < 0");
}
nanos = nano;
| public Timestamp(long time)Constructs a Timestamp object
using a milliseconds time value. The
integral seconds are stored in the underlying date value; the
fractional seconds are stored in the nanos field of
the Timestamp object.
super((time/1000)*1000);
nanos = (int)((time%1000) * 1000000);
if (nanos < 0) {
nanos = 1000000000 + nanos;
super.setTime(((time/1000)-1)*1000);
}
|
Methods Summary |
---|
public boolean | after(java.sql.Timestamp ts)Indicates whether this Timestamp object is
later than the given Timestamp object.
return compareTo(ts) > 0;
| public boolean | before(java.sql.Timestamp ts)Indicates whether this Timestamp object is
earlier than the given Timestamp object.
return compareTo(ts) < 0;
| public int | compareTo(java.sql.Timestamp ts)Compares this Timestamp object to the given
Timestamp object.
int i = super.compareTo(ts);
if (i == 0) {
if (nanos > ts.nanos) {
return 1;
} else if (nanos < ts.nanos) {
return -1;
}
}
return i;
| public int | compareTo(java.util.Date o)Compares this Timestamp object to the given
Date , which must be a Timestamp
object. If the argument is not a Timestamp object,
this method throws a ClassCastException object.
(Timestamp objects are
comparable only to other Timestamp objects.)
if(o instanceof Timestamp) {
// When Timestamp instance compare it with a Timestamp
// Hence it is basically calling this.compareTo((Timestamp))o);
// Note typecasting is safe because o is instance of Timestamp
return compareTo((Timestamp)o);
} else {
// When Date doing a o.compareTo(this)
// will give wrong results.
Timestamp ts = new Timestamp(o.getTime());
return this.compareTo(ts);
}
| public boolean | equals(java.lang.Object ts)Tests to see if this Timestamp object is
equal to the given object.
This version of the method equals has been added
to fix the incorrect
signature of Timestamp.equals(Timestamp) and to preserve backward
compatibility with existing class files.
Note: This method is not symmetric with respect to the
equals(Object) method in the base class.
if (ts instanceof Timestamp) {
return this.equals((Timestamp)ts);
} else {
return false;
}
| public boolean | equals(java.sql.Timestamp ts)Tests to see if this Timestamp object is
equal to the given Timestamp object.
if (super.equals(ts)) {
if (nanos == ts.nanos) {
return true;
} else {
return false;
}
} else {
return false;
}
| public int | getNanos()Gets this Timestamp object's nanos value.
return nanos;
| public long | getTime()Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT
represented by this Timestamp object.
long time = super.getTime();
return (time + (nanos / 1000000));
| public void | setNanos(int n)Sets this Timestamp object's nanos field
to the given value.
if (n > 999999999 || n < 0) {
throw new IllegalArgumentException("nanos > 999999999 or < 0");
}
nanos = n;
| public void | setTime(long time)Sets this Timestamp object to represent a point in time that is
time milliseconds after January 1, 1970 00:00:00 GMT.
super.setTime((time/1000)*1000);
nanos = (int)((time%1000) * 1000000);
if (nanos < 0) {
nanos = 1000000000 + nanos;
super.setTime(((time/1000)-1)*1000);
}
| public java.lang.String | toString()Formats a timestamp in JDBC timestamp escape format.
yyyy-mm-dd hh:mm:ss.fffffffff ,
where ffffffffff indicates nanoseconds.
int year = super.getYear() + 1900;
int month = super.getMonth() + 1;
int day = super.getDate();
int hour = super.getHours();
int minute = super.getMinutes();
int second = super.getSeconds();
String yearString;
String monthString;
String dayString;
String hourString;
String minuteString;
String secondString;
String nanosString;
String zeros = "000000000";
String yearZeros = "0000";
StringBuffer timestampBuf;
if (year < 1000) {
// Add leading zeros
yearString = "" + year;
yearString = yearZeros.substring(0, (4-yearString.length())) +
yearString;
} else {
yearString = "" + year;
}
if (month < 10) {
monthString = "0" + month;
} else {
monthString = Integer.toString(month);
}
if (day < 10) {
dayString = "0" + day;
} else {
dayString = Integer.toString(day);
}
if (hour < 10) {
hourString = "0" + hour;
} else {
hourString = Integer.toString(hour);
}
if (minute < 10) {
minuteString = "0" + minute;
} else {
minuteString = Integer.toString(minute);
}
if (second < 10) {
secondString = "0" + second;
} else {
secondString = Integer.toString(second);
}
if (nanos == 0) {
nanosString = "0";
} else {
nanosString = Integer.toString(nanos);
// Add leading zeros
nanosString = zeros.substring(0, (9-nanosString.length())) +
nanosString;
// Truncate trailing zeros
char[] nanosChar = new char[nanosString.length()];
nanosString.getChars(0, nanosString.length(), nanosChar, 0);
int truncIndex = 8;
while (nanosChar[truncIndex] == '0") {
truncIndex--;
}
nanosString = new String(nanosChar, 0, truncIndex + 1);
}
// do a string buffer here instead.
timestampBuf = new StringBuffer(20+nanosString.length());
timestampBuf.append(yearString);
timestampBuf.append("-");
timestampBuf.append(monthString);
timestampBuf.append("-");
timestampBuf.append(dayString);
timestampBuf.append(" ");
timestampBuf.append(hourString);
timestampBuf.append(":");
timestampBuf.append(minuteString);
timestampBuf.append(":");
timestampBuf.append(secondString);
timestampBuf.append(".");
timestampBuf.append(nanosString);
return (timestampBuf.toString());
| public static java.sql.Timestamp | valueOf(java.lang.String s)Converts a String object in JDBC timestamp escape format to a
Timestamp value.
String date_s;
String time_s;
String nanos_s;
int year;
int month;
int day;
int hour;
int minute;
int second;
int a_nanos = 0;
int firstDash;
int secondDash;
int dividingSpace;
int firstColon = 0;
int secondColon = 0;
int period = 0;
String formatError = "Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]";
String zeros = "000000000";
String delimiterDate = "-";
String delimiterTime = ":";
StringTokenizer stringTokeninzerDate;
StringTokenizer stringTokeninzerTime;
if (s == null) throw new java.lang.IllegalArgumentException("null string");
int counterD = 0;
int intDate[] = {4,2,2};
int counterT = 0;
int intTime[] = {2,2,12};
// Split the string into date and time components
s = s.trim();
dividingSpace = s.indexOf(' ");
if (dividingSpace > 0) {
date_s = s.substring(0,dividingSpace);
time_s = s.substring(dividingSpace+1);
} else {
throw new java.lang.IllegalArgumentException(formatError);
}
stringTokeninzerTime = new StringTokenizer(time_s, delimiterTime);
stringTokeninzerDate = new StringTokenizer(date_s, delimiterDate);
while(stringTokeninzerDate.hasMoreTokens()) {
String tokenDate = stringTokeninzerDate.nextToken();
if(tokenDate.length() != intDate[counterD] ) {
throw new java.lang.IllegalArgumentException(formatError);
}
counterD++;
}
/*
//Commenting this portion out for checking of time
while(stringTokeninzerTime.hasMoreTokens()) {
String tokenTime = stringTokeninzerTime.nextToken();
if (counterT < 2 && tokenTime.length() != intTime[counterT] ) {
throw new java.lang.IllegalArgumentException(formatError);
}
counterT++;
}
*/
// Parse the date
firstDash = date_s.indexOf('-");
secondDash = date_s.indexOf('-", firstDash+1);
// Parse the time
if (time_s == null)
throw new java.lang.IllegalArgumentException(formatError);
firstColon = time_s.indexOf(':");
secondColon = time_s.indexOf(':", firstColon+1);
period = time_s.indexOf('.", secondColon+1);
// Convert the date
if ((firstDash > 0) && (secondDash > 0) &&
(secondDash < date_s.length()-1)) {
year = Integer.parseInt(date_s.substring(0, firstDash)) - 1900;
month =
Integer.parseInt(date_s.substring
(firstDash+1, secondDash)) - 1;
day = Integer.parseInt(date_s.substring(secondDash+1));
} else {
throw new java.lang.IllegalArgumentException(formatError);
}
// Convert the time; default missing nanos
if ((firstColon > 0) & (secondColon > 0) &
(secondColon < time_s.length()-1)) {
hour = Integer.parseInt(time_s.substring(0, firstColon));
minute =
Integer.parseInt(time_s.substring(firstColon+1, secondColon));
if ((period > 0) & (period < time_s.length()-1)) {
second =
Integer.parseInt(time_s.substring(secondColon+1, period));
nanos_s = time_s.substring(period+1);
if (nanos_s.length() > 9)
throw new java.lang.IllegalArgumentException(formatError);
if (!Character.isDigit(nanos_s.charAt(0)))
throw new java.lang.IllegalArgumentException(formatError);
nanos_s = nanos_s + zeros.substring(0,9-nanos_s.length());
a_nanos = Integer.parseInt(nanos_s);
} else if (period > 0) {
throw new java.lang.IllegalArgumentException(formatError);
} else {
second = Integer.parseInt(time_s.substring(secondColon+1));
}
} else {
throw new java.lang.IllegalArgumentException();
}
return new Timestamp(year, month, day, hour, minute, second, a_nanos);
|
|