Reputation: 77
I'm using Joda time to convert gregorian date and time to Ethiopic chronology and I'm trying to format it with "MMMM dd, yyyy" format. I expect the Date to be displayed as "Meskerem 01, 2007" instead I get "1 01, 2007". Is this a bug in Joda time or am I doing something wrong?
DateTimeFormatter dtf = DateTimeFormat.forPattern("MMMM dd, yyyy")
Date time myDate = new DateTime(2014,9,11,0,0,0,0).withChronology(EthiopicChronology.getInstance()).toString(dtf)
Upvotes: 3
Views: 265
Reputation: 44071
Well, JodaTime has never been good in internationalization, sorry. But I will present a workaround.
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
Just to note: This code (as suggested by @Opal?) does not work for me:
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
The reason is the sad fact that Joda-Time does not manage its own text resources for non-gregorian chronologies, compare also this SO-post. You can also use a specialized field implementation as suggested in that post. Here I have presented a solution using DateTimePrinter
on which you have to add the missing month names you need.
Upvotes: 2
Reputation: 84864
Have a look at the example below:
@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)
It prints the date correctly - full month name. Now have a look at the docs. It states that Chronology
object returns a new formatter. Probably this is not a bug but the just returned formatter is used instead of the one defined.
UPDATE
It might be a bug, it doesn't work with BuddhistChronology
as well:
@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)
Upvotes: 0