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.

4
  • Possibility of handling exception. Commented Apr 17, 2015 at 13:26
  • 2
    The former is more readable but they are essentially equivalent (ok, not strictly equivalent: stackoverflow.com/questions/29691513/…). Commented Apr 17, 2015 at 13:27
  • 3
    I would at least have formatted the latter more like the former... Commented Apr 17, 2015 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. Commented Apr 17, 2015 at 13:42

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.