java中可以用system.currenttimemillis() 获取当前时间的long形式,它的标示形式是从1970年1月1日起的到当前的毫秒的数。web service 是用java写的,把这个long型数据转成timestamp再存入mysql,所以用调用我们的web service可以直接把这个值传入。
但是.net下计算时间的方式不太一样,它是计算单位是ticks,这里就需要做一个c#时间转换。关于ticks,msdn上是这样说的:
a single tick represents one hundred nanoseconds or one ten-millionth of a second. the value of this property represents the number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, january 1, 0001.
就是从公元元年元月1日午夜到指定时间的千万分之一秒了,为了和java比较,说成万分之一毫秒。
( system.datetime.utcnow.ticks - new datetime(1970, 1, 1, 0, 0, 0).ticks)/10000;
如果要得到java中 system.currenttimemillis() 一样的结果,就可以做java与c#时间转换,写成上面那样,也可以这样写:
timespan ts=new timespan( system.datetime.utcnow.ticks - new datetime(1970, 1, 1, 0, 0, 0).ticks); (long)ts.totalmilliseconds;
需要注意的是这里是用的 system.datetime.utcnow 而不是 system.datetime.now ,因为我们在东八区,如果用后面那种形式就会发现时间会和想象当中的差了8个小时。java与c#时间转换到这里就彻底实现了。
以上就是如何将java与c#时间进行转换的详细内容。
