我正在读取一些存储为 long 的 Microsoft FileTime 值,并且正在尝试将其转换为人类可读的日期。
例如,该值131733712713359180
转换为:Wednesday, June 13, 2018 1:47:51pm
。这是使用在线工具完成的,这里:在线时间转换器
我已经让它在 Java 中工作得很好,但是当我尝试在 C# 中做到这一点时,我得到了错误的年份。我得到的输出是:13/06/0418 13:47:51
.
我用来进行转换的代码是:
public string CalculateTimestamp(Int64 epoch)
{
DateTime date = DateTime.Now;
try
{
date = new DateTime(epoch);
DateTime filetime = new DateTime(date.ToFileTime());
result = filetime.ToString();
}
catch (Exception uhoh)
{
result = "failedtoparsetimestamp";
}
return result;
}
在 Java 中进行转换时,这是我正在使用的代码。
public String calculateTimeStamp(long epoch) {
if (epoch == 0) {
return "--";
}
long unixDifference = 11644473600000L;
long timeStamp = (epoch / (10 * 1000)) - unixDifference;
Date date = new Date(timeStamp);
return date.toString();
}
我猜想 C# 转换应该更直接,但我无法弄清楚为什么年份是错误的。我都试过了UInt64
和Int64
,都给出了相同的(错误的)结果。
任何建议将不胜感激。
谢谢