I am trying to convert an ISO (Gregorian) date into a hijri (Islamic) date and the compare the first to a hijri date.
What I do and it does not work is:
Chronology iso = ISOChronology.getInstanceUTC();
Chronology hijri = IslamicChronology.getInstanceUTC();
DateTime dtISO = new DateTime("2015-03-12T10:04:43", iso);
System.out.println("dtISO: " + dtISO);
DateTime toHijri = new DateTime(dtISO, hijri);
System.out.println("toHijri: " + toHijri); // 1436-05-21T10:04:43.000Z
DateTimeFormatter formatter = DateTimeFormat.forPattern("YYYY-MM-dd HH-mm");
DateTime hijriDt = new DateTime(formatter.parseDateTime("1442-05-21 10-10"), iso);
if (hijriDt.isAfter(toHijri)) {
System.out.println("hijriDt date is after toHijri date");
}
if (hijriDt.isBefore(toHijri)) {
System.out.println("hijriDt date is before toHijri date");
}
if (hijriDt.isEqual(toHijri)) {
System.out.println("hijriDt date is equal toHijri date");
}
No matter what the hijri date is it always returns "hijriDt date is before toHijri date".
Now as I was experimenting I tried this:
Chronology iso = ISOChronology.getInstanceUTC();
Chronology hijri = IslamicChronology.getInstanceUTC();
LocalDateTime toHijri = new LocalDateTime(new DateTime("2015-03-12T10:04:43", iso), hijri);
System.out.println("toHijri: " + toHijri); // 1436-05-21T10:04:43.000Z
DateTimeFormatter formatter = DateTimeFormat.forPattern("YYYY-MM-dd HH-mm");
LocalDateTime hijriDt = new LocalDateTime(formatter.parseDateTime("1442-05-21 10-10"), iso);
if (hijriDt.isAfter(toHijri)) {
System.out.println("hijriDt date is after toHijri date");
}
if (hijriDt.isBefore(toHijri)) {
System.out.println("hijriDt date is before toHijri date");
}
if (hijriDt.isEqual(toHijri)) {
System.out.println("hijriDt date is equal toHijri date");
}
which works like a charm. Notice how the toHijri date is constructed: LocalDateTime toHijri = new LocalDateTime(new DateTime("2015-03-12T10:04:43", iso), hijri);
A DateTime instance is wrapped inside a LocalDateTime.
Why is this happening? Why the first block does not work?
This is not what you want:
DateTime hijriDt = new DateTime(formatter.parseDateTime("1442-05-21 10-10"), iso);
That's not using the Hijri calendar anywhere:
I suspect you want:
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH-mm")
.withChronology(hijri);
DateTime hijriDt = formatter.parseDateTime("1442-05-21 10-10");
See more on this question at Stackoverflow