Search in sources :

Example 1 with IslamicCalendar

use of com.ibm.icu.util.IslamicCalendar in project mycore by MyCoRe-Org.

the class MCRCalendar method getCalendarFromIslamicDate.

/**
 * This method convert a islamic calendar date to a IslamicCalendar valuei civil mode.
 * The syntax for the islamic input is: <br>
 * <ul>
 * <li> [[[t]t.][m]m.][yyy]y [H.|h.]</li>
 * <li> [.\u0647 | .\u0647 .\u0642] [[[t]t.][m]m.][yyy]y</li>
 * <li> y[yyy][-m[m][-t[t]]] H.|h.</li>
 * </ul>
 *
 * @param date_string
 *            the date as string.
 * @param last
 *            the value is true if the date should be filled with the
 *            highest value of month or day like 12 or 30 else it fill the
 *            date with the lowest value 1 for month and day.
 *
 * @return the IslamicCalendar date value or null if an error was occurred.
 * @exception MCRException if parsing has an error
 */
protected static IslamicCalendar getCalendarFromIslamicDate(String date_string, boolean last) {
    try {
        date_string = date_string.toUpperCase(Locale.ROOT);
        int start = 0;
        int ende = date_string.length();
        int i = date_string.indexOf("H.");
        if (i != -1) {
            ende = i;
        }
        if (date_string.length() > 10) {
            i = date_string.indexOf(".\u0647.\u0642");
            if (i != -1) {
                start = 3;
            } else {
                i = date_string.indexOf(".\u0647");
                if (i != -1) {
                    start = 2;
                }
            }
        }
        date_string = date_string.substring(start, ende).trim();
        // german or ISO?
        start = 0;
        boolean iso = false;
        String token = ".";
        if (date_string.indexOf("-", start + 1) != -1) {
            iso = true;
            token = "-";
        }
        // 
        int firstdot = date_string.indexOf(token, start + 1);
        int secdot = -1;
        if (firstdot != -1) {
            secdot = date_string.indexOf(token, firstdot + 1);
        }
        int day = 1;
        int mon = 0;
        int year = 0;
        if (secdot != -1) {
            // day month year
            if (iso) {
                year = Integer.parseInt(date_string.substring(start, firstdot));
                mon = Integer.parseInt(date_string.substring(firstdot + 1, secdot)) - 1;
                day = Integer.parseInt(date_string.substring(secdot + 1, date_string.length()));
            } else {
                day = Integer.parseInt(date_string.substring(start, firstdot));
                mon = Integer.parseInt(date_string.substring(firstdot + 1, secdot)) - 1;
                year = Integer.parseInt(date_string.substring(secdot + 1, date_string.length()));
            }
        } else {
            if (firstdot != -1) {
                // month year
                if (iso) {
                    year = Integer.parseInt(date_string.substring(start, firstdot));
                    mon = Integer.parseInt(date_string.substring(firstdot + 1, date_string.length())) - 1;
                } else {
                    mon = Integer.parseInt(date_string.substring(start, firstdot)) - 1;
                    year = Integer.parseInt(date_string.substring(firstdot + 1, date_string.length()));
                }
                if (last) {
                    if (mon % 2 == 0) {
                        day = 30;
                    }
                    if (mon % 2 == 1) {
                        day = 29;
                    }
                }
            } else {
                // year
                year = Integer.parseInt(date_string.substring(start, date_string.length()));
                if (last) {
                    mon = 11;
                    day = 29;
                }
            }
        }
        // test of the monthly
        if (mon > 11 || mon < 0) {
            throw new MCRException("The month of the date is inadmissible.");
        }
        // Test of the daily
        if (day > 30 || mon % 2 == 1 && mon < 11 && day > 29 || day < 1) {
            throw new MCRException("The day of the date is inadmissible.");
        }
        IslamicCalendar calendar = new IslamicCalendar();
        calendar.setCivil(true);
        calendar.set(year, mon, day);
        return calendar;
    } catch (Exception e) {
        throw new MCRException("The ancient islamic date is false.", e);
    }
}
Also used : IslamicCalendar(com.ibm.icu.util.IslamicCalendar)

Aggregations

IslamicCalendar (com.ibm.icu.util.IslamicCalendar)1