Date-time objects have no format
Your comments indicate that you are conflating date-time objects with strings representing their values.
Date-time objects have no format. The objects can generate strings to represent their values, but such strings are distinct and separate. Likewise, a date-time object can be instantiated by parsing a string, but the new date-object will be distinct and separate.
Localizing
You can generate a string as shown in the Answer by VHS where you specify a certain formatting pattern. But generally a better route is to let java.time localize automatically for you.
To localize, specify:
FormatStyle
to determine how long or abbreviated should the string be.
Locale
to determine (a) the human language for translation of name of day, name of month, and such, and (b) the cultural norms deciding issues of abbreviation, capitalization, punctuation, separators, and such.
Example:
Locale l = Locale.CANADA_FRENCH ; // Or Locale.US, Locale.ITALY, etc.
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( FormatStyle.FULL ).withLocale( l );
String output = myLocalDate.format( f );
The DateTimeFormatterBuilder
class is for special needs. Generally all you need is the DateTimeFormatter
class.
See this example code run live at IdeOne.com. But beware of limitation with that IdeOne.com web site’s implementation of Java: Alternate locales are ignored, hard-coded to a single English locale (perhaps Locale.US).
DateTimeFormatter formatter = DateTimeFormatter.BASIC_ISO_DATE;
LocalDate localDate = LocalDate.parse("20120403", formatter);
String outputStandard = localDate.toString();
DateTimeFormatter fCanadaFrench =
DateTimeFormatter.ofLocalizedDate( FormatStyle.LONG )
.withLocale( Locale.CANADA_FRENCH ) ;
String outputCanadaFrench = localDate.format( fCanadaFrench ) ;
DateTimeFormatter fUS =
DateTimeFormatter.ofLocalizedDate( FormatStyle.LONG )
.withLocale( Locale.US ) ;
String outputUS = localDate.format( fUS ) ;
localDate.toString(): 2012-04-03
outputCanadaFrench: 3 avril 2012
outputUS: April 3, 2012