Imran
Imran

Reputation: 5672

How to detect Hijri dates?

Can anybody tell a good way to detect that a date coming as a String is in Hijri Format? Any good solution that detects String Date type (Hijri) automatically?

Initial Work or Thought: We can check that year starts from 14 or 13 like etc as Hijri years are e.g. "1435"..

I know Java JODA API for conversion both ways:

//Convert Georgian to Hijri

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);
System.out.println(islamicDateString);

//Convert Hijri to Georgian :

Chronology iso = ISOChronology.getInstanceUTC();
Chronology hijri = IslamicChronology.getInstanceUTC();
DateTime dtHijri = new DateTime(1435,04,24,00,00,hijri); 
DateTime dtIso = new DateTime(dtHijri, iso);
System.out.println(dtIso);

Upvotes: 4

Views: 1251

Answers (1)

DevilsHnd - 退した
DevilsHnd - 退した

Reputation: 9192

As far as I know the only real way to distinguish a Hijri date from a Gregorian date is by the supplied year. For most applications this is probably adequate but for others it's pretty much useless. Hijri uses the day/month/year format. As you already know trying to distinguish between the 2nd month and the 2nd day is pointless so you can forget about that but distinguishing the Hijri year from a Gregorian year posses similar problems... Christopher Columbus drifted in and plopped his feet down on North American soil in 1492 but 1492 in Hijri is 2069 in Gregorian.

It's all a matter of establishing a set of rules I suppose. If the code is meant to accept Hijri dates only then make sure the user can only supply Hijri dates and visa versa for supplying Gregorian dates. If Hijri date format is expected and someone supplies a year of 2016 then this is obviously in error since 2016 in Hijri is actually the year 2577. This is a tiny bit to far in the future if you ask me.

How far back or forward can a User supply a date? The difference between Gregorian 2016 and the equivalent Hijri of 1437 (difference of 579 years) is great enough that you can establish a confident interpretation of which date format was actually supplied and make the necessary conversion. With this being said however, what is acceptable for one application may not be acceptable for another.

Upvotes: 6

Related Questions