Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
604 views
in Technique[技术] by (71.8m points)

java - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...