Search in sources :

Example 51 with Calendar

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

the class UtilDateTime method toTimeString.

/**
 * Makes a time String in the format HH:MM:SS from a Date. If the seconds are 0, then the output is in HH:MM.
 *
 * @param date The Date
 * @return A time String in the format HH:MM:SS or HH:MM
 */
public static String toTimeString(java.util.Date date) {
    if (date == null) {
        return "";
    }
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    return (toTimeString(calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND)));
}
Also used : Calendar(com.ibm.icu.util.Calendar)

Example 52 with Calendar

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

the class UtilDateTime method getMonthStart.

public static Timestamp getMonthStart(Timestamp stamp, int daysLater, int monthsLater, TimeZone timeZone, Locale locale) {
    Calendar tempCal = toCalendar(stamp, timeZone, locale);
    tempCal.set(tempCal.get(Calendar.YEAR), tempCal.get(Calendar.MONTH), 1, 0, 0, 0);
    tempCal.add(Calendar.MONTH, monthsLater);
    tempCal.add(Calendar.DAY_OF_MONTH, daysLater);
    Timestamp retStamp = new Timestamp(tempCal.getTimeInMillis());
    retStamp.setNanos(0);
    return retStamp;
}
Also used : Calendar(com.ibm.icu.util.Calendar) Timestamp(java.sql.Timestamp)

Example 53 with Calendar

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

the class UtilDateTime method toDate.

/**
 * Makes a Date from separate ints for month, day, year, hour, minute, and second.
 *
 * @param month  The month int
 * @param day    The day int
 * @param year   The year int
 * @param hour   The hour int
 * @param minute The minute int
 * @param second The second int
 * @return A Date made from separate ints for month, day, year, hour, minute, and second.
 */
public static java.util.Date toDate(int month, int day, int year, int hour, int minute, int second) {
    Calendar calendar = Calendar.getInstance();
    try {
        calendar.set(year, month - 1, day, hour, minute, second);
        calendar.set(Calendar.MILLISECOND, 0);
    } catch (Exception e) {
        return null;
    }
    return new java.util.Date(calendar.getTime().getTime());
}
Also used : Calendar(com.ibm.icu.util.Calendar) ParseException(java.text.ParseException) Date(java.util.Date)

Example 54 with Calendar

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

the class TimeDuration method advanceCalendar.

private static int advanceCalendar(Calendar start, Calendar end, int units, int type) {
    if (units >= 1) {
        // Bother, the below needs explanation.
        // 
        // If start has a day value of 31, and you add to the month,
        // and the target month is not allowed to have 31 as the day
        // value, then the day will be changed to a value that is in
        // range.  But, when the code needs to then subtract 1 from
        // the month, because it has advanced to far, the day is *not*
        // set back to the original value of 31.
        // 
        // This bug can be triggered by having a duration of -1 day,
        // then adding this duration to a calendar that represents 0
        // milliseconds, then creating a new duration by using the 2
        // Calendar constructor, with cal1 being 0, and cal2 being the
        // new calendar that you added the duration to.
        // 
        // To solve this problem, we make a temporary copy of the
        // start calendar, and only modify it if we actually have to.
        Calendar tmp = (Calendar) start.clone();
        int tmpUnits = units;
        tmp.add(type, tmpUnits);
        while (tmp.after(end)) {
            tmp.add(type, -1);
            units--;
        }
        if (units != 0) {
            start.add(type, units);
        }
    }
    return units;
}
Also used : Calendar(com.ibm.icu.util.Calendar)

Example 55 with Calendar

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

the class UtilHttp method makeParamValueFromComposite.

/**
 * Given the prefix of a composite parameter, recomposes a single Object from
 * the composite according to compositeType. For example, consider the following
 * form widget field,
 *
 * <pre>
 * {@code
 * <field name="meetingDate">
 *     <date-time type="timestamp" input-method="time-dropdown">
 * </field>
 * }
 * </pre>
 *
 * The result in HTML is three input boxes to input the date, hour and minutes separately.
 * The parameter names are named meetingDate_c_date, meetingDate_c_hour, meetingDate_c_minutes.
 * Additionally, there will be a field named meetingDate_c_compositeType with a value of "Timestamp".
 * where _c_ is the COMPOSITE_DELIMITER. These parameters will then be recomposed into a Timestamp
 * object from the composite fields.
 *
 * @param request
 * @param prefix
 * @return Composite object from data or null if not supported or a parsing error occurred.
 */
public static Object makeParamValueFromComposite(HttpServletRequest request, String prefix, Locale locale) {
    String compositeType = request.getParameter(makeCompositeParam(prefix, "compositeType"));
    if (UtilValidate.isEmpty(compositeType)) {
        return null;
    }
    // collect the composite fields into a map
    Map<String, String> data = new HashMap<>();
    for (Enumeration<String> names = UtilGenerics.cast(request.getParameterNames()); names.hasMoreElements(); ) {
        String name = names.nextElement();
        if (!name.startsWith(prefix + COMPOSITE_DELIMITER)) {
            continue;
        }
        // extract the suffix of the composite name
        String suffix = name.substring(name.indexOf(COMPOSITE_DELIMITER) + COMPOSITE_DELIMITER_LENGTH);
        // and the value of this parameter
        String value = request.getParameter(name);
        // key = suffix, value = parameter data
        data.put(suffix, value);
    }
    if (Debug.verboseOn()) {
        Debug.logVerbose("Creating composite type with parameter data: " + data.toString(), module);
    }
    // handle recomposition of data into the compositeType
    if ("Timestamp".equals(compositeType)) {
        String date = data.get("date");
        String hour = data.get("hour");
        String minutes = data.get("minutes");
        String ampm = data.get("ampm");
        if (date == null || date.length() < 10) {
            return null;
        }
        if (UtilValidate.isEmpty(hour)) {
            return null;
        }
        if (UtilValidate.isEmpty(minutes)) {
            return null;
        }
        boolean isTwelveHour = UtilValidate.isNotEmpty(ampm);
        // create the timestamp from the data
        try {
            int h = Integer.parseInt(hour);
            Timestamp timestamp = Timestamp.valueOf(date.substring(0, 10) + " 00:00:00.000");
            Calendar cal = Calendar.getInstance(locale);
            cal.setTime(timestamp);
            if (isTwelveHour) {
                boolean isAM = ("AM".equals(ampm) ? true : false);
                if (isAM && h == 12) {
                    h = 0;
                }
                if (!isAM && h < 12) {
                    h += 12;
                }
            }
            cal.set(Calendar.HOUR_OF_DAY, h);
            cal.set(Calendar.MINUTE, Integer.parseInt(minutes));
            return new Timestamp(cal.getTimeInMillis());
        } catch (IllegalArgumentException e) {
            Debug.logWarning("User input for composite timestamp was invalid: " + e.getMessage(), module);
            return null;
        }
    }
    // we don't support any other compositeTypes (yet)
    return null;
}
Also used : HashMap(java.util.HashMap) Calendar(com.ibm.icu.util.Calendar) 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