Example usage for org.apache.commons.lang.time DateUtils parseDate

List of usage examples for org.apache.commons.lang.time DateUtils parseDate

Introduction

In this page you can find the example usage for org.apache.commons.lang.time DateUtils parseDate.

Prototype

public static Date parseDate(String str, String... parsePatterns) throws ParseException 

Source Link

Document

Parses a string representing a date by trying a variety of different parsers.

The parse will try each parse pattern in turn.

Usage

From source file:org.frat.common.converter.DeepConverter.java

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override/*from   w ww . jav a 2 s  .  c  om*/
public Object convert(Object value, Class targetClass, Object context) {
    if (targetClass != null && value != null) {
        Class<? extends Object> sourceClass = value.getClass();
        if (targetClass.equals(sourceClass)) {
            return value;
        }
        // primitive type
        if (sourceClass.isPrimitive() || targetClass.isPrimitive()) {
            return value;
        }
        // collection
        if (Collection.class.isAssignableFrom(sourceClass) && Collection.class.isAssignableFrom(targetClass)) {
            // not supported
            return null;
        } else if (Timestamp.class.isAssignableFrom(sourceClass)
                && targetClass.isAssignableFrom(String.class)) {
            return timeStampFormatter.format((Timestamp) value);
        } else if (Date.class.isAssignableFrom(sourceClass) && targetClass.isAssignableFrom(String.class)) {
            return dateFormatter.format((Date) value);
        } else if (java.util.Date.class.isAssignableFrom(targetClass)
                && sourceClass.isAssignableFrom(String.class)) {
            try {
                return DateUtils.parseDate(value.toString(), new String[] { "yyyy-MM-dd" });
            } catch (ParseException e) {
                return null;
            }
        } else if (targetClass.isAssignableFrom(sourceClass)) {
            return value;
        }
        // complex type
        if (targetClass.getClassLoader() != null && sourceClass.getClassLoader() != null) {
            return ConverterService.convert(value, targetClass, null);
        }

    }
    return null;
}

From source file:org.gaixie.micrite.common.search.SearchFactory.java

/**
* ??(??),?,?:yyyy-MM-dd HH:mm/* w w  w.ja  v  a  2  s .  c om*/
* 
* @param sourceDate
* @return
*/
private static Date checkDate(String sourceDate) {
    if (sourceDate == null) {
        return null;
    }
    try {
        Date date = DateUtils.parseDate(sourceDate,
                new String[] { "yyyy-MM-dd hh:mm:ss", "yyyy-MM-dd hh:mm", "yyyy-MM-dd" });
        return date;
    } catch (ParseException e) {
    }
    return null;
}

From source file:org.gaixie.micrite.common.search.SearchFactory.java

@SuppressWarnings("unchecked")
private static Object convertValue(Class type, String value, String relation) {
    Object object;//from  w ww.j a  v a2  s  .c o m
    if (relation.equals("between")) {
        List list = new ArrayList();
        String[] betValue = StringUtils.split(value, ';');
        if (type == java.util.Date.class) {
            if (betValue[0].length() == 10)
                betValue[0] += " 00:00:00";
            else
                betValue[0] += ":00";
            if (betValue[1].length() == 10)
                betValue[1] += " 23:59:59";
            else
                betValue[1] += ":59";
            try {
                list.add(DateUtils.parseDate(betValue[0], new String[] { "yyyy-MM-dd hh:mm:ss" }));
                list.add(DateUtils.parseDate(betValue[1], new String[] { "yyyy-MM-dd hh:mm:ss" }));
            } catch (Exception e) {
                throw new RasterFormatException("??");
            }
        } else {
            list.add(OgnlOps.convertValue(betValue[0], type));
            list.add(OgnlOps.convertValue(betValue[1], type));
        }
        object = list;
    } else if (relation.equals("in")) {
        List list = new ArrayList();
        for (String s : StringUtils.split(value, ';')) {
            list.add(OgnlOps.convertValue(s, type));
        }
        object = list;
    } else if (relation.equals("like")) {
        object = OgnlOps.convertValue("%" + value + "%", type);
    } else {
        if (type == java.util.Date.class) {
            if (relation.equals("<") || relation.equals("<=")) {
                if (value.length() == 10)
                    value += " 23:59:59";
                else
                    value += ":59";
            } else {
                if (value.length() == 10)
                    value += " 00:00:00";
                else
                    value += ":00";
            }
            try {
                object = DateUtils.parseDate(value, new String[] { "yyyy-MM-dd hh:mm:ss" });
            } catch (ParseException e) {
                throw new RasterFormatException("??");
            }
        } else
            object = OgnlOps.convertValue(value, type);
    }
    return object;
}

From source file:org.gaixie.micrite.crm.action.CarownerAction.java

public void setEndDate(String endDate) throws ParseException {
    this.endDate = DateUtils.parseDate(endDate + ":00", new String[] { "yyyy-MM-dd hh:mm:ss" });
}

From source file:org.gaixie.micrite.crm.action.CarownerAction.java

public void setStartDate(String startDate) throws ParseException {
    this.startDate = DateUtils.parseDate(startDate + ":00", new String[] { "yyyy-MM-dd hh:mm:ss" });
}

From source file:org.gbif.portal.web.content.dataset.OccurrenceDateFilterHelper.java

/**
 * @see org.gbif.portal.web.content.filter.FilterHelper#preProcess(java.util.List, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
 *///  w  ww .jav  a  2  s. c  o  m
public void preProcess(List<PropertyStoreTripletDTO> triplets, HttpServletRequest request,
        HttpServletResponse response) {

    //pick out date triplets
    List<PropertyStoreTripletDTO> dateTriplets = new ArrayList<PropertyStoreTripletDTO>();
    for (PropertyStoreTripletDTO triplet : triplets) {
        if (triplet.getSubject().equals(occurrenceDateSubject)) {
            dateTriplets.add(triplet);
        }
    }
    triplets.removeAll(dateTriplets);

    //process date triplets
    for (PropertyStoreTripletDTO triplet : dateTriplets) {
        if (triplet.getSubject().equals(occurrenceDateSubject)) {
            String tripletObject = (String) triplet.getObject();
            String startDate = null;
            String endDate = null;

            //is it a range or specific date?
            if (tripletObject != null && tripletObject.indexOf('-') > 0) {
                int indexOfSeparator = tripletObject.indexOf('-');
                startDate = tripletObject.substring(0, indexOfSeparator);
                endDate = tripletObject.substring(indexOfSeparator + 1);
            } else {
                startDate = tripletObject;
                endDate = tripletObject;
            }

            try {
                Date start = DateUtil.setTime(DateUtils.parseDate(startDate, new String[] { "ddMMyyyy" }), 0, 0,
                        0);
                Date end = DateUtil.setTime(DateUtils.parseDate(endDate, new String[] { "ddMMyyyy" }), 23, 59,
                        59);

                triplets.add(new PropertyStoreTripletDTO(triplet.getNamespace(), triplet.getSubject(),
                        greaterThanPredicate, start));
                triplets.add(new PropertyStoreTripletDTO(triplet.getNamespace(), triplet.getSubject(),
                        lessThanPredicate, end));
            } catch (ParseException e) {
                logger.warn(e.getMessage(), e);
                //do nothing for this dodgy triplet
            }
        }
    }
}

From source file:org.gbif.portal.web.content.dataset.OccurrenceDateFilterHelper.java

/**
 * Creates a string of the form 7 June 1987 with internationalization of month.
 * //from  ww  w  .  j a v a  2  s .  c o m
 * @param theDate
 * @return
 */
public String getPrettyPrintDateString(String dateString) {
    Date theDate;
    try {
        theDate = DateUtils.parseDate(dateString, new String[] { "ddMMyyyy" });
    } catch (ParseException e) {
        logger.error(e);
        return "";
    }
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(theDate);
    StringBuffer sb = new StringBuffer();
    sb.append(calendar.get(Calendar.DAY_OF_MONTH));
    sb.append(' ');
    int month = calendar.get(Calendar.MONTH) + 1; //GregorianCalendar indexes from 0 for month
    sb.append(messageSource.getMessage("month." + month, null, null));
    sb.append(' ');
    sb.append(calendar.get(Calendar.YEAR));
    return sb.toString();
}

From source file:org.gbif.portal.web.util.DateUtil.java

/**
 * Construct a date/*w  w  w  .j a  va 2  s .co m*/
 * @param request
 * @param dateFieldName
 * @return
 */
public static Date getDateFromRequest(HttpServletRequest request, String dateFieldName) {

    String day = request.getParameter(dateFieldName + "_day");
    String month = request.getParameter(dateFieldName + "_month");
    String year = request.getParameter(dateFieldName + "_year");
    request.setAttribute(dateFieldName + "_day", day);
    request.setAttribute(dateFieldName + "_month", month);
    request.setAttribute(dateFieldName + "_year", year);

    if (day == null || month == null && year == null)
        return null;

    try {
        return DateUtils.parseDate(day + month + year, new String[] { "ddMMyyyy" });
    } catch (ParseException e) {
        log.trace(e.getMessage(), e);
    }
    return null;
}

From source file:org.gbif.portal.web.util.DateUtil.java

/**
 * Takes a date string and attempts to parse
 * @param dateString/* w w  w. jav a2  s .c  o  m*/
 * @return parse Date object, null if unparsable
 */
public static Date parseDate(String dateString) {
    if (dateString == null)
        return null;
    Date parsedDate = null;
    try {
        dateString = dateString.trim();
        parsedDate = DateUtils.parseDate(dateString, new String[] { "dd/MM/yyyy", "yyyyMMdd", "yyyy MM dd",
                "yyyy/MM/dd", "yyyy-MM-dd", "yyyy dd MM", "yyyy" });
        if (parsedDate != null)
            return parsedDate;
    } catch (Exception e) {
        log.debug(e);
    }
    try {
        parsedDate = DateUtils.parseDate(dateString, new String[] { "ddMMyyyy", "dd MM yyyy", "ddMMMyyyy",
                "dd MMM yyyy", "dd/MM/yyyy", "dd-MM-yyyy", "dd-MMM-yyyy", "dd MM yyyy" });
        return parsedDate;
    } catch (Exception e) {
        log.debug(e);
    }
    return null;
}

From source file:org.gluu.oxtrust.util.jsf.UptimeConverter.java

public Object getAsObject(FacesContext context, UIComponent comp, String value) throws ConverterException {
    if ((value == null) || value.trim().length() == 0) {
        return null;
    }/*from www.  j  a  v a  2  s  .c o  m*/

    try {
        return DateUtils.parseDate(value, dateFormats);
    } catch (ParseException e) {
        throw new ConverterException("Unable to convert " + value + " to seconds!");
    }
}