15

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);
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
infinite_loop_
  • 179
  • 1
  • 11
  • 1
    Have a look here [Joda Time bug or my mistake?][1] [1]: http://stackoverflow.com/questions/1525932/joda-time-bug-or-my-mistake-java-joda-time-dates-as-strings-parsing The second answer provides a workaround. – Boris Feb 25 '14 at 11:37
  • Try out solution described in http://stackoverflow.com/a/1572131/1688570 it worked for me. – TouDick Mar 14 '14 at 00:03
  • thank you @TouDick but i used another solution that doesn't use JODA. – infinite_loop_ Mar 26 '14 at 13:15

2 Answers2

3

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.

Zeki
  • 5,107
  • 1
  • 20
  • 27
3

tl;dr

java.time.chrono.HijrahDate.from(
    LocalDate.of( 2014 , 2 , 25 ) 
)
.get( ChronoField.YEAR )

1435

java.time

The Joda-Time project is now in maintenance mode, advising migration to the java.time classes.

The java.time classes offer a HijrahChronology and HijrahDate in the java.time.chrono package.

For Java 6 & 7, and for earlier Android, the ThreeTen-Backport project also offers a HijrahChronology and HijrahDate.

LocalDate ld = LocalDate.of( 2014 , 2 , 25 ) ;
HijrahDate hd = HijrahDate.from( ld  );
String output = hd.toString() ;

output: Hijrah-umalqura AH 1435-04-25

As for other formats, the format method with DateTimeFormatter seem to revert to ISO chronology.

Locale locale = Locale.forLanguageTag( "en-US-u-ca-islamic-umalqura" );
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( FormatStyle.FULL ).withLocale( locale );
String output2 = hd.format( f );

output2: Tuesday, February 25, 2014

While I do not have time at the moment to do so, I suggest looking at the source code of the HijrahDate::toString method.

You can roll-your-own formatting by using the example code as seen in the java.time.chrono package documentation.

int day = hd.get( ChronoField.DAY_OF_MONTH );
int dow = hd.get( ChronoField.DAY_OF_WEEK );
int month = hd.get( ChronoField.MONTH_OF_YEAR );
int year = hd.get( ChronoField.YEAR );
System.out.printf( "%s %s %d-%s-%d%n" , hd.getChronology().getId() , dow , day , month , year );

Hijrah-umalqura 2 25-4-1435


See also:

Convert Jalali calendar to Georgian in java

Get a gregorian date from Hijri date strings


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Thank you so much for this :) I had my app crash last year because of a library, re-built the entire app and didn't know it's the library cuz there was another error. Lost all my hope but then found your answers and ThreeTenABP, wonderful work mate! – Kashan Ahmad Aug 26 '21 at 08:07