4

I'm looking to convert a Joda LocalDate to unix epoch time (long) in Java.

I've looked in to the LocalDate documentation and there doesn't appear to be anything on getting this value.

Link

I'm new to Joda and have been searching around and haven't found a right way of doing this yet. I know this has to be easy but I haven't figured it out. Any help would be appreciated.

itzmebibin
  • 9,199
  • 8
  • 48
  • 62
Billow
  • 53
  • 1
  • 6

1 Answers1

2

In a general case it's not as simple as it seems - Unix time defines a continuous timeline starting at 1970-01-01 00:00 UTC. LocalDate is - as the name suggests - local to a certain place in the world. The beginning and end of the day 2015-01-01 is at a different point in time in Sydney than in Berlin. Also, Unix time involves time whereas LocalDate... not ;) So - if it indeed makes sense to convert a Local Date to a UTC timestamp there are two questions you must answer for yourself:

  • in which time zone do you want to interpret the LocalDate?
  • what should be the time part after the conversion?

Assuming it does make sense to do it and you can answer the questions, you can implement it using

LocalDate#toDateTime(LocalTime time, DateTimeZone zone)#getMillis()

or - if you want the start of the day

LocalDate#toDateTimeAtStartOfDay(DateTimeZone zone)#getMillis()
Adam Michalik
  • 9,678
  • 13
  • 71
  • 102