对纪元使用不同的初始日期

发布于 2024-10-30 16:39:24 字数 894 浏览 3 评论 0原文

所有,

我正在使用一个二进制规范,其时间戳字段定义为“自 2000 年 1 月 1 日 UTC 时间以来的毫秒数”。我正在进行以下计算:

public static final TimeZone UTC = TimeZone.getTimeZone("UTC") ;
public static final Calendar Y2K_EPOCH = Calendar.getInstance(UTC);
static {
    Y2K_EPOCH.clear();
    // Month is 0 based; day is 1 based.  Reset time to be first second of January 1, 2000
    Y2K_EPOCH.set(2000, 0, 1, 0, 0, 0);
}
public static final long MS_BETWEEN_ORIGINAL_EPOCH_AND_Y2K_EPOCH = Y2K_EPOCH.getTimeInMillis();

public static long getMillisecondsSinceY2K(Date date) {
    long time = date.getTime();
    if (time < MS_BETWEEN_ORIGINAL_EPOCH_AND_Y2K_EPOCH) {
        throw new IllegalArgumentException("Date must occur after January 1, 2000");
    }
    return time - MS_BETWEEN_ORIGINAL_EPOCH_AND_Y2K_EPOCH;
}

我的问题是,这是在标准 Java Date 对象和此数据类型之间进行转换的正确方法吗?有更好的方法吗?我知道乔达时间,但如果我能帮助的话,我宁愿不引入外部依赖。

All,

I am working with a binary specification whose TimeStamp fields are defined as "Milliseconds since January 1, 2000 UTC time". I am doing the following calculation:

public static final TimeZone UTC = TimeZone.getTimeZone("UTC") ;
public static final Calendar Y2K_EPOCH = Calendar.getInstance(UTC);
static {
    Y2K_EPOCH.clear();
    // Month is 0 based; day is 1 based.  Reset time to be first second of January 1, 2000
    Y2K_EPOCH.set(2000, 0, 1, 0, 0, 0);
}
public static final long MS_BETWEEN_ORIGINAL_EPOCH_AND_Y2K_EPOCH = Y2K_EPOCH.getTimeInMillis();

public static long getMillisecondsSinceY2K(Date date) {
    long time = date.getTime();
    if (time < MS_BETWEEN_ORIGINAL_EPOCH_AND_Y2K_EPOCH) {
        throw new IllegalArgumentException("Date must occur after January 1, 2000");
    }
    return time - MS_BETWEEN_ORIGINAL_EPOCH_AND_Y2K_EPOCH;
}

My question is, is this the correct way to do the conversion between standard Java Date objects and this datatype? Is there a better way of doing this? I know about Joda time but I'd rather not bring that external dependency in if I can help it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怪我闹别瞎闹 2024-11-06 16:39:24

我觉得不错。

请注意,您可以更改

long time = date.getTime();
if (time < MS_BETWEEN_ORIGINAL_EPOCH_AND_Y2K_EPOCH)
    ...

if (date.before(Y2K_EPOCH))
    ...

关于您对闰秒的担忧,这里是文档的摘录:

尽管 Date 类旨在反映协调世界时 (UTC),但它可能并不完全如此,具体取决于 Java 虚拟机的主机环境。在所有情况下,几乎所有现代操作系统都假设 1 天 = 24 × 60 × 60 = 86400 秒。然而,在 UTC 中,大约每隔一两年就会有一次额外的秒,称为“闰秒”。闰秒始终添加为当天的最后一秒,并且始终在 12 月 31 日或 6 月 30 日添加。例如,由于添加了闰秒,1995 年的最后一分钟长 61 秒。大多数计算机时钟不够精确,无法反映闰秒的区别。

Looks good to me.

Note that you could change

long time = date.getTime();
if (time < MS_BETWEEN_ORIGINAL_EPOCH_AND_Y2K_EPOCH)
    ...

to

if (date.before(Y2K_EPOCH))
    ...

Regarding your worry about leap seconds, here is an excerpt from the docs:

Although the Date class is intended to reflect coordinated universal time (UTC), it may not do so exactly, depending on the host environment of the Java Virtual Machine. Nearly all modern operating systems assume that 1 day = 24 × 60 × 60 = 86400 seconds in all cases. In UTC, however, about once every year or two there is an extra second, called a "leap second." The leap second is always added as the last second of the day, and always on December 31 or June 30. For example, the last minute of the year 1995 was 61 seconds long, thanks to an added leap second. Most computer clocks are not accurate enough to be able to reflect the leap-second distinction.

心如荒岛 2024-11-06 16:39:24

时间是一个棘手的混乱,尤其是 UTC 时间。假设您想要基于任意纪元的相当好的时间,像您所做的那样的简单减法应该没问题。如果您担心闰秒精度,我强烈建议您使用 Joda 或一些可靠的外部库。

Time is a tricky mess, especially UTC time. Assuming you want a pretty good time based of an arbitrary epoch, a simple subtraction like what you do should be fine. If you are worried about leap-seconds precision I would highly suggest you use Joda or some reliable external library.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文