javadategregorian-calendardate-conversionhijri

converting gregorian to hijri date


I want to convert from Gregorian to Hijri(Islamic) date and I need a java class for this converting. I want to give it an Gregorian date in format of "yyyy/mm/dd" as string and it give me the Hijri date in the same format. can anyone help me?


Solution

  • Firstly, separate out the conversion part from the formatting/parsing part. You can deal with those easily later - and there are lots of questions on Stack Overflow about that.

    Personally I'd use Joda Time, which typically makes life much simpler. For example:

    import org.joda.time.Chronology;
    import org.joda.time.LocalDate;
    import org.joda.time.chrono.IslamicChronology;
    import org.joda.time.chrono.ISOChronology;
    
    public class Test {
        public static void main(String[] args) throws Exception {
            Chronology iso = ISOChronology.getInstanceUTC();
            Chronology hijri = IslamicChronology.getInstanceUTC();
    
            LocalDate todayIso = new LocalDate(2013, 3, 31, iso);
            LocalDate todayHijri = new LocalDate(todayIso.toDateTimeAtStartOfDay(),
                                                 hijri);
            System.out.println(todayHijri); // 1434-05-19
        }
    } 
    

    (It feels like there should be a cleaner way of converting dates between chronologies, but I couldn't find one immediately.)