Search in sources :

Example 1 with BuddhistCalendar

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

the class MCRCalendar method getCalendarFromBuddhistDate.

/**
 * This method convert a BuddhistCalendar date to a IslamicCalendar value.
 * The syntax for the buddhist input is: <br>
 * <ul>
 * <li> [-][[[t]t.][m]m.][yyy]y [B.E.]</li>
 * <li> [-] [[[t]t.][m]m.][yyy]y</li>
 * <li> [-] y[yyy][-m[m][-t[t]]] [B.E.]</li>
 * <li> [-] y[yyy][-m[m][-t[t]]]</li>
 * </ul>
 *
 * @param datestr
 *            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 31 else it fill the
 *            date with the lowest value 1 for month and day.
 *
 * @return the BuddhistCalendar date value or null if an error was occurred.
 * @exception MCRException if parsing has an error
 */
protected static BuddhistCalendar getCalendarFromBuddhistDate(String datestr, boolean last) {
    try {
        datestr = datestr.trim();
        // test before Buddhas
        boolean bb = false;
        int start = 0;
        int ende = datestr.length();
        if (datestr.substring(0, 1).equals("-")) {
            bb = true;
            start = 1;
            datestr = datestr.substring(start).trim();
            ende = datestr.length();
        }
        start = 0;
        if (datestr.length() > 4) {
            int i = datestr.indexOf("B.E.");
            if (i != -1) {
                start = 0;
                ende = i;
            }
        }
        datestr = datestr.substring(start, ende).trim();
        // german oder ISO?
        start = 0;
        boolean iso = false;
        String token = ".";
        if (datestr.indexOf("-", start + 1) != -1) {
            iso = true;
            token = "-";
        }
        // 
        int firstdot = datestr.indexOf(token, start + 1);
        int secdot = -1;
        if (firstdot != -1) {
            secdot = datestr.indexOf(token, firstdot + 1);
        }
        int day = 0;
        int mon = 0;
        int year = 0;
        if (secdot != -1) {
            // day, month, year
            if (iso) {
                year = Integer.parseInt(datestr.substring(start, firstdot));
                mon = Integer.parseInt(datestr.substring(firstdot + 1, secdot)) - 1;
                day = Integer.parseInt(datestr.substring(secdot + 1, datestr.length()));
            } else {
                day = Integer.parseInt(datestr.substring(start, firstdot));
                mon = Integer.parseInt(datestr.substring(firstdot + 1, secdot)) - 1;
                year = Integer.parseInt(datestr.substring(secdot + 1, datestr.length()));
            }
        } else {
            if (firstdot != -1) {
                // month, year
                if (iso) {
                    year = Integer.parseInt(datestr.substring(start, firstdot));
                    mon = Integer.parseInt(datestr.substring(firstdot + 1, datestr.length())) - 1;
                } else {
                    mon = Integer.parseInt(datestr.substring(start, firstdot)) - 1;
                    year = Integer.parseInt(datestr.substring(firstdot + 1, datestr.length()));
                }
                if (last) {
                    if (mon == 0 || mon == 2 || mon == 4 || mon == 6 || mon == 7 || mon == 9 || mon == 11) {
                        day = 31;
                    }
                    if (mon == 1) {
                        day = 28;
                    }
                    if (mon == 3 || mon == 5 || mon == 8 || mon == 10) {
                        day = 30;
                    }
                } else {
                    day = 1;
                }
            } else {
                // year
                year = Integer.parseInt(datestr.substring(start, datestr.length()));
                if (last) {
                    mon = 11;
                    day = 29;
                } else {
                    mon = 0;
                    day = 1;
                }
            }
        }
        BuddhistCalendar budcal = new BuddhistCalendar();
        // test of the monthly
        if (mon > 11 || mon < 0) {
            throw new MCRException("The month of the date is inadmissible.");
        }
        // Test of the daily
        if ((mon == 0 || mon == 2 || mon == 4 || mon == 6 || mon == 7 || mon == 9 || mon == 11) && day > 31 || (mon == 3 || mon == 5 || mon == 8 || mon == 10) && day > 30 || mon == 1 && day > 29 && year % 4 == 0 || mon == 1 && day > 28 && year % 4 > 0 || day < 1) {
            throw new MCRException("The day of the date is inadmissible.");
        }
        if (bb) {
            // if before Buddha
            year = -year + 1;
        }
        if (year == 2125 && mon == 9 && day >= 5 && day < 15) {
            day = 15;
        }
        budcal.set(year, mon, day);
        return budcal;
    } catch (Exception e) {
        throw new MCRException("The ancient buddhist date is false.", e);
    }
}
Also used : BuddhistCalendar(com.ibm.icu.util.BuddhistCalendar)

Aggregations

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