LocalDateFormatter每月基数

Luc*_*s T 3 java date-formatting java-8 localdate

是否有LocalDateFormatter的格式模式来显示当月的基数以及其他值?

例如,我希望打印2016年11月1日,或2017年2月27日.

在此先感谢,卢卡斯

gre*_*449 5

为此,您可以使用DateTimeFormatterBuilder

public DateTimeFormatterBuilder appendText(TemporalField field, Map<Long, String> textLookup)
Run Code Online (Sandbox Code Playgroud)

采用a的方法Map用于查找字段的值.就像是:

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);
Run Code Online (Sandbox Code Playgroud)