1

i have this function to get date:

fun getDate() {

    var hijriMonths = resources.getStringArray(R.array.hijri_months)
    val dateFormatter: DateTimeFormatter = DateTimeFormatter.ofPattern("dd-MM-uuuu")
    var sdf = SimpleDateFormat("dd-MM-yyyy",Locale.ENGLISH)
    val c = Calendar.getInstance()
    try {
        c.time = sdf.parse(currentDate("dd-MM-yyyy"))
    } catch (e: ParseException) {
        e.printStackTrace()
    }
    c.add(
        Calendar.DATE,
        viewModel.days_changed
    )

    var _date: String = sdf.format(c.time)
    val gregorianDate: LocalDate = LocalDate.parse(_date, dateFormatter)
    val islamicDate: HijrahDate = HijrahDate.from(gregorianDate)
    islamic_date.text = islamicDate.get(ChronoField.DAY_OF_MONTH).toString()+" "+hijriMonths[islamicDate.get(
        ChronoField.MONTH_OF_YEAR
    ) - 1]+" "+islamicDate.get(ChronoField.YEAR) +getString(R.string.hijriSign)

    sdf = SimpleDateFormat("dd MMMM yyyy", Locale.ENGLISH)
    try {
        c.time = sdf.parse(currentDate("dd MMMM yyyy"))
    } catch (e: ParseException) {
        e.printStackTrace()
    }
    c.add(
        Calendar.DATE,
        viewModel.days_changed
    )
    _date = sdf.format(c.time)
    var day_name = SimpleDateFormat("EEEE", Locale.ENGLISH).format(c.time)

    date.text = _date
    day.text = day_name

}

I'm getting these dates which is correct but the only wrong thing about it is the day of the Islamic date (it add one day) supposed to be 2 no 3 Muharram

enter image description here

what did i do wrong here?

Enigma
  • 353
  • 4
  • 14
  • Variants of the Islamic calendar exist. Possibly your variant differs from the one used by your software. – Ole V.V. Aug 11 '21 at 06:38
  • You are mixing old and modern, which is a bad idea and causes needless complication. When you can use the modern `DateTimeFormatter`, `LocalDate` and `HijrahDate`, don’t also mix in the poorly designed and long outdated `Date`, `Calendar` and `SimpleDateFormat`. The modern API, java.time, gives you all the functionality you need. – Ole V.V. Aug 11 '21 at 06:41
  • Thank you for explaining, what should i do now? – Enigma Aug 11 '21 at 07:31
  • Avoid using any date-related classes from the `java.util` package, and stick to the `java.time` package. You can look up tutorials on how to use it. – Tenfour04 Aug 11 '21 at 12:26
  • I have sort of reproduced. From `HijrahDate.from(LocalDate.of(2021, Month.AUGUST, 11))` I got `Hijrah-umalqura AH 1443-01-03`. – Ole V.V. Aug 11 '21 at 19:22

1 Answers1

2

The problem

Variants of the Islamic calendar exist. They may differ by one or two days on certain dates. The HijrahDate class from java.time that I think that you used gives you the Umm al-Qura variant. A likely explanation why your result was a day off is that you expected some other variant.

Time4J

Other libraries than java.time offer Islamic calendar support. java.time would be my first go-to library, but if it doesn’t offer what you need, it’s probably time to look elsewhere. One possible place to look is the Time4J library. It supports 11 variants of the Islamic calendar, so chances are that it also offers what you need. I am no Android developer myself, and unfortunately I don’t know whether the Android version of Time4J, Time4A, also supports them. As a taste, in the following I am using a couple of the variants.

    PlainDate gregorianDate = PlainDate.of(2021, Month.AUGUST, 11);
 
    HijriCalendar diyanet = gregorianDate
            .transform(HijriCalendar.class, HijriCalendar.VARIANT_DIYANET);
    System.out.println(diyanet);

    HijriCalendar westIslamicCivil = gregorianDate
            .transform(HijriCalendar.class, HijriAlgorithm.WEST_ISLAMIC_CIVIL);
    System.out.println(westIslamicCivil);

Output is:

AH-1443-01-02[islamic-diyanet]
AH-1443-01-02[islamic-civil]

So in both cases we got 2 Muharram (not 3).

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161