Is there a format pattern for LocalDateFormatter to display the cardinality of the day of the month, as well as other values?
For example, I wish to print 2016 November First, or 2017 February Twenty-seventh.
Thanks in advance, Lucas
You can do this using DateTimeFormatterBuilder
with the
public DateTimeFormatterBuilder appendText(TemporalField field, Map<Long, String> textLookup)
method that takes a Map
which is used to look up the values for the field. Something like:
static final Map<Long, String> ORDINAL_DAYS = new HashMap<>();
static
{
ORDINAL_DAYS.put(1, "First");
ORDINAL_DAYS.put(2, "Second");
... values for month days 1 .. 31
ORDINAL_DAYS.put(31, "Thirty-first");
}
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendText(ChronoField.YEAR)
.appendLiteral(' ')
.appendText(ChronoField.MONTH_OF_YEAR)
.appendLiteral(' ')
.appendText(ChronoField.DAY_OF_MONTH, ORDINAL_DAYS)
.toFormatter();
String formattedDate = formatter.format(date);