@Test public void testConstructorsAndToString() { Period period = Period.ZERO.plusYears(1).plusMonths(2).plusDays(3); Duration duration = Duration.ZERO.plusHours(4).plusMinutes(5).plusSeconds(6); PeriodAndDuration pd = new PeriodAndDuration(period); assertEquals("P1Y2M3D", pd.toString()); pd = new PeriodAndDuration(duration); assertEquals("PT4H5M6S", pd.toString()); pd = new PeriodAndDuration(period, duration); assertEquals("P1Y2M3DT4H5M6S", pd.toString()); }
@Override public String toString() { if (period.isZero()) { if (duration.getSeconds() < 0) { return "-" + duration.negated().toString(); } else { return duration.toString(); } } if (duration.isZero()) { if (period.getYears() <= 0 && period.getMonths() <= 0 && period.getDays() <= 0) { return "-" + period.negated().toString(); } else { return period.toString(); } } StringBuilder sb = new StringBuilder(); int i; if (duration.getSeconds() <= 0 && period.getYears() <= 0 && period.getMonths() <= 0 && period.getDays() <= 0) { sb.append("-"); sb.append(period.negated().toString()); i = sb.length(); sb.append(duration.negated().toString()); } else { sb.append(period.toString()); i = sb.length(); sb.append(duration.toString()); } sb.deleteCharAt(i); return sb.toString(); }
PeriodAndDuration implements TemporalAmount { @Override public String toString() { if (period.isZero()) { if (duration.getSeconds() < 0) { return "-" + duration.negated().toString(); } else { return duration.toString(); } } if (duration.isZero()) { if (period.getYears() <= 0 && period.getMonths() <= 0 && period.getDays() <= 0) { return "-" + period.negated().toString(); } else { return period.toString(); } } StringBuilder sb = new StringBuilder(); int i; if (duration.getSeconds() <= 0 && period.getYears() <= 0 && period.getMonths() <= 0 && period.getDays() <= 0) { sb.append("-"); sb.append(period.negated().toString()); i = sb.length(); sb.append(duration.negated().toString()); } else { sb.append(period.toString()); i = sb.length(); sb.append(duration.toString()); } sb.deleteCharAt(i); return sb.toString(); } }
PeriodAndDuration implements TemporalAmount { @Override public String toString() { if (period.isZero()) { if (duration.getSeconds() < 0) { return "-" + duration.negated().toString(); } else { return duration.toString(); } } if (duration.isZero()) { if (period.getYears() <= 0 && period.getMonths() <= 0 && period.getDays() <= 0) { return "-" + period.negated().toString(); } else { return period.toString(); } } StringBuilder sb = new StringBuilder(); int i; if (duration.getSeconds() <= 0 && period.getYears() <= 0 && period.getMonths() <= 0 && period.getDays() <= 0) { sb.append("-"); sb.append(period.negated().toString()); i = sb.length(); sb.append(duration.negated().toString()); } else { sb.append(period.toString()); i = sb.length(); sb.append(duration.toString()); } sb.deleteCharAt(i); return sb.toString(); } PeriodAndDuration(Period period, Duration duration);  PeriodAndDuration(Period period);  PeriodAndDuration(Duration duration); }
PeriodAndDuration implements TemporalAmount { @Override public String toString() { if (period.isZero()) { if (duration.getSeconds() < 0) { return "-" + duration.negated().toString(); } else { return duration.toString(); } } if (duration.isZero()) { if (period.getYears() <= 0 && period.getMonths() <= 0 && period.getDays() <= 0) { return "-" + period.negated().toString(); } else { return period.toString(); } } StringBuilder sb = new StringBuilder(); int i; if (duration.getSeconds() <= 0 && period.getYears() <= 0 && period.getMonths() <= 0 && period.getDays() <= 0) { sb.append("-"); sb.append(period.negated().toString()); i = sb.length(); sb.append(duration.negated().toString()); } else { sb.append(period.toString()); i = sb.length(); sb.append(duration.toString()); } sb.deleteCharAt(i); return sb.toString(); } PeriodAndDuration(Period period, Duration duration);  PeriodAndDuration(Period period);  PeriodAndDuration(Duration duration); @Override List<TemporalUnit> getUnits(); @Override long get(TemporalUnit unit); @Override Temporal addTo(Temporal temporal); @Override Temporal subtractFrom(Temporal temporal); static PeriodAndDuration parse(String text); @Override String toString(); }
PeriodAndDuration implements TemporalAmount { @Override public String toString() { if (period.isZero()) { if (duration.getSeconds() < 0) { return "-" + duration.negated().toString(); } else { return duration.toString(); } } if (duration.isZero()) { if (period.getYears() <= 0 && period.getMonths() <= 0 && period.getDays() <= 0) { return "-" + period.negated().toString(); } else { return period.toString(); } } StringBuilder sb = new StringBuilder(); int i; if (duration.getSeconds() <= 0 && period.getYears() <= 0 && period.getMonths() <= 0 && period.getDays() <= 0) { sb.append("-"); sb.append(period.negated().toString()); i = sb.length(); sb.append(duration.negated().toString()); } else { sb.append(period.toString()); i = sb.length(); sb.append(duration.toString()); } sb.deleteCharAt(i); return sb.toString(); } PeriodAndDuration(Period period, Duration duration);  PeriodAndDuration(Period period);  PeriodAndDuration(Duration duration); @Override List<TemporalUnit> getUnits(); @Override long get(TemporalUnit unit); @Override Temporal addTo(Temporal temporal); @Override Temporal subtractFrom(Temporal temporal); static PeriodAndDuration parse(String text); @Override String toString(); static final PeriodAndDuration ZERO; final Period period; final Duration duration; }