我正在使用 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/