Joda Time 没有年份的本地化日期格式
Localised Date format without year with Joda Time
我正在尝试以本地格式显示日期但没有年份。所以应该是:
- 英国 6 月 12 日
- 美国 6 月 12 日
是否可以用Joda时间实现?
我们已经尝试了 "dd MMMM" 模式,但它不起作用。
我们尝试了 StringFormat.longDate()
和剥离年份信息,但有更优雅的解决方案吗?
在幕后,JodaTime 使用 JDK 的 java.text.DateFormat.getDateInstance(int style, Locale aLocale)
- 查看 org.joda.time.format.DateTimeFormat.StyleFormatter#getPattern(Locale locale)
的来源它如何委托给 java.text.DateFormat
:
String getPattern(Locale locale) {
DateFormat f = null;
switch (iType) {
case DATE:
f = DateFormat.getDateInstance(iDateStyle, locale);
break;
case TIME:
f = DateFormat.getTimeInstance(iTimeStyle, locale);
break;
case DATETIME:
f = DateFormat.getDateTimeInstance(iDateStyle, iTimeStyle, locale);
break;
}
if (f instanceof SimpleDateFormat == false) {
throw new IllegalArgumentException("No datetime pattern for locale: " + locale);
}
return ((SimpleDateFormat) f).toPattern();
}
所以每个语言环境的格式都嵌入在 JDK 中,甚至不在 JodaTime 中。
使用此代码,您可以获得不同语言环境的预定义模式和输出:
public static void main(String[] args) {
DateTime dt = DateTime.now();
String usFormat = DateTimeFormat.patternForStyle("L-", Locale.US);
String ukFormat = DateTimeFormat.patternForStyle("L-", Locale.UK);
System.out.println(dt.toString(usFormat));
System.out.println(dt.toString(ukFormat));
}
打印
October 20, 2015
20 October 2015
但是,模式仅预定义为四种样式:short、medium、long 和full,分别适用于日期和时间部分。请参阅 DateTimeFormat#patternForStyle
:
的 JavaDoc
The first character is the date style, and the second character is the time style. Specify a character of 'S' for short style, 'M' for medium, 'L' for long, and 'F' for full. A date or time may be ommitted by specifying a style character '-'.
所以如果你想删除年份部分,你需要post-处理从DateTimeFormat.patternForStyle()
获得的模式。这可以做到,例如。通过删除所有 "Y" 和 "y" 字符,但一般来说,如果您想为任意语言环境执行此操作,可能会产生一些混乱的模式。
我正在尝试以本地格式显示日期但没有年份。所以应该是:
- 英国 6 月 12 日
- 美国 6 月 12 日
是否可以用Joda时间实现?
我们已经尝试了 "dd MMMM" 模式,但它不起作用。
我们尝试了 StringFormat.longDate()
和剥离年份信息,但有更优雅的解决方案吗?
在幕后,JodaTime 使用 JDK 的 java.text.DateFormat.getDateInstance(int style, Locale aLocale)
- 查看 org.joda.time.format.DateTimeFormat.StyleFormatter#getPattern(Locale locale)
的来源它如何委托给 java.text.DateFormat
:
String getPattern(Locale locale) {
DateFormat f = null;
switch (iType) {
case DATE:
f = DateFormat.getDateInstance(iDateStyle, locale);
break;
case TIME:
f = DateFormat.getTimeInstance(iTimeStyle, locale);
break;
case DATETIME:
f = DateFormat.getDateTimeInstance(iDateStyle, iTimeStyle, locale);
break;
}
if (f instanceof SimpleDateFormat == false) {
throw new IllegalArgumentException("No datetime pattern for locale: " + locale);
}
return ((SimpleDateFormat) f).toPattern();
}
所以每个语言环境的格式都嵌入在 JDK 中,甚至不在 JodaTime 中。
使用此代码,您可以获得不同语言环境的预定义模式和输出:
public static void main(String[] args) {
DateTime dt = DateTime.now();
String usFormat = DateTimeFormat.patternForStyle("L-", Locale.US);
String ukFormat = DateTimeFormat.patternForStyle("L-", Locale.UK);
System.out.println(dt.toString(usFormat));
System.out.println(dt.toString(ukFormat));
}
打印
October 20, 2015
20 October 2015
但是,模式仅预定义为四种样式:short、medium、long 和full,分别适用于日期和时间部分。请参阅 DateTimeFormat#patternForStyle
:
The first character is the date style, and the second character is the time style. Specify a character of 'S' for short style, 'M' for medium, 'L' for long, and 'F' for full. A date or time may be ommitted by specifying a style character '-'.
所以如果你想删除年份部分,你需要post-处理从DateTimeFormat.patternForStyle()
获得的模式。这可以做到,例如。通过删除所有 "Y" 和 "y" 字符,但一般来说,如果您想为任意语言环境执行此操作,可能会产生一些混乱的模式。