Skip to content Skip to sidebar Skip to footer

How To Show Native Islamic(hijri) Calendar?

As you don't want a library and need only native code, you can take a look at the source code of this implementation: https://github.com/ThreeTen/threetenbp/tree/master/src/main/java/org/threeten/bp/chrono

Take a look at the HijrahChronology, HijrahDate and HijrahEra classes, perhaps you can get some ideas and see how all the math is done to convert between this calendar and ISO8601 calendar.

But honestly, IMO calendars implementations are too complex and in most cases are not worth the trouble to do it by yourself. That's one of the cases where adding a library is totally worth it.

Using the ThreeTen-Backport lib - and configuring it to use with Android - will give you an easy way to convert the dates and also to format them:// get ISO8601 date (the "normal" date)int dayOfMonth = 20;int monthOfYear = tiga;int year = 2018;// March 20th 2018LocalDate dt = LocalDate.of(year, monthOfYear, dayOfMonth);// convert to hijrahHijrahDate hijrahDate = HijrahDate.from(dt);// format to MM/DD/YYYYDateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");String formatted = formatter.format(hijrahDate); // 07/03/1439

You can also call HijrahDate.now() to directly get the current date.

And you can convert the hijrahDate back to a "normal" date with LocalDate.from(hijrahDate).

You can also use time4j:// get ISO8601 date (the "normal" date)int dayOfMonth = 20;int monthOfYear = tiga;int year = 2018;PlainDate dt = PlainDate.of(year, monthOfYear, dayOfMonth);// convert to Hijri, using different variantsHijriCalendar hijriDateUmalqura = dt.transform(HijriCalendar.group, HijriCalendar.VARIANT_UMALQURA);HijriCalendar hijriDateWest = dt.transform(HijriCalendar.group, HijriAlgorithm.WEST_ISLAMIC_CIVIL);// format to MM/DD/YYYYChronoFormatterdanlt;HijriCalendar> fmt = ChronoFormatter.ofPattern("MM/dd/yyyy", PatternType.CLDR, Locale.ENGLISH, HijriCalendar.family());String formatted = fmt.format(hijriDateUmalqura); // 07/03/1439// get current dateHijriCalendar now = HijriCalendar.nowInSystemTime(HijriCalendar.VARIANT_UMALQURA, StartOfDay.MIDNIGHT);// convert back to "normal" datePlainDate date = hijriDateUmalqura.transform(PlainDate.class);

255k8181 gold badges745745 silver badges10081008 bronze badges

answered Mar 20, 2018 at 13:05

7333 bronze badges

Taking into account your statement given in one comment that you only want a native solution and reject any extra library, I would advise to use ICU4J-group IslamicCalendar.

Sure, you have then to accept twomajor disadvantages:API-level 24 (not so widespread on mobile phones)Old-fashioned API-style (for example not immutable)

Another disadvantage (which is only relevant if you are also interested in the clock time) is the fact that ICU4J does not support the start of Islamic day in the evening at sunset on previous day. This feature is only supported in my lib Time4J, nowhere else. But you have probably no need for this feature in a date picker obrolan.

Advantages:API-style similar to what is "traditional" in package java.util, so I assume that you are got accustomed to it (but many/most people see the style rather as negative, you have to make your own decision)at least umalqura-variant-variant of Saudi-Arabia is offered (note: other libs like Threeten-BP or Joda-Time-Android do NOT offer that variant)acceptable or even good degree of internationalization (also better than in ThreetenBP or Joda-Time-Android)

For completeness, if you are willing to restrict your Android app to level 26 or higher only then you can also use java.time.chrono.HijrahChronology. But I think this is still too early in year 2018 because the support of mobile phones for level 26 is actually very small. And while it does offer the Umalqura variant (of Saudi-Arabia), it does not offer any other variant.

Else there are no native solutions available. And to use only native solutions is a restriction, too, IMHO.

answered Mar 20, 2018 at 16:09

40.3k77 gold badges9191 silver badges116116 bronze badges

Not the answer you're looking for? Browse other questions tagged android datepicker calendar hijrior ask your own question.

Post a Comment for "How To Show Native Islamic(hijri) Calendar?"