Search in sources :

Example 1 with MCRCalendar

use of org.mycore.common.MCRCalendar in project mycore by MyCoRe-Org.

the class MCRXMLFunctions method getISODateFromMCRHistoryDate.

/**
 * The method get a date String in format yyyy-MM-ddThh:mm:ssZ for ancient date values.
 *
 * @param date_value the date string
 * @param field_name the name of field of MCRMetaHistoryDate, it should be 'von' or 'bis'
 * @param calendar_name the name if the calendar defined in MCRCalendar
 * @return the date in format yyyy-MM-ddThh:mm:ssZ
 */
public static String getISODateFromMCRHistoryDate(String date_value, String field_name, String calendar_name) throws ParseException {
    String formatted_date;
    if (field_name == null || field_name.trim().length() == 0) {
        return "";
    }
    boolean use_last_value = false;
    if ("bis".equals(field_name)) {
        use_last_value = true;
    }
    try {
        Calendar calendar = MCRCalendar.getHistoryDateAsCalendar(date_value, use_last_value, calendar_name);
        GregorianCalendar g_calendar = MCRCalendar.getGregorianCalendarOfACalendar(calendar);
        formatted_date = MCRCalendar.getCalendarDateToFormattedString(g_calendar, "yyyy-MM-dd") + "T00:00:00.000Z";
        if (g_calendar.get(GregorianCalendar.ERA) == GregorianCalendar.BC) {
            formatted_date = "-" + formatted_date;
        }
    } catch (Exception e) {
        String errorMsg = "Error while converting date string : " + date_value + " - " + use_last_value + " - " + calendar_name;
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug(errorMsg, e);
        }
        LOGGER.warn(errorMsg);
        return "";
    }
    return formatted_date;
}
Also used : GregorianCalendar(com.ibm.icu.util.GregorianCalendar) MCRCalendar(org.mycore.common.MCRCalendar) Calendar(com.ibm.icu.util.Calendar) GregorianCalendar(com.ibm.icu.util.GregorianCalendar) URISyntaxException(java.net.URISyntaxException) JDOMException(org.jdom2.JDOMException) ParseException(java.text.ParseException) CancellationException(java.util.concurrent.CancellationException) CompletionException(java.util.concurrent.CompletionException) SAXException(org.xml.sax.SAXException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) TransformerException(javax.xml.transform.TransformerException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Aggregations

Calendar (com.ibm.icu.util.Calendar)1 GregorianCalendar (com.ibm.icu.util.GregorianCalendar)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 ParseException (java.text.ParseException)1 CancellationException (java.util.concurrent.CancellationException)1 CompletionException (java.util.concurrent.CompletionException)1 TransformerException (javax.xml.transform.TransformerException)1 JDOMException (org.jdom2.JDOMException)1 MCRCalendar (org.mycore.common.MCRCalendar)1 SAXException (org.xml.sax.SAXException)1