예제 #1
0
  @Test
  public void registerFormatter() {
    // #register-formatter
    Formatters.register(
        LocalTime.class,
        new SimpleFormatter<LocalTime>() {

          private Pattern timePattern = Pattern.compile("([012]?\\d)(?:[\\s:\\._\\-]+([0-5]\\d))?");

          @Override
          public LocalTime parse(String input, Locale l) throws ParseException {
            Matcher m = timePattern.matcher(input);
            if (!m.find()) throw new ParseException("No valid Input", 0);
            int hour = Integer.valueOf(m.group(1));
            int min = m.group(2) == null ? 0 : Integer.valueOf(m.group(2));
            return new LocalTime(hour, min);
          }

          @Override
          public String print(LocalTime localTime, Locale l) {
            return localTime.toString("HH:mm");
          }
        });
    // #register-formatter

    Form<WithLocalTime> form = Form.form(WithLocalTime.class);
    WithLocalTime obj = form.bind(ImmutableMap.of("time", "23:45")).get();
    assertThat(obj.time, equalTo(new LocalTime(23, 45)));
    assertThat(form.fill(obj).field("time").value(), equalTo("23:45"));
  }
예제 #2
0
 static {
   register(Date.class, new Formats.DateFormatter("yyyy-MM-dd"));
   register(Date.class, new Formats.AnnotationDateFormatter());
   register(String.class, new Formats.AnnotationNonEmptyFormatter());
   registerOption();
 }