安卓:时间戳转换的问题!时间、问题

由网友(素酒青衣贤)分享简介:我解析一些XML的电视指南数据,并遇到了这个18位数字时间戳(633934980000869533)。看起来像C#的日期时间刻度。有谁知道如何将其转换为普通的Java日期/时间?I'm parsing some xml for tv guide data and ran into this 18 digit time...

我解析一些XML的电视指南数据,并遇到了这个18位数字时间戳(633934980000869533)。看起来像C#的日期时间刻度。有谁知道如何将其转换为普通的Java日期/时间?

I'm parsing some xml for tv guide data and ran into this 18 digit timestamp (633934980000869533). Looks like C#'s DateTime ticks. Does anyone know how to convert this to regular java Date/Time?

推荐答案

如果它的是的一个.NET蜱值,你刚刚得到了扩展和重订了。

If it is a .NET ticks value, you've just got to scale and rebase it.

午夜1970年1月1日重新通过621355968000000000蜱在.NET和1毫秒= 10,000蜱psented $ P $ ...这样:

Midnight on January 1st 1970 is represented by 621355968000000000 ticks in .NET, and 1 millisecond = 10,000 ticks... so:

public static Date fromDotNetTicks(long ticks)
{
    // Rebase to Jan 1st 1970, the Unix epoch
    ticks -= 621355968000000000L;
    long millis = ticks / 10000;
    return new Date(millis);
}

显然,你可能会想这些神奇数字提取到命名的常量:)

Obviously you'll probably want to extract those magic numbers into named constants :)

我刚刚检查,上面的code给出了2009年11月11日,凌晨1点UTC。

I've just checked, and the code above gives November 11th 2009, 1am UTC.

阅读全文

相关推荐

最新文章