13

I wanted to convert joda time LocalTime to millis or milliseconds. I saw that the getLocalMillis is a protected method. Looks like there is no method to get the millis value of LocalTime.

So, do I have to get the values of each field in millis and then add them up to get the total millis ? Why does Joda Time not have a public method for getting Local Millis ?

Jedi Knight
  • 571
  • 2
  • 7
  • 18
  • `a public method for getting Local Millis`? What would you expect the method to return? The milliseconds from start of the day, or from the epoch? – leonbloy Mar 20 '13 at 02:24
  • 1
    @leonbloy - Well, I learned new things from the answer below. When I did not know those things, I was expecting milliseconds from epoch to LocalTime. – Jedi Knight Mar 20 '13 at 02:27
  • And what with those "Keywords for search engine"? Please remove that – leonbloy Mar 20 '13 at 02:28
  • @leonbloy - it makes the post easy to find. I will edit it. – Jedi Knight Mar 20 '13 at 02:29
  • Maybe related to this question: http://stackoverflow.com/questions/11665404/simplest-way-to-get-local-milliseconds-in-a-time-zone-with-joda-time – RenniePet Mar 09 '15 at 21:23

3 Answers3

25

A LocalTime does not represent an absolute instant in time, but rather it describes a time of any day for an arbitrary timezone.

Render your LocalTime into an DateTime via LocalTime#toDateTimeToday() or LocalTime#toDateTimeToday(DateTimeZone) if you're looking for the moment described by that time today. Or if you want that moment on another day, construct the appropriate LocalDate and see the LocalDate#toDateTime(...) methods. Then call DateTime#getMillis().

cheeken
  • 33,663
  • 4
  • 35
  • 42
4

You can use also .toDate().getTime()

Nickolay Savchenko
  • 1,474
  • 16
  • 28
2

If you really need the time milliseconds, you can also do:

LocalTime#get(DateTimeFieldType.millisOfDay())
twalthr
  • 2,584
  • 16
  • 15