timestamp.java
来自「apache推出的net包」· Java 代码 · 共 486 行 · 第 1/2 页
JAVA
486 行
* 64-bit unsigned fixed-point number. * * @return NTP 64-bit timestamp value. * @throws NumberFormatException - if the string does not contain a parsable timestamp. */ protected static long decodeNtpHexString(String s) throws NumberFormatException { if (s == null) { throw new NumberFormatException("null"); } int ind = s.indexOf('.'); if (ind == -1) { if (s.length() == 0) return 0; return Long.parseLong(s, 16) << 32; // no decimal } return Long.parseLong(s.substring(0, ind), 16) << 32 | Long.parseLong(s.substring(ind + 1), 16); } /*** * Parses the string argument as a NTP hexidecimal timestamp representation string * (e.g. "c1a089bd.fc904f6d"). * * @param s - hexstring. * @return the Timestamp represented by the argument in hexidecimal. * @throws NumberFormatException - if the string does not contain a parsable timestamp. */ public static TimeStamp parseNtpString(String s) throws NumberFormatException { return new TimeStamp(decodeNtpHexString(s)); } /*** * Converts Java time to 64-bit NTP time representation. * * @param t Java time * @return NTP timestamp representation of Java time value. */ protected static long toNtpTime(long t) { boolean useBase1 = t < msb0baseTime; // time < Feb-2036 long baseTime; if (useBase1) { baseTime = t - msb1baseTime; // dates <= Feb-2036 } else { // if base0 needed for dates >= Feb-2036 baseTime = t - msb0baseTime; } long seconds = baseTime / 1000; long fraction = ((baseTime % 1000) * 0x100000000L) / 1000; if (useBase1) { seconds |= 0x80000000L; // set high-order bit if msb1baseTime 1900 used } long time = seconds << 32 | fraction; return time; } /*** * Computes a hashcode for this Timestamp. The result is the exclusive * OR of the two halves of the primitive <code>long</code> value * represented by this <code>TimeStamp</code> object. That is, the hashcode * is the value of the expression: * <blockquote><pre> * (int)(this.ntpValue()^(this.ntpValue() >>> 32)) * </pre></blockquote> * * @return a hash code value for this object. */ public int hashCode() { return (int) (ntpTime ^ (ntpTime >>> 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 ntpTime == ((TimeStamp) obj).ntpValue(); } return false; } /*** * Converts this <code>TimeStamp</code> object to a <code>String</code>. * The NTP timestamp 64-bit long value is represented as hex string with * seconds separated by fractional seconds by a decimal point; * e.g. c1a089bd.fc904f6d <=> Tue, Dec 10 2002 10:41:49.986 * * @return NTP timestamp 64-bit long value as hex string with seconds * separated by fractional seconds. */ public String toString() { return toString(ntpTime); } /*** * Left-pad 8-character hex string with 0's * * @param buf - StringBuffer which is appended with leading 0's. * @param l - a long. */ private static void appendHexString(StringBuffer buf, long l) { String s = Long.toHexString(l); for (int i = s.length(); i < 8; i++) buf.append('0'); buf.append(s); } /*** * Converts 64-bit NTP timestamp value to a <code>String</code>. * The NTP timestamp value is represented as hex string with * seconds separated by fractional seconds by a decimal point; * e.g. c1a089bd.fc904f6d <=> Tue, Dec 10 2002 10:41:49.986 * * @return NTP timestamp 64-bit long value as hex string with seconds * separated by fractional seconds. */ public static String toString(long ntpTime) { StringBuffer buf = new StringBuffer(); // high-order second bits (32..63) as hexstring appendHexString(buf, (ntpTime >>> 32) & 0xffffffffL); // low-order fractional seconds bits (0..31) as hexstring buf.append('.'); appendHexString(buf, ntpTime & 0xffffffffL); return buf.toString(); } /*** * Converts this <code>TimeStamp</code> object to a <code>String</code> * of the form: * <blockquote><pre> * EEE, MMM dd yyyy HH:mm:ss.SSS</pre></blockquote> * See java.text.SimpleDataFormat for code descriptions. * * @return a string representation of this date. */ public String toDateString() { DateFormat formatter = null; if (simpleFormatter != null) { formatter = (DateFormat) simpleFormatter.get(); } if (formatter == null) { // No cache yet, or cached formatter GC'd formatter = new SimpleDateFormat(NTP_DATE_FORMAT, Locale.US); formatter.setTimeZone(TimeZone.getDefault()); simpleFormatter = new SoftReference(formatter); } Date ntpDate = getDate(); synchronized (formatter) { return formatter.format(ntpDate); } } /*** * Converts this <code>TimeStamp</code> object to a <code>String</code> * of the form: * <blockquote><pre> * EEE, MMM dd yyyy HH:mm:ss.SSS UTC</pre></blockquote> * See java.text.SimpleDataFormat for code descriptions. * * @return a string representation of this date in UTC. */ public String toUTCString() { DateFormat formatter = null; if (utcFormatter != null) formatter = (DateFormat) utcFormatter.get(); if (formatter == null) { // No cache yet, or cached formatter GC'd formatter = new SimpleDateFormat(NTP_DATE_FORMAT + " 'UTC'", Locale.US); formatter.setTimeZone(TimeZone.getTimeZone("UTC")); utcFormatter = new SoftReference(formatter); } Date ntpDate = getDate(); synchronized (formatter) { return formatter.format(ntpDate); } } /*** * Compares two Timestamps numerically. * * @param anotherTimeStamp - the <code>TimeStamp</code> to be compared. * @return the value <code>0</code> if the argument TimeStamp is equal to * this TimeStamp; a value less than <code>0</code> if this TimeStamp * is numerically less than the TimeStamp argument; and a * value greater than <code>0</code> if this TimeStamp is * numerically greater than the TimeStamp argument * (signed comparison). */ public int compareTo(TimeStamp anotherTimeStamp) { long thisVal = this.ntpTime; long anotherVal = anotherTimeStamp.ntpTime; return (thisVal < anotherVal ? -1 : (thisVal == anotherVal ? 0 : 1)); } /*** * Compares this TimeStamp to another Object. If the Object is a TimeStamp, * this function behaves like <code>compareTo(TimeStamp)</code>. Otherwise, * it throws a <code>ClassCastException</code> (as TimeStamps are comparable * only to other TimeStamps). * * @param o the <code>Object</code> to be compared. * @return the value <code>0</code> if the argument is a TimeStamp * numerically equal to this TimeStamp; a value less than * <code>0</code> if the argument is a TimeStamp numerically * greater than this TimeStamp; and a value greater than * <code>0</code> if the argument is a TimeStamp numerically * less than this TimeStamp. * @exception ClassCastException if the argument is not a * <code>TimeStamp</code>. * @see java.lang.Comparable */ public int compareTo(Object o) { return compareTo((TimeStamp) o); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?