📄 timestamp.java
字号:
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(); 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()); } /** * Gets this <code>Timestamp</code> object's <code>nanos</code> value. * * @return this <code>Timestamp</code> object's fractional seconds component * @see #setNanos */ public int getNanos() { return nanos; } /** * Sets this <code>Timestamp</code> object's <code>nanos</code> field * to the given value. * * @param n the new fractional seconds component * @exception java.lang.IllegalArgumentException if the given argument * is greater than 999999999 or less than 0 * @see #getNanos */ public void setNanos(int n) { if (n > 999999999 || n < 0) { throw new IllegalArgumentException("nanos > 999999999 or < 0"); } nanos = n; } /** * Tests to see if this <code>Timestamp</code> object is * equal to the given <code>Timestamp</code> object. * * @param ts the <code>Timestamp</code> value to compare with * @return <code>true</code> if the given <code>Timestamp</code> * object is equal to this <code>Timestamp</code> object; * <code>false</code> otherwise */ 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 <code>Timestamp</code> object is * equal to the given object. * * This version of the method <code>equals</code> has been added * to fix the incorrect * signature of <code>Timestamp.equals(Timestamp)</code> and to preserve backward * compatibility with existing class files. * * Note: This method is not symmetric with respect to the * <code>equals(Object)</code> method in the base class. * * @param ts the <code>Object</code> value to compare with * @return <code>true</code> if the given <code>Object</code> * instance is equal to this <code>Timestamp</code> object; * <code>false</code> otherwise */ public boolean equals(java.lang.Object ts) { if (ts instanceof Timestamp) { return this.equals((Timestamp)ts); } else { return false; } } /** * Indicates whether this <code>Timestamp</code> object is * earlier than the given <code>Timestamp</code> object. * * @param ts the <code>Timestamp</code> value to compare with * @return <code>true</code> if this <code>Timestamp</code> object is earlier; * <code>false</code> otherwise */ public boolean before(Timestamp ts) { if (super.before(ts)) { return true; } else { if (super.equals(ts)) { if (nanos < ts.nanos) { return true; } else { return false; } } else { return false; } } } /** * Indicates whether this <code>Timestamp</code> object is * later than the given <code>Timestamp</code> object. * * @param ts the <code>Timestamp</code> value to compare with * @return <code>true</code> if this <code>Timestamp</code> object is later; * <code>false</code> otherwise */ public boolean after(Timestamp ts) { if (super.after(ts)) { return true; } else { if (super.equals(ts)) { if (nanos > ts.nanos) { return true; } else { return false; } } else { return false; } } } /** * Compares this <code>Timestamp</code> object to the given * <code>Timestamp</code> object. * * @param ts the <code>Timestamp</code> object to be compared to * this <code>Timestamp</code> object * @return the value <code>0</code> if the two <code>Timestamp</code> * objects are equal; a value less than <code>0</code> if this * <code>Timestamp</code> object is before the given argument; * and a value greater than <code>0</code> if this * <code>Timestamp</code> object is after the given argument. * @since 1.2 */ public int compareTo(Timestamp ts) { int i = super.compareTo(ts); if (i == 0) { if (nanos > ts.nanos) { return 1; } else if (nanos < ts.nanos) { return -1; } } return i; } /** * Compares this <code>Timestamp</code> object to the given * <code>Object</code>, which must be a <code>Timestamp</code> * object. If the argument is not a <code>Timestamp</code> object, * this method throws a <code>ClassCastException</code> object. * (<code>Timestamp</code> objects are * comparable only to other <code>Timestamp</code> objects.) * * @param o the <code>Object</code> to be compared, which must be a * <code>Timestamp</code> object * @return the value <code>0</code> if this <code>Timestamp</code> object * and the given object are equal; a value less than <code>0</code> * if this <code>Timestamp</code> object is before the given argument; * and a value greater than <code>0</code> if this * <code>Timestamp</code> object is after the given argument. * * @exception ClassCastException if the argument is not a * <code>Timestamp</code> object */ public int compareTo(Object o) { return compareTo((Timestamp)o); } static final long serialVersionUID = 2745179027874758501L;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -