1
我的要求是格式化日期,如「新年在」+ someYear +「已打開」+ someDay。 我們如何使用DateTimeFormatterBuilder實現這一目標?如何在Java 8中使用DateTimeFormatterBuilder進行自定義日期分層?
我的要求是格式化日期,如「新年在」+ someYear +「已打開」+ someDay。 我們如何使用DateTimeFormatterBuilder實現這一目標?如何在Java 8中使用DateTimeFormatterBuilder進行自定義日期分層?
我嘗試這樣做如下:
DateTimeFormatter customFormatter = new DateTimeFormatterBuilder()
.appendLiteral("New Year in ")
.appendValue(ChronoField.YEAR)
.appendLiteral(" was on ")
.appendText(ChronoField.DAY_OF_WEEK,TextStyle.FULL_STANDALONE)
.toFormatter();
LocalDate ld1 = LocalDate.of(2016, Month.JANUARY, 1);
String formatted = ld1.format(customFormatter);
System.out.println(formatted);
但是,我認爲它有一個流程,我們需要明確地傳遞準確的新的一年的日期。但是,它解決了我特別解析/格式化日期的問題。
這裏需要建築師嗎?也許你還可以使用「'yyyy'中的'新年'是'EEEE'的模式? (文字可以被apostrophs逃脫) –