Since Period
s are defined by individual component fields (e.g. 5 years, 7 weeks), they cannot (easily) be converted directly to, say, minute values without first associating them with a particular instant in time.
For example, if you have a period of 2 months, how many minutes does it contain? In order to know that, you need to know which two months. Perhaps:
- June and July? (30 + 31 = 61 days)
- July and August? (31 + 31 = 62 days)
- February of a leap year and March? (29 + 31 = 60 days)
Each one of those is going to have a different number of minutes.
That in mind, there are a few ways to approach this. Here are a couple:
If you're guaranteed that your Period
won't contain months (or higher), you can just use toStandardSeconds()
:
Period period = new Period(60, 40, 20, 500);
System.out.println(period.toStandardSeconds().getSeconds() / 60.0);
// outputs 3640.3333333333335
However, if you do end up with a month value in your Period
, you'll get (per the javadoc) an UnsupportedOperationException
:
java.lang.UnsupportedOperationException: Cannot convert to Seconds as this period contains months and months vary in length
Otherwise, you can associate the Period
with an instant in time and use a Duration
:
Period period = new Period(1, 6, 2, 2, 5, 4, 3, 100);
// apply the period starting right now
Duration duration = period.toDurationFrom(new DateTime());
System.out.println(duration.toStandardSeconds().getSeconds() / 60.0);
// outputs 810964.05 (when right now is "2012-01-09T13:36:43.880-06:00")
Note that #2 will print different values depending on the day the code runs. But that's just the nature of it.
As an alternative, you might consider just using Duration
s from the start (if possible) and not using Period
s at all.