Convert Joda LocalDateTime to Unix timestamp

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

Example:

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

Jon Skeet
people
quotationmark

Given that you want the Unix timestamp "the given LocalDateTime, in UTC" the simplest approach is just to convert it to a DateTime by specifying the DateTimeZone for UTC, and convert that:

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 (e.g. java.util.Date) may expect milliseconds since the Unix epoch.

people

See more on this question at Stackoverflow