Convert Joda LocalDateTime to Unix Timestamp

How to convert org.joda.time.LocalDateTime to a Unix timestamp, given that the local time is in the UTC timezone?

Example:

new LocalDateTime(2015, 10, 02, 11, 31, 40) > 1443785500 .

+8
java unix-timestamp jodatime
source share
1 answer

Given that you need a Unix timestamp "given LocalDateTime , in UTC", the easiest way is to simply convert it to DateTime by specifying DateTimeZone for UTC and convert this:

 LocalDateTime local = new LocalDateTime(2015, 10, 02, 11, 31, 40); DateTime utc = local.toDateTime(DateTimeZone.UTC); long secondsSinceEpoch = utc.getMillis() / 1000; 

Note the use of seconds here as a Unix timestamp - other APIs (like java.util.Date ) can expect milliseconds from the Unix era.

+18
source share

All Articles