Example usage for org.apache.commons.lang3.time FastDateFormat parse

List of usage examples for org.apache.commons.lang3.time FastDateFormat parse

Introduction

In this page you can find the example usage for org.apache.commons.lang3.time FastDateFormat parse.

Prototype

@Override
    public Date parse(final String source, final ParsePosition pos) 

Source Link

Usage

From source file:org.apache.openmeetings.service.calendar.caldav.IcalUtils.java

/**
 * Adapted from DateUtils to support Timezones, and parse ical dates into {@link java.util.Date}.
 * Note: Replace FastDateFormat to java.time, when shifting to Java 8 or higher.
 *
 * @param str      Date representation in String.
 * @param patterns Patterns to parse the date against
 * @param _timeZone Timezone of the Date.
 * @return <code>java.util.Date</code> representation of string or
 * <code>null</code> if the Date could not be parsed.
 *//*from  w w w . j  av  a 2  s  .c o  m*/
public Date parseDate(String str, String[] patterns, TimeZone _timeZone) {
    FastDateFormat parser;
    Locale locale = WebSession.get().getLocale();

    TimeZone timeZone = str.endsWith("Z") ? TimeZone.getTimeZone("UTC") : _timeZone;

    ParsePosition pos = new ParsePosition(0);
    for (String pattern : patterns) {
        parser = FastDateFormat.getInstance(pattern, timeZone, locale);
        pos.setIndex(0);
        Date date = parser.parse(str, pos);
        if (date != null && pos.getIndex() == str.length()) {
            return date;
        }
    }
    log.error("Unable to parse the date: " + str + " at " + -1);
    return null;
}