2

I ran into a strange issue with JodaTime's Period class. I come to the point where I've instantiated a Period object, which is being printed as: PT8M19.966S, which clearly says 8 minutes and 19 seconds (this is correct at this point), and I call Period.toMillis. The result I get is some random number, such as 968, or 152, numbers that clearly are not what this method is supposed to return. So I wonder if it is some kind of bug, or misuse from my side.

skaffman
  • 398,947
  • 96
  • 818
  • 769
Martin Asenov
  • 1,288
  • 2
  • 20
  • 38

2 Answers2

7

You mean getMillis()? The javadoc says it only returns the millisecond part of the period, not the period's duration in milliseconds.

Sean Owen
  • 66,182
  • 23
  • 141
  • 173
1

As suggested by Louis above, you should convert Period to Duration first, and then get its milliseconds:

long millis = period.toStandardDuration().getMillis();

Main reason for that is that ReadablePeriod doesn't know exactly how many milliseconds it has inside, until you apply it to a calendar. For example, how many milliseconds are in one month? We can't get an answer until this month is applied to a calendar.

yegor256
  • 102,010
  • 123
  • 446
  • 597