Java 8 DateTimeFormatters 常量在哪里定义?
Where are the Java 8 DateTimeFormatters constants defined?
我正在查看 DateTimeFormatter
class and I was wondering where are the constants like "M"
, "EEE"
and "YY"
etc are defined. More specifically there should be code like this somewhere given a ChronoField
and TextStyle
,它应该 return DateTimeFormatter
字符串片段,例如(ChronoField.MONTH_OF_YEAR, TextStyle.SHORT)
应该映射到字符串 "MMM"
经过一些源挖掘,我发现这些是在 private static final Map
中预定义的,称为 FIELD_MAP
,它是 DateTimeFormatBuilder class 的成员:
/** Map of letters to fields. */
private static final Map<Character, TemporalField> FIELD_MAP = new HashMap<>();
static {
// SDF = SimpleDateFormat
FIELD_MAP.put('G', ChronoField.ERA); // SDF, LDML (different to both for 1/2 chars)
FIELD_MAP.put('y', ChronoField.YEAR_OF_ERA); // SDF, LDML
FIELD_MAP.put('u', ChronoField.YEAR); // LDML (different in SDF)
FIELD_MAP.put('Q', IsoFields.QUARTER_OF_YEAR); // LDML (removed quarter from 310)
FIELD_MAP.put('q', IsoFields.QUARTER_OF_YEAR); // LDML (stand-alone)
FIELD_MAP.put('M', ChronoField.MONTH_OF_YEAR); // SDF, LDML
FIELD_MAP.put('L', ChronoField.MONTH_OF_YEAR); // SDF, LDML (stand-alone)
FIELD_MAP.put('D', ChronoField.DAY_OF_YEAR); // SDF, LDML
FIELD_MAP.put('d', ChronoField.DAY_OF_MONTH); // SDF, LDML
FIELD_MAP.put('F', ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH); // SDF, LDML
FIELD_MAP.put('E', ChronoField.DAY_OF_WEEK); // SDF, LDML (different to both for 1/2 chars)
FIELD_MAP.put('c', ChronoField.DAY_OF_WEEK); // LDML (stand-alone)
FIELD_MAP.put('e', ChronoField.DAY_OF_WEEK); // LDML (needs localized week number)
FIELD_MAP.put('a', ChronoField.AMPM_OF_DAY); // SDF, LDML
FIELD_MAP.put('H', ChronoField.HOUR_OF_DAY); // SDF, LDML
FIELD_MAP.put('k', ChronoField.CLOCK_HOUR_OF_DAY); // SDF, LDML
FIELD_MAP.put('K', ChronoField.HOUR_OF_AMPM); // SDF, LDML
FIELD_MAP.put('h', ChronoField.CLOCK_HOUR_OF_AMPM); // SDF, LDML
FIELD_MAP.put('m', ChronoField.MINUTE_OF_HOUR); // SDF, LDML
FIELD_MAP.put('s', ChronoField.SECOND_OF_MINUTE); // SDF, LDML
FIELD_MAP.put('S', ChronoField.NANO_OF_SECOND); // LDML (SDF uses milli-of-second number)
FIELD_MAP.put('A', ChronoField.MILLI_OF_DAY); // LDML
FIELD_MAP.put('n', ChronoField.NANO_OF_SECOND); // 310 (proposed for LDML)
FIELD_MAP.put('N', ChronoField.NANO_OF_DAY); // 310 (proposed for LDML)
}
我想我找到了一个 hack 来关闭而不看 @kocko 提到的私有字段:
String getFormat(ChornoField field, TextStyle style) {
new DateTimeFormatterBuilder()
.appendText(field, style)
.toString() // This does not actually return the pattern but close enough??
}
我正在查看 DateTimeFormatter
class and I was wondering where are the constants like "M"
, "EEE"
and "YY"
etc are defined. More specifically there should be code like this somewhere given a ChronoField
and TextStyle
,它应该 return DateTimeFormatter
字符串片段,例如(ChronoField.MONTH_OF_YEAR, TextStyle.SHORT)
应该映射到字符串 "MMM"
经过一些源挖掘,我发现这些是在 private static final Map
中预定义的,称为 FIELD_MAP
,它是 DateTimeFormatBuilder class 的成员:
/** Map of letters to fields. */
private static final Map<Character, TemporalField> FIELD_MAP = new HashMap<>();
static {
// SDF = SimpleDateFormat
FIELD_MAP.put('G', ChronoField.ERA); // SDF, LDML (different to both for 1/2 chars)
FIELD_MAP.put('y', ChronoField.YEAR_OF_ERA); // SDF, LDML
FIELD_MAP.put('u', ChronoField.YEAR); // LDML (different in SDF)
FIELD_MAP.put('Q', IsoFields.QUARTER_OF_YEAR); // LDML (removed quarter from 310)
FIELD_MAP.put('q', IsoFields.QUARTER_OF_YEAR); // LDML (stand-alone)
FIELD_MAP.put('M', ChronoField.MONTH_OF_YEAR); // SDF, LDML
FIELD_MAP.put('L', ChronoField.MONTH_OF_YEAR); // SDF, LDML (stand-alone)
FIELD_MAP.put('D', ChronoField.DAY_OF_YEAR); // SDF, LDML
FIELD_MAP.put('d', ChronoField.DAY_OF_MONTH); // SDF, LDML
FIELD_MAP.put('F', ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH); // SDF, LDML
FIELD_MAP.put('E', ChronoField.DAY_OF_WEEK); // SDF, LDML (different to both for 1/2 chars)
FIELD_MAP.put('c', ChronoField.DAY_OF_WEEK); // LDML (stand-alone)
FIELD_MAP.put('e', ChronoField.DAY_OF_WEEK); // LDML (needs localized week number)
FIELD_MAP.put('a', ChronoField.AMPM_OF_DAY); // SDF, LDML
FIELD_MAP.put('H', ChronoField.HOUR_OF_DAY); // SDF, LDML
FIELD_MAP.put('k', ChronoField.CLOCK_HOUR_OF_DAY); // SDF, LDML
FIELD_MAP.put('K', ChronoField.HOUR_OF_AMPM); // SDF, LDML
FIELD_MAP.put('h', ChronoField.CLOCK_HOUR_OF_AMPM); // SDF, LDML
FIELD_MAP.put('m', ChronoField.MINUTE_OF_HOUR); // SDF, LDML
FIELD_MAP.put('s', ChronoField.SECOND_OF_MINUTE); // SDF, LDML
FIELD_MAP.put('S', ChronoField.NANO_OF_SECOND); // LDML (SDF uses milli-of-second number)
FIELD_MAP.put('A', ChronoField.MILLI_OF_DAY); // LDML
FIELD_MAP.put('n', ChronoField.NANO_OF_SECOND); // 310 (proposed for LDML)
FIELD_MAP.put('N', ChronoField.NANO_OF_DAY); // 310 (proposed for LDML)
}
我想我找到了一个 hack 来关闭而不看 @kocko 提到的私有字段:
String getFormat(ChornoField field, TextStyle style) {
new DateTimeFormatterBuilder()
.appendText(field, style)
.toString() // This does not actually return the pattern but close enough??
}