📄 timestamp.java
字号:
* Computes a hashcode for this Long. The result is the exclusive * OR of the two halves of the primitive <code>long</code> value * represented by this <code>Long</code> object. That is, the hashcode * is the value of the expression: * <blockquote><pre> * (int)(this.longValue()^(this.longValue()>>>32)) * </pre></blockquote> * * @return a hash code value for this object. */ /* public int hashCode() { return (int)(timestamp ^ (timestamp >> 32)); } */ /** * Compares this object against the specified object. * The result is <code>true</code> if and only if the argument is * not <code>null</code> and is a <code>Long</code> object that * contains the same <code>long</code> value as this object. * * @param obj the object to compare with. * @return <code>true</code> if the objects are the same; * <code>false</code> otherwise. */ public boolean equals(Object obj) { if (obj instanceof Timestamp) { return equals((Timestamp)obj); } return false; } /** * @return internal state of the Timestamp as an XML ASCII string */ public final String toXml() { return toXml((String)null); } /** * @return internal state of the Timestamp as an XML ASCII string * without human readable JDBC formatting */ public final String toXml(String extraOffset) { return toXml(extraOffset, false); } /** * Dump state of this object into a XML ASCII string. * <br> * @param extraOffset indenting of tags for nice output * @param literal true -> show human readable format as well (JDBC escape format) * "2002-02-10 10:52:40.879456789" * @return internal state of the Timestamp as a XML ASCII string */ public final String toXml(String extraOffset, boolean literal) { StringBuffer sb = new StringBuffer(200); String offset = "\n "; if (extraOffset != null) offset += extraOffset; if (literal) { sb.append(offset).append("<").append(tagName).append(" nanos='").append(getTimestamp()).append("'>"); sb.append(offset).append(" ").append(toString()); sb.append(offset).append("</").append(tagName).append(">"); } else { sb.append(offset).append("<").append(tagName).append(" nanos='").append(getTimestamp()).append("'/>"); } return sb.toString(); } /** * Convert milliseconds to some more human readable representation. * @param millis An amount of elapsed milliseconds * @return A human readable time string */ public final static String millisToNice(long millis) { long seconds = millis / 1000; long sec = (seconds % 3600) % 60; long min = (seconds % 3600) / 60; long hour = seconds / 3600; StringBuffer strbuf = new StringBuffer(60); strbuf.append(" [ "); if (hour > 0L) strbuf.append(hour + " h "); if (min > 0L) strbuf.append(min + " min "); if (sec > 0L) strbuf.append(sec + " sec "); strbuf.append((millis % 1000) + " millis"); strbuf.append(" ]"); return strbuf.toString(); } /** * Stop execution for some given milliseconds. * Returns earlier on Interrupted exception * * @param millis amount of milliseconds to wait */ public static void sleep(long millis) { try { Thread.sleep(millis); } catch (InterruptedException i) { } } /** * Test only. * <pre> * javac -g -d $XMLBLASTER_HOME/classes Timestamp.java * * java org.xmlBlaster.util.Timestamp * * Dump a nanosecond 'long' to a string representation: * * java org.xmlBlaster.util.Timestamp 1076677832527000001 * * Dump a a string representation to a nanosecond 'long': * * java org.xmlBlaster.util.Timestamp "2004-02-13 14:10:32.527000001" * </pre> */ public static void main(String[] args) { if (args.length > 0) { if (args[0].indexOf(":") != -1) { // 2004-02-13 14:10:32.527000001 Timestamp tt = Timestamp.valueOf(args[0]); System.out.println(tt.toXml("", true)); } else { long nanos = Long.valueOf(args[0]).longValue(); Timestamp tt = new Timestamp(nanos); System.out.println(tt.toXml("", true)); } System.exit(0); } int count = 5; StringBuffer buf = new StringBuffer(count * 120); for (int ii=0; ii<count; ii++) test(buf); System.out.println(buf); testToString(); testToString(); testToString(); System.out.println("TEST 1"); testValueOf(); testValueOf(); testValueOf(); System.out.println("TEST 2"); testToXml(false); testToXml(false); testToXml(false); System.out.println("TEST 3"); testToXml(true); testToXml(true); testToXml(true); Timestamp ts1 = new Timestamp(); Timestamp ts2 = new Timestamp(); if (ts1.equals(ts2)) System.out.println("ERROR: equals()"); if (ts2.compareTo(ts1) < 1) System.out.println("ERROR: compareTo()"); if (ts2.toString().equals(Timestamp.valueOf(ts2.toString()).toString()) == false) System.out.println("ERROR: valueOf() ts2.toString()=" + ts2.toString() + " Timestamp.valueOf(ts2.toString()).toString()=" + Timestamp.valueOf(ts2.toString()).toString()); System.out.println(ts2.toXml("")); System.out.println(ts2.toXml("", true)); } /** Test only */ private static final Timestamp test(StringBuffer buf) { Timestamp ts = new Timestamp(); buf.append("Timestamp toString()=" + ts.toString() + " getTimestamp()=" + ts.getTimestamp() + " getMillis()=" + ts.getMillis() + " getMillisOnly()=" + ts.getMillisOnly() + " getNanosOnly()=" + ts.getNanosOnly() + " getScannedAndDumped()=" + Timestamp.valueOf(ts.toString()).toString() + "\n"); return ts; } /** Test only */ private static final void testToString() { int count = 10000; long start = System.currentTimeMillis(); Timestamp ts = new Timestamp(); for (int ii=0; ii<count; ii++) { ts.toString(); } long elapsed = System.currentTimeMillis() - start; System.out.println("toString(): " + count + " toString " + elapsed + " millisec -> " + ((elapsed*1000.*1000.)/count) + " nanosec/toString()"); } /** Test only */ private static final void testValueOf() { int count = 10000; Timestamp ts1 = new Timestamp(); String val = ts1.toString(); long start = System.currentTimeMillis(); for (int ii=0; ii<count; ii++) { Timestamp.valueOf(val); } long elapsed = System.currentTimeMillis() - start; System.out.println("valueOf(): " + count + " valueOf " + elapsed + " millisec -> " + ((elapsed*1000.*1000.)/count) + " nanosec/valueOf()"); } /** Test only */ private static final void testToXml(boolean literal) { int count = 10000; long start = System.currentTimeMillis(); Timestamp ts = new Timestamp(); for (int ii=0; ii<count; ii++) { ts.toXml(null, literal); } long elapsed = System.currentTimeMillis() - start; System.out.println("toXml(" + literal + "): " + count + " toXml " + elapsed + " millisec -> " + ((elapsed*1000.*1000.)/count) + " nanosec/toXml()"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -