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);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值