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:com.dianping.cache.service.condition.OperationLogSearchCondition.java

public void setOperateEndAsStr(String operateEnd) throws ParseException {
    this.operateEnd = DateUtils.parseDate(operateEnd + " 23:59:59", new String[] { DATE_PATTERN });
}

From source file:com.nbp.nmp.benefit.common.CommonDataHandleUtil.java

/**
 * ?? date    ?/*from   w  w w  . j a  v  a2s  .  com*/
 * ? ?? ?? "yyyyMMdd"
 * @param dateString
 * @param dateFormat
 * @return
 */
public static Date stringToDate(String dateString, String dateFormat) {
    if (dateFormat == null) {
        dateFormat = "yyyyMMdd";
    }
    String[] parttern = { dateFormat };

    try {
        return DateUtils.parseDate(dateString, parttern);
    } catch (Exception e) {
        return null;
    }
}

From source file:gemlite.core.common.DateUtil.java

public final static Date parse(String strDate) {
    Date dt = null;//from  w  w  w  .ja  va  2 s.c o m
    try {
        dt = DateUtils.parseDate(strDate, supportPatterns);
    } catch (ParseException e) {
        LogUtil.getCoreLog().error("Date:" + strDate, e);
    }
    return dt;
}

From source file:com.ms.commons.test.common.convert.impl.DateConverter.java

@Override
public Date internalConvert(Object value) {
    if ((value.getClass() == double.class) || (value.getClass() == Double.class)) {
        return HSSFDateUtil.getJavaDate((Double) value);
    }/*from  w w w .  jav  a2s . co  m*/
    if (value.getClass() == String.class) {
        String sv = value.toString().trim();
        if (sv.length() == 0) {
            return null;
        }

        if (sv.startsWith("=")) {
            try {
                SimpleDateMathExpressionBuilder sme = new SimpleDateMathExpressionBuilder();
                MathExpression me = MathExpressionParseUtil.parse(sv.substring(1), sme);
                return (Date) me.evaluate(null);
            } catch (MathParseException e) {
                throw new RuntimeException(e);
            }
        } else {
            try {
                return DateUtils.parseDate(sv, parsePatterns);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }
    }

    try {
        return DateUtils.parseDate(String.valueOf(value), parsePatterns);
    } catch (ParseException e) {
        throw new RuntimeException(e);
    }
}

From source file:gov.nih.nci.ess.ae.DomainToGridObjectConverter.java

private Date convert(TSDateTime tsDateTime) {
    try {//w ww . ja  v  a  2s.  co m
        if (tsDateTime != null && tsDateTime.getNullFlavor() == null) {
            String value = tsDateTime.getValue();
            if (value != null) {
                return DateUtils.parseDate(value, new String[] { TS_DATETIME_PATTERN });
            }
        }
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:gemlite.core.internal.domain.utilClass.MqDataSource.java

public final Date getDate(String name) {
    String str = map.get(name);//w w w .  j a v  a  2s.  c o m

    Date dt = null;
    try {
        dt = DateUtils.parseDate(str, DomainUtil.gs_date_pattern);
    } catch (Exception e) {
        LogUtil.getCoreLog().error("Name:" + name + " value:" + str, e);
    }
    return dt;
}

From source file:gemlite.core.common.DateUtil.java

public final static Date parse(String strDate, String pattern) {
    Date dt = null;//from ww w.ja  va 2  s.  co m
    try {
        dt = DateUtils.parseDate(strDate, new String[] { pattern });
    } catch (ParseException e) {
        LogUtil.getCoreLog().error("Date:" + strDate, e);
    }
    return dt;
}

From source file:cn.powerdash.libsystem.common.converter.DeepConverter.java

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override/*from www.  jav  a2  s.c  o  m*/
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 new SimpleDateFormat(TIMESTAMP_FORMAT).format((Timestamp) value);
        } else if (Date.class.isAssignableFrom(sourceClass) && targetClass.isAssignableFrom(String.class)) {
            return new SimpleDateFormat(DATE_FORMAT).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:com.ikanow.infinit.e.harvest.utils.DateUtility.java

public synchronized static long parseDate(String sDate) {
    if (null == _allowedDatesArray_startsWithLetter) {
        _allowedDatesArray_startsWithLetter = new String[] { DateFormatUtils.SMTP_DATETIME_FORMAT.getPattern(),

                "MMM d, yyyy hh:mm a", "MMM d, yyyy HH:mm", "MMM d, yyyy hh:mm:ss a", "MMM d, yyyy HH:mm:ss",
                "MMM d, yyyy hh:mm:ss.SS a", "MMM d, yyyy HH:mm:ss.SS",

                "EEE MMM dd HH:mm:ss zzz yyyy", "EEE MMM dd yyyy HH:mm:ss zzz",
                "EEE MMM dd yyyy HH:mm:ss 'GMT'Z (zzz)", };
        _allowedDatesArray_numeric_1 = new String[] { "yyyy-MM-dd'T'HH:mm:ss'Z'",
                DateFormatUtils.ISO_DATE_FORMAT.getPattern(),
                DateFormatUtils.ISO_DATE_TIME_ZONE_FORMAT.getPattern(),
                DateFormatUtils.ISO_DATETIME_FORMAT.getPattern(),
                DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern() };

        _allowedDatesArray_numeric_2 = new String[] { "yyyyMMdd", "yyyyMMdd hh:mm a", "yyyyMMdd HH:mm",
                "yyyyMMdd hh:mm:ss a", "yyyyMMdd HH:mm:ss", "yyyyMMdd hh:mm:ss.SS a", "yyyyMMdd HH:mm:ss.SS",
                // Julian, these are unlikely
                "yyyyDDD", "yyyyDDD hh:mm a", "yyyyDDD HH:mm", "yyyyDDD hh:mm:ss a", "yyyyDDD HH:mm:ss",
                "yyyyDDD hh:mm:ss.SS a", "yyyyDDD HH:mm:ss.SS", };
        _allowedDatesArray_stringMonth = new String[] { "dd MMM yy", "dd MMM yy hh:mm a", "dd MMM yy HH:mm",
                "dd MMM yy hh:mm:ss a", "dd MMM yy HH:mm:ss", "dd MMM yy hh:mm:ss.SS a",
                "dd MMM yy HH:mm:ss.SS", };
        _allowedDatesArray_numericMonth = new String[] { "MM dd yy", "MM dd yy hh:mm a", "MM dd yy HH:mm",
                "MM dd yy hh:mm:ss a", "MM dd yy HH:mm:ss", "MM dd yy hh:mm:ss.SS a", "MM dd yy HH:mm:ss.SS", };
    }//from   w ww.  java 2s.c  o m

    // Starts with day or month:

    String sDateTmp = sDate;
    if (Character.isLetter(sDate.charAt(0))) {
        try {
            Date date = DateUtils.parseDate(sDate, _allowedDatesArray_startsWithLetter);
            return date.getTime();
        } catch (Exception e) {
        } // keep going         
    } //TESTED
    else if (Character.isLetter(sDate.charAt(5))) {

        // month must be string, doesn't start with day though

        try {
            int index = sDate.indexOf(':');
            if (index > 0) {
                sDate = new StringBuffer(sDate.substring(0, index).replaceAll("[./-]", " "))
                        .append(sDate.substring(index)).toString();
            } else {
                sDate = sDate.replaceAll("[ ./-]", " ");
            }
            Date date = DateUtils.parseDate(sDate, _allowedDatesArray_stringMonth);
            return date.getTime();
        } catch (Exception e) {
        } // keep going                              
    } //TESTED
    else {

        // Starts with a number most likely...

        int n = 0;
        for (; n < 4; ++n) {
            if (!Character.isDigit(sDate.charAt(n))) {
                break;
            }
        }
        if (4 == n) {

            // (Probably starts with a year)            

            // One of the formal formats starting with a year            

            try {
                Date date = DateUtils.parseDate(sDate, _allowedDatesArray_numeric_1);
                return date.getTime();
            } catch (Exception e) {
            } // keep going

            // Something more ad hoc starting with a year                        

            try {
                int index = sDate.indexOf(':');
                if (index > 0) {
                    sDate = new StringBuffer(sDate.substring(0, index).replace("-", ""))
                            .append(sDate.substring(index)).toString();
                } else {
                    sDate = sDate.replace("-", "");
                }
                Date date = DateUtils.parseDate(sDate, _allowedDatesArray_numeric_2);
                return date.getTime();
            } catch (Exception e) {
            } // keep going                     
        } //TESTED

        // Probably starts with a day         

        try {
            int index = sDate.indexOf(':');
            if (index > 0) {
                sDate = new StringBuffer(sDate.substring(0, index).replaceAll("[./-]", " "))
                        .append(sDate.substring(index)).toString();
            } else {
                sDate = sDate.replaceAll("[./-]", " ");
            }
            Date date = DateUtils.parseDate(sDate, _allowedDatesArray_numericMonth);
            return date.getTime();
        } //TESTED
        catch (Exception e) {
        } // keep going                     

    }
    sDate = sDateTmp;

    // If we're here, nothing's worked, try "natural language processing"

    try {
        return Chronic.parse(sDate).getBeginCalendar().getTime().getTime();
    } //TESTED
    catch (Exception e2) {
        // Error all the way out
        throw new RuntimeException("Can't parse: " + sDate);
    } //TESTED
}

From source file:eu.cloud4soa.relationalrepo.MonitoringStatisticRepositoryTest.java

@Test
@Ignore/*from w w w.  j a  v a  2s. c  o m*/
public void testSelect() throws ParseException {
    monitoringJob = new MonitoringJob();
    monitoringJob.setId(new Long(18));

    String[] pattern = new String[1];
    pattern[0] = "yyyy-MM-dd HH:mm:ss";

    Date start = DateUtils.parseDate("2012-07-02 04:06:56", pattern);
    Date end = DateUtils.parseDate("2012-07-02 16:01:33", pattern);

    List<MonitoringStatistic> list = monitoringStatisticRepository.retrieveAllInRangeLimited(monitoringJob,
            start, end, 500);
    System.out.println(list.size());
}