最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Is DateTimeFormatter missing ofYearMonth() method? - Stack Overflow

programmeradmin0浏览0评论

Is DateTimeFormatter missing ofYearMonth() method?

It has

ofLocalizedDateTime()
ofLocalizedDate()
ofLocalizedTime()

If using ofPattern(), it is not locale sensitive.

Is DateTimeFormatter missing ofYearMonth() method?

It has

ofLocalizedDateTime()
ofLocalizedDate()
ofLocalizedTime()

If using ofPattern(), it is not locale sensitive.

Share Improve this question edited Feb 3 at 9:10 Anonymous 86.4k15 gold badges162 silver badges177 bronze badges asked Feb 3 at 6:16 eastwatereastwater 5,64013 gold badges69 silver badges148 bronze badges 4
  • 4 There's ofLocalizedPattern added in Java 19. – Slaw Commented Feb 3 at 6:34
  • 1 yes, it is missing that method, and also a YearMonthStyle ... and a lot more combinations (YearDayOfYear, YearQuarterOfYear, YearWeekOfYear, ...) that is why we can (must) use patterns (or DateTimeFormatterBuilder) -- maybe because it is not expected to be used that often – user85421 Commented Feb 3 at 7:48
  • Yes, it seems to be a design choice. I am told that the CLDR (Unicode Common Locale Data Repository) has localized formats for many more combinations than just date, time and date&time. But they are not offered as methods on DateTimeFormatter. – Anonymous Commented Feb 3 at 9:08
  • 1 Why the down-vote? This is a valid Question. – Basil Bourque Commented Feb 3 at 17:04
Add a comment  | 

1 Answer 1

Reset to default 4

Yes.

By digging into the JDK source code, we can see that the class responsible for formatting localised date/times is LocalizedPrinterParser. This class has three fields - dateStyle, timeStyle, and requestedPattern. The former two fields are set by ofLocalizedDateTime, ofLocalizedDate, and ofLocalizedTime. requestedPattern is set by ofLocalizedPattern.

That's all DateTimeFormatter currently supports for localised formats, The closest to a ofLocalizedYearMonth would be to use a pattern such as yyyyMM in ofLocalizedPattern. Example:

System.out.println(
    DateTimeFormatter
        .ofLocalizedPattern("yyyyMM")
        .withLocale(Locale.CHINESE).format(LocalDate.now())
);

// prints 公元2025年2月

Unlike the other three methods, you cannot specify a FormatStyle, but there might be other pattern strings available for a given locale that produce a longer/shorter output.

The available patterns for each locale can be found in the CLDR repo, under the <availableFormats> tag in the XML file for a given locale. For example, here is where all the available formats for English for the Gregorian calendar is listed. This page explains how the requested pattern is matched against the ones in the CLDR.

Note that Java's APIs closely match how the formats are specified in the XML files in CLDR. The different format styles for dates are listed under the <dateFormats> tag, the format styles for times are listed under the <timeFormats> tag, and similarly for the <dateTimeFormats> tag. There is no <yearMonthFormats> or anything like that.

So unless the CLDR updates its format to include something like that, there is not much that the JDK can do.

发布评论

评论列表(0)

  1. 暂无评论