[New] Allow to create PxW durations

This commit is contained in:
Robert von Burg 2023-06-08 07:12:17 +02:00
parent d82d3c2795
commit 731cea462c
Signed by: eitch
GPG Key ID: 75DB9C85C74331F7
4 changed files with 28 additions and 1 deletions

View File

@ -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);
}

View File

@ -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())

View File

@ -11,6 +11,7 @@ Fr=Fr
Sa=Sa
Su=Su
days=Days
weeks=Weeks
months=Months
years=Years
minutes=Minutes

View File

@ -11,6 +11,7 @@ Fr=Fr
Sa=Sa
Su=So
days=Tage
weeks=Wochen
months=Monate
years=Jahre
minutes=Minuten