Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joda-Time Formatting for Islamic Date

I am using Joda-Time to get the Islamic date in the dd MMMM yyyy but I am always getting dd MM yyyy.

Any advise? could it be that Hijri dates are not supported for formatting? It's not clear on the Joda-Time website.

 DateTime dtISO = new DateTime(2014,2,25,0,0,0,0);
 DateTime dtIslamic = dtISO.withChronology(IslamicChronology.getInstance());
 String formatIslamic= "dd MMMM yyyy";
 DateTimeFormatter formatter = DateTimeFormat.forPattern(formatIslamic).withChronology( IslamicChronology.getInstance());
 String islamicDateString = formatter.print(dtIslamic);
like image 719
infinite_loop_ Avatar asked Feb 25 '14 10:02

infinite_loop_


People also ask

How do I change the date format in Joda-time?

How to change the SimpleDateFormat to jodatime? String s = "2014-01-15T14:23:50.026"; DateTimeFormatter dtf = DateTimeFormat. forPattern("yyyy-MM-dd'T'HH:mm:ss. SSSS"); DateTime instant = dtf.

What is Joda-time format?

Joda-Time provides a comprehensive formatting system. There are two layers: High level - pre-packaged constant formatters. Mid level - pattern-based, like SimpleDateFormat.

Is Joda-time deprecated?

So the short answer to your question is: YES (deprecated).

Does Joda DateTime have time zones?

Adjusting Time ZoneUse the DateTimeZone class in Joda-Time to adjust to a desired time zone. Joda-Time uses immutable objects. So rather than change the time zone ("mutate"), we instantiate a new DateTime object based on the old but with the desired difference (some other time zone). Use proper time zone names.


1 Answers

This is currently not implemented. The BasicChronology class sets the monthOfYear field to use GJMonthOfYearDateTimeField which in turn gets it's data from java.text.DateFormatSymbols. The IslamicChronology uses a sets the monthOfYear field to a BasicMonthOfYearDateTimeField which has the following implementation of getAsText:

public String getAsText(int fieldValue, Locale locale) {
    return Integer.toString(fieldValue);
}

What someone needs to do is to create a IslamicMonthOfYearDateTimeField that extends BasicMonthOfYearDateTimeField and overrides the method so that it returns the name of the month rather than the numeric value of the month. This could either be done in the joda-time codebase, or completely outside. To get this working outside of joda, just extend IslamicChronology and override assemble to pull in your new IslamicMonthOfYearDateTimeField. I'd issue a pull request to joda-time myself, but I doubt they'd accept a non-localized solution.

like image 159
Zeki Avatar answered Sep 22 '22 05:09

Zeki