Example usage for java.text DateFormat parse

List of usage examples for java.text DateFormat parse

Introduction

In this page you can find the example usage for java.text DateFormat parse.

Prototype

public abstract Date parse(String source, ParsePosition pos);

Source Link

Document

Parse a date/time string according to the given parse position.

Usage

From source file:org.opendatakit.briefcase.util.WebUtils.java

/**
 * Parse a string into a datetime value. Tries the common Http formats, the
 * iso8601 format (used by Javarosa), the default formatting from
 * Date.toString(), and a time-only format.
 * /*ww  w  .j a  v a 2 s  .  c  om*/
 * @param value
 * @return
 */
public static final Date parseDate(String value) {
    if (value == null || value.length() == 0)
        return null;

    String[] iso8601Pattern = new String[] { PATTERN_ISO8601 };

    String[] localizedParsePatterns = new String[] {
            // try the common HTTP date formats that have time zones
            PATTERN_RFC1123, PATTERN_RFC1036, PATTERN_DATE_TOSTRING };

    String[] localizedNoTzParsePatterns = new String[] {
            // ones without timezones... (will assume UTC)
            PATTERN_ASCTIME };

    String[] tzParsePatterns = new String[] { PATTERN_ISO8601, PATTERN_ISO8601_DATE, PATTERN_ISO8601_TIME };

    String[] noTzParsePatterns = new String[] {
            // ones without timezones... (will assume UTC)
            PATTERN_ISO8601_WITHOUT_ZONE, PATTERN_NO_DATE_TIME_ONLY,
            PATTERN_YYYY_MM_DD_DATE_ONLY_NO_TIME_DASH };

    Date d = null;
    // iso8601 parsing is sometimes off-by-one when JR does it...
    d = parseDateSubset(value, iso8601Pattern, null, TimeZone.getTimeZone("GMT"));
    if (d != null)
        return d;
    // try to parse with the JavaRosa parsers
    d = DateUtils.parseDateTime(value);
    if (d != null)
        return d;
    d = DateUtils.parseDate(value);
    if (d != null)
        return d;
    d = DateUtils.parseTime(value);
    if (d != null)
        return d;
    // try localized and english text parsers (for Web headers and interactive filter spec.)
    d = parseDateSubset(value, localizedParsePatterns, Locale.ENGLISH, TimeZone.getTimeZone("GMT"));
    if (d != null)
        return d;
    d = parseDateSubset(value, localizedParsePatterns, null, TimeZone.getTimeZone("GMT"));
    if (d != null)
        return d;
    d = parseDateSubset(value, localizedNoTzParsePatterns, Locale.ENGLISH, TimeZone.getTimeZone("GMT"));
    if (d != null)
        return d;
    d = parseDateSubset(value, localizedNoTzParsePatterns, null, TimeZone.getTimeZone("GMT"));
    if (d != null)
        return d;
    // try other common patterns that might not quite match JavaRosa parsers
    d = parseDateSubset(value, tzParsePatterns, null, TimeZone.getTimeZone("GMT"));
    if (d != null)
        return d;
    d = parseDateSubset(value, noTzParsePatterns, null, TimeZone.getTimeZone("GMT"));
    if (d != null)
        return d;
    // try the locale- and timezone- specific parsers
    {
        DateFormat formatter = DateFormat.getDateTimeInstance();
        ParsePosition pos = new ParsePosition(0);
        d = formatter.parse(value, pos);
        if (d != null && pos.getIndex() == value.length()) {
            return d;
        }
    }
    {
        DateFormat formatter = DateFormat.getDateInstance();
        ParsePosition pos = new ParsePosition(0);
        d = formatter.parse(value, pos);
        if (d != null && pos.getIndex() == value.length()) {
            return d;
        }
    }
    {
        DateFormat formatter = DateFormat.getTimeInstance();
        ParsePosition pos = new ParsePosition(0);
        d = formatter.parse(value, pos);
        if (d != null && pos.getIndex() == value.length()) {
            return d;
        }
    }
    throw new IllegalArgumentException("Unable to parse the date: " + value);
}

From source file:org.opendatakit.common.utils.WebUtils.java

/**
 * Parse a string into a datetime value. Tries the common Http formats, the
 * iso8601 format (used by Javarosa), the default formatting from
 * Date.toString(), and a time-only format.
 *
 * @param value//from   w w  w  .j  a  va2 s .  c  o  m
 * @return
 */
public static final Date parseDate(String value) {
    if (value == null || value.length() == 0)
        return null;

    String[] iso8601Pattern = new String[] { PATTERN_ISO8601 };

    String[] localizedParsePatterns = new String[] {
            // try the common HTTP date formats that have time zones
            PATTERN_RFC1123, PATTERN_RFC1036, PATTERN_DATE_TOSTRING };

    String[] localizedNoTzParsePatterns = new String[] {
            // ones without timezones... (will assume UTC)
            PATTERN_ASCTIME };

    String[] tzParsePatterns = new String[] { PATTERN_ISO8601, PATTERN_ISO8601_DATE, PATTERN_ISO8601_TIME };

    String[] noTzParsePatterns = new String[] {
            // ones without timezones... (will assume UTC)
            PATTERN_ISO8601_WITHOUT_ZONE, PATTERN_NO_DATE_TIME_ONLY, PATTERN_YYYY_MM_DD_DATE_ONLY_NO_TIME_DASH,
            PATTERN_GOOGLE_DOCS };

    Date d = null;
    // iso8601 parsing is sometimes off-by-one when JR does it...
    d = parseDateSubset(value, iso8601Pattern, null, TimeZone.getTimeZone("GMT"));
    if (d != null)
        return d;
    // try to parse with the JavaRosa parsers
    d = DateUtils.parseDateTime(value);
    if (d != null)
        return d;
    d = DateUtils.parseDate(value);
    if (d != null)
        return d;
    d = DateUtils.parseTime(value);
    if (d != null)
        return d;
    // try localized and english text parsers (for Web headers and interactive
    // filter spec.)
    d = parseDateSubset(value, localizedParsePatterns, Locale.ENGLISH, TimeZone.getTimeZone("GMT"));
    if (d != null)
        return d;
    d = parseDateSubset(value, localizedParsePatterns, null, TimeZone.getTimeZone("GMT"));
    if (d != null)
        return d;
    d = parseDateSubset(value, localizedNoTzParsePatterns, Locale.ENGLISH, TimeZone.getTimeZone("GMT"));
    if (d != null)
        return d;
    d = parseDateSubset(value, localizedNoTzParsePatterns, null, TimeZone.getTimeZone("GMT"));
    if (d != null)
        return d;
    // try other common patterns that might not quite match JavaRosa parsers
    d = parseDateSubset(value, tzParsePatterns, null, TimeZone.getTimeZone("GMT"));
    if (d != null)
        return d;
    d = parseDateSubset(value, noTzParsePatterns, null, TimeZone.getTimeZone("GMT"));
    if (d != null)
        return d;
    // try the locale- and timezone- specific parsers
    {
        DateFormat formatter = DateFormat.getDateTimeInstance();
        ParsePosition pos = new ParsePosition(0);
        d = formatter.parse(value, pos);
        if (d != null && pos.getIndex() == value.length()) {
            return d;
        }
    }
    {
        DateFormat formatter = DateFormat.getDateInstance();
        ParsePosition pos = new ParsePosition(0);
        d = formatter.parse(value, pos);
        if (d != null && pos.getIndex() == value.length()) {
            return d;
        }
    }
    {
        DateFormat formatter = DateFormat.getTimeInstance();
        ParsePosition pos = new ParsePosition(0);
        d = formatter.parse(value, pos);
        if (d != null && pos.getIndex() == value.length()) {
            return d;
        }
    }
    throw new IllegalArgumentException("Unable to parse the date: " + value);
}

From source file:com.sun.socialsite.util.DateUtil.java

/**
 * Parse data as either 6-char or 8-char format.
 *///from  ww  w  .j av a 2  s .c o m
public static Date parseWeblogURLDateString(String dateString, TimeZone tz, Locale locale) {

    Date ret = new Date();
    DateFormat char8DateFormat = DateUtil.get8charDateFormat();
    DateFormat char6DateFormat = DateUtil.get6charDateFormat();

    if (dateString != null && dateString.length() == 8 && StringUtils.isNumeric(dateString)) {
        ParsePosition pos = new ParsePosition(0);
        ret = char8DateFormat.parse(dateString, pos);

        // make sure the requested date is not in the future
        Date today = null;
        Calendar todayCal = Calendar.getInstance();
        todayCal = Calendar.getInstance(tz, locale);
        todayCal.setTime(new Date());
        today = todayCal.getTime();
        if (ret.after(today)) {
            ret = today;
        }

    } else if (dateString != null && dateString.length() == 6 && StringUtils.isNumeric(dateString)) {
        ParsePosition pos = new ParsePosition(0);
        ret = char6DateFormat.parse(dateString, pos);

        // make sure the requested date is not in the future
        Calendar todayCal = Calendar.getInstance();
        todayCal = Calendar.getInstance(tz, locale);
        todayCal.setTime(new Date());
        Date today = todayCal.getTime();
        if (ret.after(today)) {
            ret = today;
        }
    }

    return ret;
}