推荐哪一个:Instant.now().toEpochMilli()或System.currentTimeMillis() [英] Which one is recommended: Instant.now().toEpochMilli() or System.currentTimeMillis()

查看:2559
本文介绍了推荐哪一个:Instant.now().toEpochMilli()或System.currentTimeMillis()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,我们可以采用多种方法来获取当前时间戳,但建议使用以下一种方法: Instant.now().toEpochMilli()System.currentTimeMillis()

In Java, we can have many different ways to get the current timestamp, but which one is recommended: Instant.now().toEpochMilli() or System.currentTimeMillis()

推荐答案

两者都很好.除了少数目的,都不推荐使用.

Both are fine. And neither is recommended except for a minority of purposes.

在Java中,我们可以有多种方法来获取当前时间戳,

In Java, we can have many different ways to get the current timestamp,

对于当前时间戳,只需使用Instant.now().无需转换为毫秒.

For current timestamp just use Instant.now(). No need to convert to milliseconds.

Java诞生之初的许多方法(标准库中也有许多方法)自以纪元为参数以来花费了long毫秒.但是,今天我会认为这是过时的.看看是否可以找到(或创建)或更现代的方法,例如使用Instant作为参数.面向对象,不要使用原始的long.它将使您的代码更清晰,更不言自明.

Many methods from the first years of Java, also many in the standard library, took a long number of milliseconds since the epoch as argument. However, today I would consider that old-fashioned. See if you can find — or create — or more modern method that takes for instance an Instant as argument instead. Go object-oriented and don’t use a primitive long. It will make your code clearer and more self-explanatory.

正如Eliott Frisch在评论中所说,如果这是用于测量经过时间,则您可能更喜欢System.nanoTime()的更高分辨率.

As Eliott Frisch said in a comment, if this is for measuring elapsed time, you may prefer the higher resolution of System.nanoTime().

假设您有充分的理由想要从纪元开始算起毫秒数,...

Assuming that you have good reasons for wanting a count of milliseconds since the epoch, …

推荐哪个:Instant.now().toEpochMilli()System.currentTimeMillis() [?]

which one is recommended: Instant.now().toEpochMilli() or System.currentTimeMillis()[?]

意见不一.有人会说您应该对所有日期和时间工作使用现代日期和时间API java.time.这意味着在这里Instant. Unsg java.time通常是一个好习惯,因为Java 1.0和1.1中的日期和时间类(DateCalendarTimeZoneDateFormatSimpleDateFormat等)设计得很差,现在已经过时了,当然不再有我们应该使用的了.另一方面,我并没有特别注意System.curremtTimeMillis()的任何设计问题(除了我上面提到的关于完全使用毫秒的long计数的内容,这显然是Instant.now().toEpochMilli()System.currentTimeMillis()固有的)

Opinions differ. Some will say that you should use java.time, the modern date and time API, for all of your date and time work. This would imply Instant here. Unsg java.time is generally a good habit since the date and time classes from Java 1.0 and 1.1 (Date, Calendar, TimeZone, DateFormat, SimpleDateFormat and others) are poorly designed and now long outdated, certainly not any that we should use anymore. On the other hand I am not aware of any design problem with System.curremtTimeMillis() in particular (except what I mentioned above about using a long count of milliseconds at all, which obviously is intrinsic to both Instant.now().toEpochMilli() and System.currentTimeMillis()).

如果两者之间的性能差异不大,我很难想象这很重要的情况.

If there is a slight performance difference between the two, I have a hard time imagining the situation where this will matter.

选择在您所处的环境中更具可读性和不那么令人惊讶的方式.

Take the option that you find more readable and less surprising in your context.

  • JSR 310 :: System.currentTimeMillis() vs Instant.toEpochMilli() :: TimeZone
  • Java current time different values in api

这篇关于推荐哪一个:Instant.now().toEpochMilli()或System.currentTimeMillis()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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