Java Reference
In-Depth Information
Formatter Default Locale: en_US
Short Date: 4/19/12
Medium Date: Apr 19, 2012
Long Date: April 19, 2012
Full Date: Thursday, April 19, 2012
Short Time: 4:30 PM
Short Datetime: 4/19/12 4:30 PM
Medium Datetime: Apr 19, 2012 4:30:20 PM
German Medium Datetime: 19.04.2012 16:30:20
Indian(en) Short Datetime: 19/4/12 4:30 PM
Indian(en) Medium Datetime: 19 Apr, 2012 4:30:20 PM
Using the DateTimeFormatterBuilder Class
Internally, all datetime formatters are obtained using DateTimeFormatterBuilder . Typically, you will not need to use
this class. The previously discussed methods are sufficient in almost all use cases. The class has a no-args constructor
and many appendXxx() methods. You create an instance of the class and call those appendXxx() methods to build the
desired formatter. Finally, call the toFomatter() method to get a DateTimeFormatter object. The following snippet of
code builds a DateTimeFormatter object to format a date in the format like “Christmas in YEAR is on WEEK_DAY”:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import static java.time.format.TextStyle.FULL_STANDALONE;
import static java.time.temporal.ChronoField.DAY_OF_WEEK;
import static java.time.temporal.ChronoField.YEAR;
...
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendLiteral("Christmas in ")
.appendValue(YEAR)
.appendLiteral(" is on ")
.appendText(DAY_OF_WEEK, FULL_STANDALONE)
.toFormatter();
LocalDate ld = LocalDate.of(2014, 12, 25);
String str = ld.format(formatter);
System.out.println(str);
Christmas in 2014 is on Thursday
You can create the same formatter using a pattern, which is a lot easier to write and read than the above code
using the DateTimeFormatterBuilder .
 
Search WWH ::




Custom Search