在 Java 或 Scala 中将微秒字符串转换为日期 [英] Convert microseconds string to date in Java or Scala

查看:52
本文介绍了在 Java 或 Scala 中将微秒字符串转换为日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Java/Scala 中将时间戳(以微秒为单位)字符串转换为日期.我的目标是比较两个时间戳并找出它们之间的差异.我正在使用 java 8,示例时间戳字符串是 1474457086337977.我想将其转换为 Date 或 Timestamp 实例.

how to convert timestamp(in microseconds) string to date in Java/Scala. My goal is to compare two timestamps and find the differences between them. I'm using java 8 and example Timestamp string is 1474457086337977. I would like to convert this into Date or Timestamp instance.

推荐答案

那么,如何将这些微秒转换为毫秒,然后只创建一个时间戳对象?

Well, what about converting those microseconds to milliseconds and then just create a timestamp object?

long microsecs = 1474457086337977L;
long millis = TimeUnit.MILLISECONDS.convert(microsecs, TimeUnit.MICROSECONDS);
Timestamp time = new Timestamp(millis);

那不行吗?

--编辑

解决答案中留下的评论:

To address the comments left in the answer:

关于 Java 8 的新日期时间 API

首先,既然您提到您使用的是 Java 8,我完全同意更好的方法是使用新的 Java 8 日期/时间 API.然而,即使在使用 Java 8 时,这也是一种奢侈,因为您可能仍在使用旧的 Java 日期/时间类与旧的 API 进行交互,或者仅仅是因为您的 API 的其余部分仍在使用它们并且你不想开始混合的东西.

First, since you mention that you're using Java 8 I totally agree that a better approach would be to use the new Java 8 Date/Time API. However, this is a luxury you don't always have even when working with Java 8 because you may still be interacting with an old API still using the old Java Date/Time classes, or simply because the rest of your API still uses them and you don't want to start mixing things.

如果您已经知道这一点,您的问题中不清楚,您似乎确定要使用 java.util.Datejava.sql.Timestamp 我没有质疑,我只是解决了你问题的参数.

It is not clear in your question if you already know this, you seem to be sure that you want to use either java.util.Date or java.sql.Timestamp and I didn't question that, I just worked around the parameters of your question.

显然,新的 Java 日期/时间 API 比旧的要好得多,但仍有数百万行代码仍在使用旧的 API,并且它们可以工作.我又一次认为这超出了答案的范围,而且您似乎已经在这里有了其他好的答案来解决这个问题.

Clearly the new Java date/time APIs are much better than the old ones, but there are millions of lines of code out there still using the old APIs and they work. Yet again I thought that was out of the scope of the answer and it seems you already have other good answers here to address that.

关于可能的数据丢失

一个评论提到答案可能会导致数据丢失.我认为在 Java 中,所有整数运算都可能发生下溢或溢出.我的错误可能是没有提到它.

One comment mentions that the answer might run into data loss. I think in Java all integer arithmetic is subject to potential underflow or overflow. My mistake is probably not have mentioned it.

TimeUnit.convert 方法确实可能在某些情况下最终导致上溢或下溢.它记录在方法中.

It is true that TimeUnit.convert method might end up causing overflows or underflows in certain scenarios. It is documented in the method.

  • 一纳秒是十亿分之一秒 (1/1000000000)
  • 一微秒是百万分之一秒 (1/1000000).
  • 一毫秒是千分之一秒 (1/1000)

这意味着,一旦表示为 long,毫秒数应该比微秒数小得多,对吗?

Which means that, once expressed as a long, a millisecond number should be a much smaller number than a microsecond one, right?

TimeUnit.convert 使用的公式如下

final long MICROS = 1000000L;
final long MILLIS = 1000L;

long microsecs = 1474457086337977L;
long millisecs = microsecs / (MICROS / MILLIS)

这意味着只有当你的微秒真的很小的时候,你才会遇到数据丢失,例如如果您的时间少于 1,000 微秒.你应该验证你的代码永远不会进入这样的场景.

Which means you would run into data loss only if your microseconds are really small numbers e.g. if you had less than 1,000 microseconds. You should validate your code never goes into a scenario like this.

此答案中留下的一条评论认为,正确的答案可能应该使用纳秒,但同样,纳秒长值将比微秒大得多,因此,在转换为纳秒期间,您可能仍会遇到溢出.

One comment left in this answer argues that the right answer should probably use nanoseconds, but then again a nanosecond long value would be a much bigger number than your microseconds and so, during conversions to nanoseconds you might still run into overflows.

例如,想想如果你有 Long.MAX_VALUE 微秒会发生什么,你怎么能只使用 Java 长算术将它转换为纳秒而没有溢出,因为纳秒应该是一个更大的数字比您的 Long.MAX_VALUE 微秒?

For example, think what would happen if you had Long.MAX_VALUE microseconds, how could you convert that to nanoseconds using just Java long arithmetic without an overflow given that nanoseconds are supposed to be a much bigger number than your Long.MAX_VALUE microseconds?

我的观点是,无论您使用 Java 8 Date Time 还是旧的 Java Date Time API,您都需要一个 long 值来表示时间线中的一个瞬间,但是 long 在过去或未来可以走多远方面存在限制,并且当您在单位之间进行转换时,该算术会下溢和上溢,并且无法绕过它,您应该意识到这一点以避免非常讨厌的错误.

My point being that regardless of you using Java 8 Date Time or legacy Java Date Time APIs you need a long value representing an instant in the time line, but that long has limitations in regards to how far in the past or how far in the future you can go, and when you do conversions between units, that arithmetic is subject to underflow and overflow and there's no way around it and you should be aware of that to avoid very nasty bugs.

再一次,我认为这是一个既定的问题,超出了问题的范围,我提出这个问题只是因为我对这个遗漏投了一些反对票.

Once more, I thought that was a given and outside the scope of the question and I bring it up only because I got some downvotes for this omission.

这篇关于在 Java 或 Scala 中将微秒字符串转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆