Java Joda 日期时间到 Unix 日期时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22681348/
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
Joda DateTime to Unix DateTime
提问by アレックス
It's odd enough, but I didn't find any result about converting Joda(Time) DateTime
to Unix DateTime (or timestamp, whichever is the correct name). How can I do this?
这很奇怪,但我没有找到任何关于将 Joda(Time) 转换DateTime
为 Unix DateTime(或时间戳,以正确的名称为准)的结果。我怎样才能做到这一点?
采纳答案by reto
Any object that inherits from BaseDateTime
(including DateTime
) has the method
任何继承自BaseDateTime
(包括DateTime
)的对象都具有该方法
public long getMillis()
According to the APIit:
根据API它:
Gets the milliseconds of the datetime instant from the Java epoch of 1970-01-01T00:00:00Z.
从 Java 纪元 1970-01-01T00:00:00Z 获取日期时间瞬间的毫秒数。
So a working example to get the secondswould simply be:
因此,获取秒数的工作示例只是:
new DateTime().getMillis() / 1000
For completeness, the definition of the Unix Timestamp according to Wikipedia:
Unix time, or POSIX time, is a system for describing instants in time, defined as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970, not counting leap seconds
Unix 时间或 POSIX 时间是一种用于描述瞬间的系统,定义为自 1970 年 1 月 1 日星期四 00:00:00 协调世界时 (UTC) 以来经过的秒数,不计算闰秒
回答by Navrattan Yadav
Java 8 added a new API for working with dates and times. With Java 8 you can use
Java 8 添加了一个用于处理日期和时间的新 API。使用 Java 8,您可以使用
long unixTimestamp = Instant.now().getEpochSecond();