Search in sources :

Example 36 with Calendar

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

the class MCRMetaHistoryDate method setBisDate.

/**
 * This method set the bis to the given date.
 *
 * @param set_date
 *            a date string
 * @param calstr
 *            the calendar as String, one of CALENDARS.
 */
public final void setBisDate(String set_date, String calstr) {
    Calendar c = bis;
    try {
        c = MCRCalendar.getHistoryDateAsCalendar(set_date, true, calstr);
    } catch (Exception e) {
        LOGGER.warn("The bis date {} for calendar {} is false.", set_date, calstr);
        c = null;
    }
    setBisDate(c);
}
Also used : MCRCalendar(org.mycore.common.MCRCalendar) Calendar(com.ibm.icu.util.Calendar) GregorianCalendar(com.ibm.icu.util.GregorianCalendar) MCRException(org.mycore.common.MCRException)

Example 37 with Calendar

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

the class MCRCalendarTest method getDateToFormattedString.

/*
     * Test method for 'org.mycore.datamodel.metadata.MCRCalendar.getDateToFormattedString()'
     */
@Test
public void getDateToFormattedString() {
    GregorianCalendar default_calendar = new GregorianCalendar();
    default_calendar.set(1964, 1, 24);
    String dstring;
    Calendar cal;
    // gregorian without format
    cal = (GregorianCalendar) default_calendar.clone();
    dstring = MCRCalendar.getCalendarDateToFormattedString(cal);
    assertEquals("calendar without format", "1964-02-24 AD", dstring);
    // gregorian with format
    cal = (GregorianCalendar) default_calendar.clone();
    dstring = MCRCalendar.getCalendarDateToFormattedString(cal, "dd.MM.yyyy G");
    assertEquals("calendar with format dd.MM.yyyy G", "24.02.1964 AD", dstring);
    // gregorian with format
    cal = (GregorianCalendar) default_calendar.clone();
    dstring = MCRCalendar.getCalendarDateToFormattedString(cal, "yyyy-MM-dd");
    assertEquals("gregorian with format yyyy-MM-dd", "1964-02-24", dstring);
}
Also used : Calendar(com.ibm.icu.util.Calendar) GregorianCalendar(com.ibm.icu.util.GregorianCalendar) GregorianCalendar(com.ibm.icu.util.GregorianCalendar) Test(org.junit.Test)

Example 38 with Calendar

use of com.ibm.icu.util.Calendar 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)

Example 39 with Calendar

use of com.ibm.icu.util.Calendar in project ofbiz-framework by apache.

the class TechDataServices method capacityRemaining.

/**
 * Used to to request the remain capacity available for dateFrom in a TechDataCalenda,
 * If the dateFrom (param in) is not  in an available TechDataCalendar period, the return value is zero.
 *
 * @param techDataCalendar        The TechDataCalendar cover
 * @param dateFrom                        the date
 * @return  long capacityRemaining
 */
public static long capacityRemaining(GenericValue techDataCalendar, Timestamp dateFrom) {
    GenericValue techDataCalendarWeek = null;
    // TODO read TechDataCalendarExcWeek to manage execption week (maybe it's needed to refactor the entity definition
    try {
        techDataCalendarWeek = techDataCalendar.getRelatedOne("TechDataCalendarWeek", true);
    } catch (GenericEntityException e) {
        Debug.logError("Pb reading Calendar Week associated with calendar" + e.getMessage(), module);
        return 0;
    }
    // TODO read TechDataCalendarExcDay to manage execption day
    Calendar cDateTrav = Calendar.getInstance();
    cDateTrav.setTime(dateFrom);
    Map<String, Object> position = dayStartCapacityAvailable(techDataCalendarWeek, cDateTrav.get(Calendar.DAY_OF_WEEK));
    int moveDay = ((Integer) position.get("moveDay")).intValue();
    if (moveDay != 0)
        return 0;
    Time startTime = (Time) position.get("startTime");
    Double capacity = (Double) position.get("capacity");
    Timestamp startAvailablePeriod = new Timestamp(UtilDateTime.getDayStart(dateFrom).getTime() + startTime.getTime() + cDateTrav.get(Calendar.ZONE_OFFSET) + cDateTrav.get(Calendar.DST_OFFSET));
    if (dateFrom.before(startAvailablePeriod))
        return 0;
    Timestamp endAvailablePeriod = new Timestamp(startAvailablePeriod.getTime() + capacity.longValue());
    if (dateFrom.after(endAvailablePeriod))
        return 0;
    return endAvailablePeriod.getTime() - dateFrom.getTime();
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) Calendar(com.ibm.icu.util.Calendar) Time(java.sql.Time) UtilDateTime(org.apache.ofbiz.base.util.UtilDateTime) Timestamp(java.sql.Timestamp)

Example 40 with Calendar

use of com.ibm.icu.util.Calendar in project ofbiz-framework by apache.

the class TechDataServices method endPreviousDay.

/**
 * Used to move in a TechDataCalenda, produce the Timestamp for the end of the previous day available and its associated capacity.
 * If the dateFrom (param in) is not  in an available TechDataCalendar period, the return value is the previous day available
 *
 * @param techDataCalendar        The TechDataCalendar cover
 * @param dateFrom                        the date
 * @return a map with Timestamp dateTo, Double previousCapacity
 */
public static Map<String, Object> endPreviousDay(GenericValue techDataCalendar, Timestamp dateFrom) {
    Map<String, Object> result = new HashMap<String, Object>();
    Timestamp dateTo = null;
    GenericValue techDataCalendarWeek = null;
    // TODO read TechDataCalendarExcWeek to manage exception week (maybe it's needed to refactor the entity definition
    try {
        techDataCalendarWeek = techDataCalendar.getRelatedOne("TechDataCalendarWeek", true);
    } catch (GenericEntityException e) {
        Debug.logError("Pb reading Calendar Week associated with calendar" + e.getMessage(), module);
        return ServiceUtil.returnError("Pb reading Calendar Week associated with calendar");
    }
    // TODO read TechDataCalendarExcDay to manage execption day
    Calendar cDateTrav = Calendar.getInstance();
    cDateTrav.setTime(dateFrom);
    Map<String, Object> position = dayEndCapacityAvailable(techDataCalendarWeek, cDateTrav.get(Calendar.DAY_OF_WEEK));
    Time startTime = (Time) position.get("startTime");
    int moveDay = ((Integer) position.get("moveDay")).intValue();
    Double capacity = (Double) position.get("capacity");
    dateTo = (moveDay == 0) ? dateFrom : UtilDateTime.getDayEnd(dateFrom, Long.valueOf(moveDay));
    Timestamp endAvailablePeriod = new Timestamp(UtilDateTime.getDayStart(dateTo).getTime() + startTime.getTime() + capacity.longValue() + cDateTrav.get(Calendar.ZONE_OFFSET) + cDateTrav.get(Calendar.DST_OFFSET));
    if (dateTo.after(endAvailablePeriod)) {
        dateTo = endAvailablePeriod;
    } else {
        dateTo = UtilDateTime.getDayStart(dateTo, -1);
        cDateTrav.setTime(dateTo);
        position = dayEndCapacityAvailable(techDataCalendarWeek, cDateTrav.get(Calendar.DAY_OF_WEEK));
        startTime = (Time) position.get("startTime");
        moveDay = ((Integer) position.get("moveDay")).intValue();
        capacity = (Double) position.get("capacity");
        if (moveDay != 0)
            dateTo = UtilDateTime.getDayStart(dateTo, moveDay);
        dateTo.setTime(dateTo.getTime() + startTime.getTime() + capacity.longValue() + cDateTrav.get(Calendar.ZONE_OFFSET) + cDateTrav.get(Calendar.DST_OFFSET));
    }
    result.put("dateTo", dateTo);
    result.put("previousCapacity", position.get("capacity"));
    return result;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) HashMap(java.util.HashMap) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) Calendar(com.ibm.icu.util.Calendar) Time(java.sql.Time) UtilDateTime(org.apache.ofbiz.base.util.UtilDateTime) Timestamp(java.sql.Timestamp)

Aggregations

Calendar (com.ibm.icu.util.Calendar)75 Timestamp (java.sql.Timestamp)37 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)24 GenericValue (org.apache.ofbiz.entity.GenericValue)24 Delegator (org.apache.ofbiz.entity.Delegator)17 Date (java.util.Date)14 HashMap (java.util.HashMap)12 Locale (java.util.Locale)12 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)11 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)11 GregorianCalendar (com.ibm.icu.util.GregorianCalendar)10 ArrayList (java.util.ArrayList)8 SimpleDateFormat (java.text.SimpleDateFormat)6 LinkedList (java.util.LinkedList)6 EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)6 Map (java.util.Map)5 TimeDuration (org.apache.ofbiz.base.util.TimeDuration)5 BigDecimal (java.math.BigDecimal)4 Time (java.sql.Time)4 UtilDateTime (org.apache.ofbiz.base.util.UtilDateTime)4