1

What's the advantage of writing the first, more verbose, version instead of the compact, later one?

public static final DateTimeFormatter ISO_LOCAL_DATE;
static {
    ISO_LOCAL_DATE = new DateTimeFormatterBuilder()
            .appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
            .appendLiteral('-')
            .appendValue(MONTH_OF_YEAR, 2)
            .appendLiteral('-')
            .appendValue(DAY_OF_MONTH, 2)
            .toFormatter(ResolverStyle.STRICT, IsoChronology.INSTANCE);
}

instead of simply writing

public static final DateTimeFormatter ISO_LOCAL_DATE2 = new DateTimeFormatterBuilder()
        .appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
        .appendLiteral('-')
        .appendValue(MONTH_OF_YEAR, 2)
        .appendLiteral('-')
        .appendValue(DAY_OF_MONTH, 2)
        .toFormatter(ResolverStyle.STRICT, IsoChronology.INSTANCE);

The first style is used in the Java API, e.g. in java.time.format.DateTimeFormatter.

Sean Bright
  • 118,630
  • 17
  • 138
  • 146
Stefan K.
  • 7,701
  • 6
  • 52
  • 64
  • Possibility of handling exception. – ortis Apr 17 '15 at 13:26
  • 2
    The former is more readable but they are essentially equivalent (ok, not strictly equivalent: http://stackoverflow.com/questions/29691513/use-of-java-static-modifier-and-static-block). – assylias Apr 17 '15 at 13:27
  • 3
    I would *at least* have formatted the latter more like the former... – Jon Skeet Apr 17 '15 at 13:27
  • i think this is an organizational thing. at least one of the static formatters, RFC_1123_DATE, has to use the static initializer block, the others do so that they can be grouped together without violating the code layout conventions. since each formatter builds on previous ones it's important they be in order in one place. – Nathan Hughes Apr 17 '15 at 13:42

0 Answers0