Martin Asenov
Martin Asenov

Reputation: 1298

Joda Time's Period.getMillis returns inaccurate number

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.

Upvotes: 2

Views: 2285

Answers (2)

yegor256
yegor256

Reputation: 105133

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.

Upvotes: 1

Sean Owen
Sean Owen

Reputation: 66886

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

Upvotes: 7

Related Questions