datetime - 使用埃塞俄比亚年表格式化 Joda Time 不显示月份名称

标签 datetime grails groovy jodatime

我正在使用 Joda 时间将格里高利历日期和时间转换为埃塞俄比亚纪年法,并尝试将其格式化为“MMMM dd, yyyy”格式。我希望日期显示为“Meskerem 01, 2007”而不是“1 01, 2007”。这是 Joda 时间的错误还是我做错了什么?

DateTimeFormatter dtf = DateTimeFormat.forPattern("MMMM dd, yyyy")
Date time myDate = new DateTime(2014,9,11,0,0,0,0).withChronology(EthiopicChronology.getInstance()).toString(dtf)

最佳答案

嗯,JodaTime在国际化方面一直做的不好,sorry。但我会提出一个解决方法。

DateTimePrinter printer =
        new DateTimePrinter() {

    @Override
    public int estimatePrintedLength() {
        return 8; // type the maximum chars you need for printing ethiopic months
    }

    @Override
    public void printTo(StringBuffer buf, ReadablePartial partial, Locale locale) {
        int index = LocalDate.now().indexOf(DateTimeFieldType.monthOfYear());
        int month = partial.getValue(index);
        print(buf, month);
    }

    @Override
    public void printTo(Writer out, ReadablePartial partial, Locale locale)
        throws IOException
    {
        StringBuffer sb = new StringBuffer();
        printTo(sb, partial, locale);
        out.write(sb.toString());
    }

    @Override
    public void printTo(
        StringBuffer buf,
        long         instant,
        Chronology   chrono,
        int          displayOffset,
        DateTimeZone displayZone,
        Locale       locale
    ) {
        LocalDate date = new LocalDate(instant, EthiopicChronology.getInstance());
        print(buf, date.getMonthOfYear());
    }

    @Override
    public void printTo(
        Writer       out,
        long         instant,
        Chronology   chrono,
        int          displayOffset,
        DateTimeZone displayZone,
        Locale       locale
    ) throws IOException
    {
        StringBuffer sb = new StringBuffer();
        printTo(sb, instant, chrono, displayOffset, displayZone, locale);
        out.write(sb.toString());
    }

    private void print(StringBuffer buf, int month) {
        switch (month) {
            case 1 : // attention: ethiopic month index
                buf.append("Meskerem");
                break;
            // case 2: etc.
            default :
                buf.append(month);
        }
    }
};

DateTimeFormatter dtf =
    new DateTimeFormatterBuilder().append(printer).appendPattern(" dd, yyyy").toFormatter();
Chronology chronology = EthiopicChronology.getInstance();
DateTime ethiopic = new DateTime(2014, 9, 11, 0, 0, 0).withChronology(chronology);
String myDate = ethiopic.toString(dtf);
System.out.println(ethiopic); // 2007-01-01T00:00:00.000+02:00 (ethiopic month number and year and day-of-month!!!)
System.out.println(myDate); // Meskerem 01, 2007

请注意:此代码(如@Opal 所建议的那样?)对我不起作用:

Chronology chronology = EthiopicChronology.getInstance();
DateTimeFormatter dtf =
    DateTimeFormat.forPattern("MMMM dd, yyyy").withChronology(chronology);
String myDate = new DateTime(2014, 9, 11, 0, 0, 0).toString(dtf2);
System.out.println(myDate); // 1 01, 2007

原因是 Joda-Time 不管理自己的非公历年表的文本资源这一可悲的事实,也比较这个 SO-post .您还可以按照该帖子中的建议使用专门的字段实现。在这里,我提出了一个使用 DateTimePrinter 的解决方案,您必须在其中添加所需的缺失月份名称。

关于datetime - 使用埃塞俄比亚年表格式化 Joda Time 不显示月份名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29896558/

相关文章:

mysql - Fluent Nhibernate mysql日期问题

grails - Grails设置脚手架下拉框的默认值

Groovy:定义不带 "def"的变量与使用锚定有什么区别?

python strftime 不适用于小时分钟和秒

python - 有条件地将 DataFrame 中的 Int64 字段替换为 Int64 字段的月份加上 Datetime64[ns] 字段

grails - 取消选择<g:field>后,Grails更新页面

jenkins - 在 Jenkins 管道中解析 XML 文件

hibernate - 无法解决 Grails 2.4.5 中的 HibernateOptimisticLockingFailureException

python - Pandas DataFrame 按时间戳分组

grails - Grails 2 Spring Security插件-成功登录后重定向到/login/denied