LocalDateFormatter月的基数 [英] LocalDateFormatter day of month cardinality

查看:78
本文介绍了LocalDateFormatter月的基数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there a format pattern for LocalDateFormatter to display the cardinality of the day of the month, as well as other values?

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

For example, I wish to print 2016 November First, or 2017 February Twenty-seventh.

预先感谢,卢卡斯

推荐答案

您可以使用 DateTimeFormatterBuilder

public DateTimeFormatterBuilder appendText(TemporalField field, Map<Long, String> textLookup)

带有 Map

方法,该方法用于查找字段的值.像这样:

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);

这篇关于LocalDateFormatter月的基数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆