How to get the current UTC time in seconds

2020-08-10 08:08发布

I have an application, which needs to compare the time in seconds.

I want to know how to get the current UTC time in seconds.

Can some one post an example of it how can we do this in Java?

7条回答
成全新的幸福
2楼-- · 2020-08-10 08:38

Joda makes everything simple

import org.joda.time.ReadableInstant;
import org.joda.time.DateTime;

import static org.joda.time.DateTimeZone.UTC;
import static org.joda.time.Seconds.secondsBetween;

...

ReadableInstant start = new DateTime("2011-01-01", UTC);
ReadableInstant end = new DateTime("2011-02-02", UTC);

int secondsDifference = secondsBetween(start, end).getSeconds();
查看更多
登录 后发表回答