[New] Allow to create PxW durations
This commit is contained in:
parent
d82d3c2795
commit
731cea462c
|
@ -464,6 +464,25 @@ public final class PeriodDuration implements TemporalAmount, Serializable, Compa
|
|||
return period.isZero() && duration.isZero();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if all parts of this amount are zero except days, which must be a multiple of 7
|
||||
*
|
||||
* @return true if only the days are set, and are a multiple of 7
|
||||
*/
|
||||
public boolean isWeeks() {
|
||||
return duration.isZero() && period.getYears() == 0 && period.getMonths() == 0 && period.getDays() % 7 == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of weeks defined by the days which are a multiple of 7, i.e. if the days value is 14, then it
|
||||
* returns 2 for the weeks.
|
||||
*
|
||||
* @return the number of weeks represented by the days
|
||||
*/
|
||||
public int getWeeks() {
|
||||
return period.getDays() / 7;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
|
@ -707,8 +726,11 @@ public final class PeriodDuration implements TemporalAmount, Serializable, Compa
|
|||
return duration.toString();
|
||||
}
|
||||
if (duration.isZero()) {
|
||||
if (isWeeks())
|
||||
return "P" + getWeeks() + "W";
|
||||
return period.toString();
|
||||
}
|
||||
|
||||
return period + duration.toString().substring(1);
|
||||
}
|
||||
|
||||
|
|
|
@ -36,7 +36,10 @@ public class PeriodDurationFormatter {
|
|||
|
||||
u = (int) periodDuration.get(ChronoUnit.DAYS);
|
||||
if (u > 0) {
|
||||
sb.append(u).append(" ").append(i18n(locale, "days"));
|
||||
if (u % 7 == 0)
|
||||
sb.append(u / 7).append(" ").append(i18n(locale, "weeks"));
|
||||
else
|
||||
sb.append(u).append(" ").append(i18n(locale, "days"));
|
||||
periodDuration = periodDuration.minus(Period.ofDays(u));
|
||||
|
||||
if (periodDuration.isZero())
|
||||
|
|
|
@ -11,6 +11,7 @@ Fr=Fr
|
|||
Sa=Sa
|
||||
Su=Su
|
||||
days=Days
|
||||
weeks=Weeks
|
||||
months=Months
|
||||
years=Years
|
||||
minutes=Minutes
|
||||
|
|
|
@ -11,6 +11,7 @@ Fr=Fr
|
|||
Sa=Sa
|
||||
Su=So
|
||||
days=Tage
|
||||
weeks=Wochen
|
||||
months=Monate
|
||||
years=Jahre
|
||||
minutes=Minuten
|
||||
|
|
Loading…
Reference in New Issue