I’m trying to set my Period object using the “millis” constructor and have all the relevant fields be updated accordingly (years, months, weeks, days, hours, minutes, seconds)
That is, using the following code:
mPeriod = new Period(millis, PeriodType.standard());
doesn’t fill all the relevant fields accordingly.
only weeks and minutes (for input millis of 1325965615539)
can someone please help me figure this thing out ?
You can normalize it using
Period#normalizedStandard();However, there’s no way
Periodcan “fill in all the relevant fields”, because it can’t make assumptions about the number of days in months or years. The best it can do is to turn it into weeks, days, and time fields.The Javadoc touches on this, but not in great detail:
(Emphasis mine)
If you need it to normalize into years and months, you need to construct the
Periodwith values in those fields.Alternatively, you could use a
Duration:Durationwith millisecondsUse
Duration#toPeriodFrom(ReadableInstant)to create aPeriodstarting at a given instant in time (e.g. newDateTime()). According to the docs, this should work:Here’s what
normalize()should do to your millisecond input:Notice the second line has been normalized, but only up to weeks.