在 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
Convert current datetime to long in Java
提问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
回答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 Clock
capable 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 Instant
class includes a convenience method toEpochMilli
provides milliseconds. Beware of data loss, as any microsecondsor nanosecondsin the Instant
are ignored when generating a count of milliseconds.
所述Instant
类包括一个方便的方法toEpochMilli
提供毫秒。当心的数据丢失,因为任何微秒或纳秒在Instant
生成的计数时被忽略毫秒。
long millis = Instant.now().toEpochMilli();
System
System
The System
class' 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.Calendar
and 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.Calendar
和java.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 long
as 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 类?
- Java SE 8, Java SE 9, and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6and Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android (<26), the ThreeTenABPproject adapts ThreeTen-Backport(mentioned above). See How to use ThreeTenABP….
- Java SE 8、Java SE 9及更高版本
- 内置。
- 具有捆绑实现的标准 Java API 的一部分。
- Java 9 添加了一些小功能和修复。
- Java SE 6和Java SE 7
- 多的java.time功能后移植到Java 6和7在ThreeTen-反向移植。
- 安卓
- 更高版本的 Android 捆绑实现 java.time 类。
- 对于早期的 Android(<26),ThreeTenABP项目采用了ThreeTen-Backport(上面提到过)。请参阅如何使用ThreeTenABP ...。
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 的试验场。你可能在这里找到一些有用的类,比如Interval
,YearWeek
,YearQuarter
,和更多。