C# Jave 时间问题 毫秒区别

本文介绍了Epochtime的概念及其与Ticks的转换方法,并提供了一个Java函数实现从Epochtime到Ticks的转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先来个名词解释:
Epoch time:指从1970年1月1日零时起到现在为止的"second(秒) 数".
注意我给"second(秒) 数"加了引号,是因为在不一样的项目中,计量单位可能是不同的,需要仔细的阅读相关文档.比如Gtalk Api的Gmail Notifications文档中,所使用的date数为从1970年1月1日零时起到现在为止的"millisecond(毫秒) 数".
C#的Datetime.ticks:指从0001年1月1日零时起到现在为止的one ten-millionth of a second数量,或者one hundred nanoseconds of a second数量,也就是"千万分之一秒"的数量.
java的Date.getTime():这个方法返回目标时间到1970年1月1日零时为止的"millisecond(毫秒) 数".

然后来做个转换:
1 second(秒)=1000 millisecond(毫秒)=10 x 100 0000 one ten-millionth of a second(千万分之一秒)

好了,接下来是我们的java转换函数复制内容到剪贴板代码:
Java code
public static long GetTicks(String epochStr) { //convert the target-epoch time to a well-format string String date = new java.text.SimpleDateFormat("yyyy/MM/dd/HH/mm/ss").format(new Date (Long.parseLong(epochStr))); String[] ds=date.split("/"); //start of the ticks time Calendar calStart=Calendar.getInstance(); calStart.set(1, 1, 3, 0, 0, 0); //the target time Calendar calEnd=Calendar.getInstance(); calEnd.set(Integer.parseInt(ds[0]) ,Integer.parseInt(ds[1]),Integer.parseInt(ds[2]),Integer.parseInt(ds[3]),Integer.parseInt(ds[4]),Integer.parseInt(ds[5]) ); //epoch time of the ticks-start time long epochStart=calStart.getTime().getTime(); //epoch time of the target time long epochEnd=calEnd.getTime().getTime(); //get the sum of epoch time, from the target time to the ticks-start time long all=epochEnd-epochStart; //convert epoch time to ticks time long ticks=( (all/1000) * 1000000) * 10; return ticks; }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值