这很奇怪,但我没有找到任何关于将 Joda(Time) DateTime
转换为 Unix DateTime(或时间戳,以正确的名称为准)的结果。我该怎么做?
最佳答案
任何继承自BaseDateTime
的对象(包括DateTime
)都有该方法
public long getMillis()
根据API它:
Gets the milliseconds of the datetime instant from the Java epoch of 1970-01-01T00:00:00Z.
因此,获得 秒 的工作示例就是:
new DateTime().getMillis() / 1000
为了完整起见,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.
您还可以通过使用 TimeUnit API 删除魔数(Magic Number)除法来进一步改进它。 :
import java.util.concurrent.TimeUnit;
TimeUnit.MILLISECONDS.toSeconds(new DateTime().getMillis());
关于java - 乔达日期时间到 Unix 日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22681348/