Search in sources :

Example 1 with JapaneseCalendar

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

the class MCRCalendar method getCalendarFromJapaneseDate.

/**
 * This method convert a JapaneseCalendar date to a JapaneseCalendar value.
 * The syntax for the japanese input is: <br>
 * <ul>
 * <li> [[[t]t.][m]m.][H|M|S|T][yyy]y <br>
 * H: Heisei; M: Meiji, S: Showa, T: Taiso
 * </li>
 * <li> [H|M|S|T]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 30 else it fill the
 *            date with the lowest value 1 for month and day.
 *
 * @return the JapaneseCalendar date value or null if an error was occurred.
 * @exception MCRException if parsing has an error
 */
protected static JapaneseCalendar getCalendarFromJapaneseDate(String datestr, boolean last) {
    try {
        datestr = datestr.trim();
        // boolean bm = false;
        int start = 0;
        // german or 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;
        String syear = "";
        if (secdot != -1) {
            // day, mon, year
            if (iso) {
                syear = 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;
                syear = datestr.substring(secdot + 1, datestr.length());
            }
        } else {
            if (firstdot != -1) {
                // mon, year
                if (iso) {
                    syear = datestr.substring(start, firstdot);
                    mon = Integer.parseInt(datestr.substring(firstdot + 1, datestr.length())) - 1;
                } else {
                    mon = Integer.parseInt(datestr.substring(start, firstdot)) - 1;
                    syear = datestr.substring(firstdot + 1, datestr.length());
                }
                if (last) {
                    if (mon <= 11) {
                        day = 30;
                    } else {
                        day = 5;
                    }
                } else {
                    day = 1;
                }
            } else {
                // year
                syear = datestr.substring(start, datestr.length());
                if (last) {
                    mon = 12;
                    day = 5;
                } else {
                    mon = 0;
                    day = 1;
                }
            }
        }
        int era;
        switch(syear.substring(0, 1)) {
            case "H":
                era = 235;
                break;
            case "S":
                era = 234;
                break;
            case "T":
                era = 233;
                break;
            case "M":
                era = 232;
                break;
            default:
                era = 0;
        }
        year = Integer.parseInt(syear.substring(1).trim());
        // test of the monthly
        if (mon > 12 || mon < 0) {
            throw new MCRException("The month of the date is inadmissible.");
        }
        // Test of the daily
        if (day > 30 || day < 1 || day > 6 && mon == 12) {
            throw new MCRException("The day of the date is inadmissible.");
        }
        JapaneseCalendar jcal = new JapaneseCalendar();
        // GregorianCalendar jcal = new GregorianCalendar();
        jcal.set(year, mon, day);
        jcal.set(JapaneseCalendar.ERA, era);
        // Calendar correction
        jcal.add(Calendar.DATE, 0);
        GregorianCalendar xcal = new GregorianCalendar();
        xcal.setTime(jcal.getTime());
        return jcal;
    } catch (Exception e) {
        throw new MCRException("The ancient jacanese date is false.", e);
    }
}
Also used : GregorianCalendar(com.ibm.icu.util.GregorianCalendar) JapaneseCalendar(com.ibm.icu.util.JapaneseCalendar)

Aggregations

GregorianCalendar (com.ibm.icu.util.GregorianCalendar)1 JapaneseCalendar (com.ibm.icu.util.JapaneseCalendar)1