Datetime 使用Ethiopic年表格式化Joda时间,不显示月份名称

Datetime 使用Ethiopic年表格式化Joda时间,不显示月份名称,datetime,grails,groovy,jodatime,Datetime,Grails,Groovy,Jodatime,我使用Joda time将公历日期和时间转换为埃塞俄比亚年表,并尝试使用“MMMM dd,yyyy”格式对其进行格式化。我希望日期显示为“Meskerem 01,2007”,而不是“1 01,2007”。这是《乔达时代》的错误还是我做错了什么 DateTimeFormatter dtf = DateTimeFormat.forPattern("MMMM dd, yyyy") Date time myDate = new DateTime(2014,9,11,0,0,0,0).withChrono

我使用Joda time将公历日期和时间转换为埃塞俄比亚年表,并尝试使用“MMMM dd,yyyy”格式对其进行格式化。我希望日期显示为“Meskerem 01,2007”,而不是“1 01,2007”。这是《乔达时代》的错误还是我做错了什么

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

请看下面的示例:

@Grab(group='joda-time', module='joda-time', version='2.7') 
import org.joda.time.format.DateTimeFormat
import org.joda.time.format.DateTimeFormatter
import org.joda.time.DateTime
import org.joda.time.chrono.EthiopicChronology

DateTimeFormatter dtf = DateTimeFormat.forPattern("MMMM dd, yyyy")
println new DateTime(2014,9,11,0,0,0,0).toString(dtf)
它正确打印日期-完整的月份名称。现在看一看这个。它声明
年表
对象返回一个新的格式化程序。可能这不是一个bug,但是使用了刚返回的格式化程序,而不是定义的格式化程序

更新

这可能是一个bug,它不适用于
佛教年表

@Grab(group='joda-time', module='joda-time', version='2.7') 
import org.joda.time.format.DateTimeFormat
import org.joda.time.format.DateTimeFormatter
import org.joda.time.DateTime
import org.joda.time.chrono.*

DateTimeFormatter dtf = DateTimeFormat.forPattern("MMMM dd, yyyy")
println new DateTime(2014,9,11,0,0,0,0).withChronology(BuddhistChronology.getInstance()).toString(dtf)

很抱歉,JodaTime在国际化方面从来都不好。但我将提出一个解决办法

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没有为非公历年表管理自己的文本资源,与此相比也是如此。您还可以按照那篇文章中的建议使用专门的字段实现。这里我介绍了一个使用
DateTimePrinter
的解决方案,您必须在上面添加所需的缺少的月份名称。

是的,它正确地打印日期,因为在本例中您没有使用Ethiopic年表。我尝试了不同格式的代码,“dd/MM/yyyy”。输出正确地打印为“01/01/2007”。我已经更新了我的答案,因为我说过这可能是一个错误。很好,我得到了+1:)@欧泊非常感谢,但如果解决方案很好?我不确定,我花了一些时间来编写代码,但代码仍然不完整,也不能说是完美的。这也是一种努力,我相信简单的字符串处理而不是使用专用打印机更容易实现。