| Method from java.sql.Timestamp Detail: |
public boolean after(Timestamp ts) {
return compareTo(ts) > 0;
}
Indicates whether this Timestamp object is
later than the given Timestamp object. |
public boolean before(Timestamp ts) {
return compareTo(ts) < 0;
}
Indicates whether this Timestamp object is
earlier than the given Timestamp object. |
public int compareTo(Timestamp ts) {
long thisTime = this.getTime();
long anotherTime = ts.getTime();
int i = (thisTime< anotherTime ? -1 :(thisTime==anotherTime?0 :1));
if (i == 0) {
if (nanos > ts.nanos) {
return 1;
} else if (nanos < ts.nanos) {
return -1;
}
}
return i;
}
Compares this Timestamp object to the given
Timestamp object. |
public int compareTo(Date o) {
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);
}
}
Compares this Timestamp object to the given
Date object. |
public boolean equals(Timestamp ts) {
if (super.equals(ts)) {
if (nanos == ts.nanos) {
return true;
} else {
return false;
}
} else {
return false;
}
}
Tests to see if this Timestamp object is
equal to the given Timestamp object. |
public boolean equals(Object ts) {
if (ts instanceof Timestamp) {
return this.equals((Timestamp)ts);
} else {
return false;
}
}
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. |
public int getNanos() {
return nanos;
}
Gets this Timestamp object's nanos value. |
public long getTime() {
long time = super.getTime();
return (time + (nanos / 1000000));
}
Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT
represented by this Timestamp object. |
public int hashCode() {
return super.hashCode();
}
{@inheritDoc}
The {@code hashCode} method uses the underlying {@code java.util.Date}
implementation and therefore does not include nanos in its computation. |
public void setNanos(int n) {
if (n > 999999999 || n < 0) {
throw new IllegalArgumentException("nanos > 999999999 or < 0");
}
nanos = n;
}
Sets this Timestamp object's nanos field
to the given value. |
public void setTime(long time) {
super.setTime((time/1000)*1000);
nanos = (int)((time%1000) * 1000000);
if (nanos < 0) {
nanos = 1000000000 + nanos;
super.setTime(((time/1000)-1)*1000);
}
}
Sets this Timestamp object to represent a point in time that is
time milliseconds after January 1, 1970 00:00:00 GMT. |
public String toString() {
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());
}
Formats a timestamp in JDBC timestamp escape format.
yyyy-mm-dd hh:mm:ss.fffffffff,
where ffffffffff indicates nanoseconds.
|
public static Timestamp valueOf(String s) {
final int YEAR_LENGTH = 4;
final int MONTH_LENGTH = 2;
final int DAY_LENGTH = 2;
final int MAX_MONTH = 12;
final int MAX_DAY = 31;
String date_s;
String time_s;
String nanos_s;
int year = 0;
int month = 0;
int day = 0;
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 = ":";
if (s == null) throw new java.lang.IllegalArgumentException("null string");
// 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);
}
// 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
boolean parsedDate = false;
if ((firstDash > 0) && (secondDash > 0) && (secondDash < date_s.length() - 1)) {
String yyyy = date_s.substring(0, firstDash);
String mm = date_s.substring(firstDash + 1, secondDash);
String dd = date_s.substring(secondDash + 1);
if (yyyy.length() == YEAR_LENGTH &&
(mm.length() >= 1 && mm.length() < = MONTH_LENGTH) &&
(dd.length() >= 1 && dd.length() < = DAY_LENGTH)) {
year = Integer.parseInt(yyyy);
month = Integer.parseInt(mm);
day = Integer.parseInt(dd);
if ((month >= 1 && month < = MAX_MONTH) && (day >= 1 && day < = MAX_DAY)) {
parsedDate = true;
}
}
}
if (! parsedDate) {
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(formatError);
}
return new Timestamp(year - 1900, month - 1, day, hour, minute, second, a_nanos);
}
Converts a String object in JDBC timestamp escape format to a
Timestamp value. |