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.
Asked
Active
Viewed 2,275 times
2
-
1Show us a reproducable example – skaffman Mar 07 '12 at 14:58
-
2...this method doesn't exist? – Sam DeHaan Mar 07 '12 at 15:00
-
sorry, I meant getMillis()... – Martin Asenov Mar 07 '12 at 15:36
2 Answers
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
-
I see... I read the docs but obviously I missed the 'part' thing. Since I don't see toStandartMillis I guess I have to use toStandardSeconds().getSeconds() * 1000. I will give it a try now... – Martin Asenov Mar 07 '12 at 15:39
-
5The proper way to do this would be `Period.toStandardDuration().getMillis()`. – Louis Wasserman Mar 07 '12 at 16:11
-
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