这段C#代码对应的Java代码是什么?
public static readonly DateTime Epoch = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public static long Now
{
get { return (long)(DateTime.UtcNow - Epoch).TotalMilliseconds; }
}
我知道我可以使用 Date
对象来计算它,但是 Java 中的 Calendar
与 C# 的 DateTime 有何不同?
目前我使用的是Java:
public static long getCurrentTimeMillis(){
TimeZone here = TimeZone.getDefault();
Time time = new Time(here.toString());
time.setToNow();
return time.toMillis(false);
}
但是这两段代码之间的差异很明显...C# 代码比 Java 代码多出 150 万毫秒... 我如何从 Java 代码中获取正确的时间(以毫秒为单位)?
最佳答案
使用这个:
System.currentTimeMillis()
这是具有毫秒分辨率的 unixtime,也就是自 1970 年 1 月 1 日午夜以来的毫秒数,the epoch
将其转换为您的替代纪元:
long Offset = new Date(100, 0, 1, 0, 0, 0).getTime();
long DotNetTime = System.currentTimeMillis() - Offset;
当然,建议一次计算此偏移量并将其设为常量。
关于c# - 如何正确获取 Java 中的当前时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5260809/