Joda Time没有年份的本地化日期格式 [英] Localised Date format without year with Joda Time

查看:169
本文介绍了Joda Time没有年份的本地化日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以本地格式显示日期但没有年份。应该是:

I'm trying to achieve showing date in local format but without year. So should be:


  • 6月12日为英国

  • 6月12日美国

  • 12 June for UK
  • June 12 for US

是否可以通过Joda时间实现?

Is it possible to achieve with Joda time?

我们尝试过dd MMMM模式,但它不起作用。
我们已经尝试了 StringFormat.longDate()并剥离年份信息,但有更优雅的解决方案吗?

We've tried "dd MMMM" pattern but it doesn't work. We've tried StringFormat.longDate() and strip year info but are there more elegant solution?

推荐答案

在封面下,JodaTime使用JDK的 java.text.DateFormat.getDateInstance(int style,Locale aLocale) - 请参阅源代码 org.joda.time.format.DateTimeFormat.StyleFormatter #getPattern(区域设置区域设置)它如何委托给 java.text.DateFormat

Underneath the covers, JodaTime uses the JDK's java.text.DateFormat.getDateInstance(int style, Locale aLocale) - see the sources of org.joda.time.format.DateTimeFormat.StyleFormatter#getPattern(Locale locale) how it delegates to 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中也是如此。

so the per-locale formats are embedded in the JDK, not even in JodaTime.

使用此代码,您可以获得不同语言环境的预定义模式和输出:

Using this code, you can get the predefined patterns and outputs for different locales:

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

但是,模式仅为四种样式预定义:短,中,长和完整,分别适用于日期和时间部分。请参阅 DateTimeFormat #patternForStyle

However, the patterns are predefined for four styles only: short, medium, long and full, applicable for both date and time parts separately. See the JavaDoc of DateTimeFormat#patternForStyle:


第一个字符是日期样式,第二个字符是时间样式。为短格式指定'S'字符,为中等指定'M',为长指定'L',为完整指定'F'。通过指定样式字符' - '可以省略日期或时间。

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 '-'.

因此,如果您想删除年份部分,需要对从 DateTimeFormat.patternForStyle()获得的模式进行后处理。这可以做到例如。通过删除所有Y和y字符,但一般来说,如果你想为任意语言环境做这件事,可以产生一些混乱的模式。

So if you want to drop the year part, you would need to post-process the pattern obtained from DateTimeFormat.patternForStyle(). This could be done eg. by dropping all "Y" and "y" characters, but in general, if you want to do it for an arbitrary locale, can produce some messed-up patterns.

这篇关于Joda Time没有年份的本地化日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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