在 Java 中将当前日期时间转换为 long

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21198882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:15:18  来源:igfitidea点击:

Convert current datetime to long in Java

javadatedatetime

提问by Pi Horse

I want to convert the current datetime to a long value in Java.

我想将当前日期时间转换为 Java 中的长值。

I know it has to be something with the DateTime

我知道它必须与 DateTime 有关

DateTime datetime = new DateTime();

But I couldn't find a way to convert the dateTime to long.

但是我找不到将 dateTime 转换为 long 的方法。

采纳答案by y?s??la

If you mean currentor nowthen System.currentTimeMillis()will do it for you. More info here: http://docs.oracle.com/javase/7/docs/api/java/lang/System.html

如果您的意思是当前现在,那么System.currentTimeMillis()将为您做。更多信息:http: //docs.oracle.com/javase/7/docs/api/java/lang/System.html

回答by hd1

Assuming you're using joda-time, the following line will sort you:

假设您正在使用joda-time,以下行将对您进行排序:

datetime.getMillis()

回答by Elliott Frisch

You can call System.currentTimeMillis(), which returns a long. Or, you can call BaseTime.getMillis(). Either will give you the date and time as a long.

您可以调用System.currentTimeMillis(),它返回一个long. 或者,您可以调用BaseTime.getMillis()。要么将日期和时间作为long.

回答by Basil Bourque

The other answers are correct.

其他答案都是正确的。

Four ways to count-from-epoch

从 epoch 开始计数的四种方法

To spell it out in detail, here are four ways to get the number of milliseconds since the UnixEpoch, the beginning of January 1, 1970 in UTC/GMT(no time zone offset). Presented in order of preference.

为了详细说明,这里有四种方法可以获取自Unix Epoch开始的毫秒数,即 1970 年 1 月 1 日开始的UTC/ GMT(无时区偏移)。按优先顺序呈现。

java.time

时间

The java.timeclasses built into Java 8 and later is defined by JSR 310. These classes supplant the outmoded java.util.Date/Calendar classes. These classes are inspired by Joda-Time but are re-architected.

Java 8 及更高版本中内置的java.time类由JSR 310定义。这些类取代了过时的 java.util.Date/Calendar 类。这些类受到 Joda-Time 的启发,但经过重新设计。

The java.time classes are capable of holding nanosecond resolution. In Java 8 specifically, capturing the current moment is limited to milliseconds, due to an old implementation of Clock. Java 9 brought a fresh implementation of Clockcapable of capturing the current moment in finer resolution, limited by the capability of the host OS and host hardware. On Oracle JDK 9.0.4 on macOS Sierra, I get values in microseconds, for six digits of decimal fraction.

java.time 类能够保持纳秒分辨率。特别是在 Java 8 中,由于Clock. Java 9 带来了一种全新的实现,Clock能够以更精细的分辨率捕获当前时刻,但受到主机操作系统和主机硬件能力的限制。在 macOS Sierra 上的 Oracle JDK 9.0.4 上,我获得了以微秒单位的值,表示十进制小数的六位数字。

The Instantclass includes a convenience method toEpochMilliprovides milliseconds. Beware of data loss, as any microsecondsor nanosecondsin the Instantare ignored when generating a count of milliseconds.

所述Instant类包括一个方便的方法toEpochMilli提供毫秒。当心的数据丢失,因为任何微秒纳秒Instant生成的计数时被忽略毫秒

long millis = Instant.now().toEpochMilli();

System

System

The Systemclass' method currentTimeMillisworks. But chances are that you are doing some other work with date-time values. If so, skip System and just use the date-time classes.

System类的方法的currentTimeMillis作品。但很有可能您正在使用日期时间值做一些其他工作。如果是这样,请跳过 System 并仅使用日期时间类。

long millis = System.currentTimeMillis();

Joda-Time

乔达时间

Joda-Timeis the popular third-party open-source library used in place of java.util.Date/Calendar. Its main class is DateTime, so perhaps this is what you had in mind given your question's use of that term.

Joda-Time是流行的第三方开源库,用于代替java.util.Date/ Calendar。它的主要类是DateTime,所以鉴于您的问题使用该术语,这也许就是您的想法。

The Joda-Timeproject is now in maintenance mode. The team advises migration to the java.timeclasses.

乔达时间的项目现在处于维护模式。该团队建议迁移到java.time类。

long millis = org.joda.time.DateTime.now().getMillis();

java.util.Date

java.util.Date

This class is best avoided, along with its siblings java.util.Calendarand java.text.SimpleDateFormat. The troublesome old date-time classes are now legacy, supplanted by java.time classes. But for your specific purpose, this class doeswork.

最好避免使用此类及其兄弟姐妹java.util.Calendarjava.text.SimpleDateFormat. 麻烦的旧日期时间类现在是遗留的,被 java.time 类取代。但是对于您的特定目的,该课程确实有效。

long millis = new java.util.Date().getTime();

By the way, I do notrecommend tracking time as a count-from-epoch. Makes debugging difficult, and near impossible to spot invalid data as humans cannot interpret a longas a date-time. Also, ambiguous as different systems use different epoch reference dates, and use different granularities of whole seconds, milliseconds, microseconds, nanoseconds, and others.

顺便说一下,我建议将时间作为从epoch 开始的计数。使调试变得困难,并且几乎不可能发现无效数据,因为人类无法将 a 解释long为日期时间。此外,由于不同的系统使用不同的纪元参考日期,并且使用整秒、毫秒微秒纳秒等不同的粒度,因此模棱两可。



About java.time

关于java.time

The java.timeframework is built into Java 8 and later. These classes supplant the troublesome old legacydate-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧的遗留日期时间类,例如java.util.Date, Calendar, & SimpleDateFormat

The Joda-Timeproject, now in maintenance mode, advises migration to the java.timeclasses.

现在处于维护模式Joda-Time项目建议迁移到java.time类。

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范是JSR 310

You may exchange java.timeobjects directly with your database. Use a JDBC drivercompliant with JDBC 4.2or later. No need for strings, no need for java.sql.*classes.

您可以直接与您的数据库交换java.time对象。使用符合JDBC 4.2或更高版本的JDBC 驱动程序。不需要字符串,不需要类。java.sql.*

Where to obtain the java.time classes?

从哪里获得 java.time 类?

The ThreeTen-Extraproject extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

ThreeTen-额外项目与其他类扩展java.time。该项目是未来可能添加到 java.time 的试验场。你可能在这里找到一些有用的类,比如IntervalYearWeekYearQuarter,和更多