DurationFormat extends NumberFormat { @Override public StringBuffer format(double duration, StringBuffer toAppendTo, FieldPosition pos) { if (displayAsHoursAndMinutes()) { toAppendTo.append(formatAsHoursAndMinutes(duration)); } else { toAppendTo.append(DURATION_FORMAT.format(duration)); } return toAppendTo; }  @Override StringBuffer format(double duration, StringBuffer toAppendTo, FieldPosition pos); @Override StringBuffer format(long number, StringBuffer toAppendTo, FieldPosition pos); @Override Number parse(String source, ParsePosition parsePosition); static final NumberFormat HOUR_OR_MINUTES_FORMAT; static final NumberFormat DURATION_FORMAT; }
@Test public void formatDurationInHoursAndMinutes() { UserSettings.instance().setDurationFormat(UserSettings.DurationFormat.HOURS_AND_MINUTES); ProjectActivity act; DateTime startTime = new DateTime(DateUtils.getNow()); act = new ProjectActivity(startTime, startTime.plusMinutes(45), null); assertEquals("00:45", format.format(act.getDuration())); act = new ProjectActivity(startTime, startTime.plusMinutes(30), null); assertEquals("00:30", format.format(act.getDuration())); act = new ProjectActivity(startTime, startTime.plusHours(1).plusMinutes(30), null); assertEquals("01:30", format.format(act.getDuration())); act = new ProjectActivity(startTime, startTime.plusMinutes(20), null); assertEquals("00:20", format.format(act.getDuration())); act = new ProjectActivity(startTime, startTime.plusHours(7).plusMinutes(15), null); assertEquals("07:15", format.format(act.getDuration())); }