TimeStamp1

package org.apache.commons.net.ntp;

import java.io.Serializable;
import java.lang.ref.SoftReference;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

public class TimeStamp
    implements Serializable, Comparable
{

    public TimeStamp(long ntpTime)
    {
        this.ntpTime = ntpTime;
    }

    public TimeStamp(String s)
        throws NumberFormatException
    {
        ntpTime = decodeNtpHexString(s);
    }

    public TimeStamp(Date d)
    {
        ntpTime = d != null ? toNtpTime(d.getTime()) : 0L;
    }

    public long ntpValue()
    {
        return ntpTime;
    }

    public long getSeconds()
    {
        return ntpTime >>> 32 & 4294967295L;
    }

    public long getFraction()
    {
        return ntpTime & 4294967295L;
    }

    public long getTime()
    {
        return getTime(ntpTime);
    }

    public Date getDate()
    {
        long time = getTime(ntpTime);
        return new Date(time);
    }

    public static long getTime(long ntpTimeValue)
    {
        long seconds = ntpTimeValue >>> 32 & 4294967295L;
        long fraction = ntpTimeValue & 4294967295L;
        fraction = Math.round((1000D * (double)fraction) / 4294967296D);
        long msb = seconds & 2147483648L;
        if(msb == 0L)
            return 2085978496000L + seconds * 1000L + fraction;
        else
            return -2208988800000L + seconds * 1000L + fraction;
    }

    public static TimeStamp getNtpTime(long date)
    {
        return new TimeStamp(toNtpTime(date));
    }

    public static TimeStamp getCurrentTime()
    {
        return getNtpTime(System.currentTimeMillis());
    }

    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 0L;
            else
                return Long.parseLong(s, 16) << 32;
        } else
        {
            return Long.parseLong(s.substring(0, ind), 16) << 32 | Long.parseLong(s.substring(ind + 1), 16);
        }
    }

你可能感兴趣的:(java,apache,.net)